Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions packages/csv-to-pg/__tests__/csv2pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,78 @@ const testCase = resolve(__dirname + '/../__fixtures__/test-case.csv');
it('noop', () => {
expect(true).toBe(true);
});

describe('conflictDoNothing', () => {
it('InsertOne with conflictDoNothing generates ON CONFLICT DO NOTHING AST', () => {
const config = {
schema: 'my-schema',
table: 'my-table',
fields: {
name: 'text'
}
};
const types = parseTypes(config);
const stmt = InsertOne({
schema: config.schema,
table: config.table,
types,
record: { name: 'test' },
conflictDoNothing: true
});

// Verify the AST contains the ON CONFLICT DO NOTHING clause
expect(stmt.RawStmt.stmt.InsertStmt.onConflictClause).toEqual({
action: 'ONCONFLICT_NOTHING'
});
});

it('InsertMany with conflictDoNothing generates ON CONFLICT DO NOTHING AST', () => {
const config = {
schema: 'my-schema',
table: 'my-table',
fields: {
name: 'text'
}
};
const types = parseTypes(config);
const stmt = InsertMany({
schema: config.schema,
table: config.table,
types,
records: [
{ name: 'test1' },
{ name: 'test2' }
],
conflictDoNothing: true
});

// Verify the AST contains the ON CONFLICT DO NOTHING clause
expect(stmt.RawStmt.stmt.InsertStmt.onConflictClause).toEqual({
action: 'ONCONFLICT_NOTHING'
});
});

it('InsertOne without conflictDoNothing has no conflict clause', () => {
const config = {
schema: 'my-schema',
table: 'my-table',
fields: {
name: 'text'
}
};
const types = parseTypes(config);
const stmt = InsertOne({
schema: config.schema,
table: config.table,
types,
record: { name: 'test' }
});

// Verify no conflict clause when conflictDoNothing is not set
expect(stmt.RawStmt.stmt.InsertStmt.onConflictClause).toBeUndefined();
});
});

xdescribe('Insert Many', () => {
it('Insert Many', async () => {
const config = {
Expand Down
9 changes: 6 additions & 3 deletions packages/csv-to-pg/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface ParserConfig {
table: string;
singleStmts?: boolean;
conflict?: string[];
conflictDoNothing?: boolean;
headers?: string[];
delimeter?: string;
json?: boolean;
Expand All @@ -30,7 +31,7 @@ export class Parser {

async parse(data?: Record<string, unknown>[]): Promise<string | void> {
const config = this.config;
const { schema, table, singleStmts, conflict, headers, delimeter } = config;
const { schema, table, singleStmts, conflict, conflictDoNothing, headers, delimeter } = config;

const opts: CsvOptions = {};
if (headers) opts.headers = headers;
Expand Down Expand Up @@ -67,7 +68,8 @@ export class Parser {
table,
types,
record,
conflict
conflict,
conflictDoNothing
})
);
return deparse(stmts);
Expand All @@ -77,7 +79,8 @@ export class Parser {
table,
types,
records,
conflict
conflict,
conflictDoNothing
});
return deparse([stmt]);
}
Expand Down
25 changes: 20 additions & 5 deletions packages/csv-to-pg/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,18 @@ const indexElem = (name: string): Node => ({

import type { OnConflictClause } from '@pgsql/types';

const makeConflictClause = (conflictElems: string[] | undefined, fields: string[]): OnConflictClause | undefined => {
const makeConflictClause = (
conflictElems: string[] | undefined,
fields: string[],
conflictDoNothing?: boolean
): OnConflictClause | undefined => {
// If conflictDoNothing is true, generate ON CONFLICT DO NOTHING without specifying columns
// This catches any unique constraint violation
if (conflictDoNothing) {
return {
action: 'ONCONFLICT_NOTHING'
};
}
if (!conflictElems || !conflictElems.length) return undefined;
const setElems = fields.filter((el) => !conflictElems.includes(el));
if (setElems.length) {
Expand All @@ -157,14 +168,16 @@ interface InsertOneParams {
types: TypesMap;
record: Record<string, unknown>;
conflict?: string[];
conflictDoNothing?: boolean;
}

export const InsertOne = ({
schema = 'public',
table,
types,
record,
conflict
conflict,
conflictDoNothing
}: InsertOneParams): Node => ({
RawStmt: {
stmt: {
Expand All @@ -189,7 +202,7 @@ export const InsertOne = ({
limitOption: 'LIMIT_OPTION_DEFAULT'
}
},
onConflictClause: makeConflictClause(conflict, Object.keys(types)),
onConflictClause: makeConflictClause(conflict, Object.keys(types), conflictDoNothing),
override: 'OVERRIDING_NOT_SET'
}
},
Expand All @@ -203,14 +216,16 @@ interface InsertManyParams {
types: TypesMap;
records: Record<string, unknown>[];
conflict?: string[];
conflictDoNothing?: boolean;
}

export const InsertMany = ({
schema = 'public',
table,
types,
records,
conflict
conflict,
conflictDoNothing
}: InsertManyParams): Node => ({
RawStmt: {
stmt: {
Expand All @@ -233,7 +248,7 @@ export const InsertMany = ({
limitOption: 'LIMIT_OPTION_DEFAULT'
}
},
onConflictClause: makeConflictClause(conflict, Object.keys(types)),
onConflictClause: makeConflictClause(conflict, Object.keys(types), conflictDoNothing),
override: 'OVERRIDING_NOT_SET'
}
},
Expand Down
5 changes: 5 additions & 0 deletions pgpm/core/src/export/export-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type FieldType = 'uuid' | 'uuid[]' | 'text' | 'text[]' | 'boolean' | 'image' | '
interface TableConfig {
schema: string;
table: string;
conflictDoNothing?: boolean;
fields: Record<string, FieldType>;
}

Expand Down Expand Up @@ -57,6 +58,10 @@ const config: Record<string, TableConfig> = {
field: {
schema: 'metaschema_public',
table: 'field',
// Use ON CONFLICT DO NOTHING to handle the unique constraint (databases_field_uniq_names_idx)
// which normalizes UUID field names by stripping suffixes like _id, _uuid, etc.
// This causes collisions when tables have both 'foo' (text) and 'foo_id' (uuid) columns.
conflictDoNothing: true,
fields: {
id: 'uuid',
database_id: 'uuid',
Expand Down