Skip to content

Commit

Permalink
Drizzle kit/remove-non-strict-postgres (#3403)
Browse files Browse the repository at this point in the history
* removed non-strict creation in postgres

* Fix new tests

---------

Co-authored-by: AndriiSherman <[email protected]>
  • Loading branch information
AleksandrSherman and AndriiSherman authored Dec 5, 2024
1 parent 3f3eb73 commit a44af76
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 120 deletions.
29 changes: 10 additions & 19 deletions drizzle-kit/src/sqlgenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ class PgCreateTableConvertor extends Convertor {
let statement = '';
const name = schema ? `"${schema}"."${tableName}"` : `"${tableName}"`;

statement += `CREATE TABLE IF NOT EXISTS ${name} (\n`;
statement += `CREATE TABLE ${name} (\n`;
for (let i = 0; i < columns.length; i++) {
const column = columns[i];

Expand Down Expand Up @@ -1672,7 +1672,7 @@ class PgAlterTableDropColumnConvertor extends Convertor {
? `"${schema}"."${tableName}"`
: `"${tableName}"`;

return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN IF EXISTS "${columnName}";`;
return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN "${columnName}";`;
}
}

Expand Down Expand Up @@ -2330,7 +2330,7 @@ export class LibSQLModifyColumn extends Convertor {
for (const table of Object.values(json2.tables)) {
for (const index of Object.values(table.indexes)) {
const unsquashed = SQLiteSquasher.unsquashIdx(index);
sqlStatements.push(`DROP INDEX IF EXISTS "${unsquashed.name}";`);
sqlStatements.push(`DROP INDEX "${unsquashed.name}";`);
indexes.push({ ...unsquashed, tableName: table.name });
}
}
Expand Down Expand Up @@ -3283,14 +3283,9 @@ class PgCreateForeignKeyConvertor extends Convertor {
: `"${tableTo}"`;

const alterStatement =
`ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${name}" FOREIGN KEY (${fromColumnsString}) REFERENCES ${tableToNameWithSchema}(${toColumnsString})${onDeleteStatement}${onUpdateStatement}`;
`ALTER TABLE ${tableNameWithSchema} ADD CONSTRAINT "${name}" FOREIGN KEY (${fromColumnsString}) REFERENCES ${tableToNameWithSchema}(${toColumnsString})${onDeleteStatement}${onUpdateStatement};`;

let sql = 'DO $$ BEGIN\n';
sql += ' ' + alterStatement + ';\n';
sql += 'EXCEPTION\n';
sql += ' WHEN duplicate_object THEN null;\n';
sql += 'END $$;\n';
return sql;
return alterStatement;
}
}

Expand Down Expand Up @@ -3387,13 +3382,9 @@ class PgAlterForeignKeyConvertor extends Convertor {
: `"${newFk.tableFrom}"`;

const alterStatement =
`ALTER TABLE ${tableFromNameWithSchema} ADD CONSTRAINT "${newFk.name}" FOREIGN KEY (${fromColumnsString}) REFERENCES ${tableToNameWithSchema}(${toColumnsString})${onDeleteStatement}${onUpdateStatement}`;
`ALTER TABLE ${tableFromNameWithSchema} ADD CONSTRAINT "${newFk.name}" FOREIGN KEY (${fromColumnsString}) REFERENCES ${tableToNameWithSchema}(${toColumnsString})${onDeleteStatement}${onUpdateStatement};`;

sql += 'DO $$ BEGIN\n';
sql += ' ' + alterStatement + ';\n';
sql += 'EXCEPTION\n';
sql += ' WHEN duplicate_object THEN null;\n';
sql += 'END $$;\n';
sql += alterStatement;
return sql;
}
}
Expand Down Expand Up @@ -3474,7 +3465,7 @@ class CreatePgIndexConvertor extends Convertor {

return `CREATE ${indexPart}${
concurrently ? ' CONCURRENTLY' : ''
} IF NOT EXISTS "${name}" ON ${tableNameWithSchema} USING ${method} (${value})${
} "${name}" ON ${tableNameWithSchema} USING ${method} (${value})${
Object.keys(withMap!).length !== 0
? ` WITH (${reverseLogic(withMap!)})`
: ''
Expand Down Expand Up @@ -3567,7 +3558,7 @@ class PgDropIndexConvertor extends Convertor {

convert(statement: JsonDropIndexStatement): string {
const { name } = PgSquasher.unsquashIdx(statement.data);
return `DROP INDEX IF EXISTS "${name}";`;
return `DROP INDEX "${name}";`;
}
}

Expand Down Expand Up @@ -3663,7 +3654,7 @@ export class SqliteDropIndexConvertor extends Convertor {

convert(statement: JsonDropIndexStatement): string {
const { name } = PgSquasher.unsquashIdx(statement.data);
return `DROP INDEX IF EXISTS \`${name}\`;`;
return `DROP INDEX \`${name}\`;`;
}
}

Expand Down
24 changes: 12 additions & 12 deletions drizzle-kit/tests/indexes/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const pgSuite: DialectSuite = {
});
expect(sqlStatements.length).toBe(1);
expect(sqlStatements[0]).toBe(
`CREATE INDEX IF NOT EXISTS "vector_embedding_idx" ON "users" USING hnsw ("name" vector_ip_ops) WITH (m=16,ef_construction=64);`,
`CREATE INDEX "vector_embedding_idx" ON "users" USING hnsw ("name" vector_ip_ops) WITH (m=16,ef_construction=64);`,
);
},

Expand Down Expand Up @@ -123,15 +123,15 @@ const pgSuite: DialectSuite = {
);

expect(sqlStatements).toStrictEqual([
'DROP INDEX IF EXISTS "indx";',
'DROP INDEX IF EXISTS "indx1";',
'DROP INDEX IF EXISTS "indx2";',
'DROP INDEX IF EXISTS "indx3";',
'CREATE INDEX IF NOT EXISTS "indx4" ON "users" USING btree (lower(id)) WHERE true;',
'CREATE INDEX IF NOT EXISTS "indx" ON "users" USING btree ("name" DESC NULLS LAST);',
'CREATE INDEX IF NOT EXISTS "indx1" ON "users" USING btree ("name" DESC NULLS LAST) WHERE false;',
'CREATE INDEX IF NOT EXISTS "indx2" ON "users" USING btree ("name" test) WHERE true;',
'CREATE INDEX IF NOT EXISTS "indx3" ON "users" USING btree (lower("id")) WHERE true;',
'DROP INDEX "indx";',
'DROP INDEX "indx1";',
'DROP INDEX "indx2";',
'DROP INDEX "indx3";',
'CREATE INDEX "indx4" ON "users" USING btree (lower(id)) WHERE true;',
'CREATE INDEX "indx" ON "users" USING btree ("name" DESC NULLS LAST);',
'CREATE INDEX "indx1" ON "users" USING btree ("name" DESC NULLS LAST) WHERE false;',
'CREATE INDEX "indx2" ON "users" USING btree ("name" test) WHERE true;',
'CREATE INDEX "indx3" ON "users" USING btree (lower("id")) WHERE true;',
]);
},

Expand Down Expand Up @@ -234,10 +234,10 @@ const pgSuite: DialectSuite = {
});
expect(sqlStatements.length).toBe(2);
expect(sqlStatements[0]).toBe(
`CREATE INDEX IF NOT EXISTS "users_name_id_index" ON "users" USING btree ("name" DESC NULLS LAST,"id") WITH (fillfactor=70) WHERE select 1;`,
`CREATE INDEX "users_name_id_index" ON "users" USING btree ("name" DESC NULLS LAST,"id") WITH (fillfactor=70) WHERE select 1;`,
);
expect(sqlStatements[1]).toBe(
`CREATE INDEX IF NOT EXISTS "indx1" ON "users" USING hash ("name" DESC NULLS LAST,"name") WITH (fillfactor=70);`,
`CREATE INDEX "indx1" ON "users" USING hash ("name" DESC NULLS LAST,"name") WITH (fillfactor=70);`,
);
},
};
Expand Down
6 changes: 3 additions & 3 deletions drizzle-kit/tests/libsql-statements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ test('set not null with index', async (t) => {

expect(sqlStatements.length).toBe(3);
expect(sqlStatements[0]).toBe(
`DROP INDEX IF EXISTS "users_name_index";`,
`DROP INDEX "users_name_index";`,
);
expect(sqlStatements[1]).toBe(
`ALTER TABLE \`users\` ALTER COLUMN "name" TO "name" text NOT NULL;`,
Expand Down Expand Up @@ -972,10 +972,10 @@ test('drop not null with two indexes', async (t) => {

expect(sqlStatements.length).toBe(5);
expect(sqlStatements[0]).toBe(
`DROP INDEX IF EXISTS "users_name_unique";`,
`DROP INDEX "users_name_unique";`,
);
expect(sqlStatements[1]).toBe(
`DROP INDEX IF EXISTS "users_age_index";`,
`DROP INDEX "users_age_index";`,
);
expect(sqlStatements[2]).toBe(
`ALTER TABLE \`users\` ALTER COLUMN "name" TO "name" text;`,
Expand Down
2 changes: 1 addition & 1 deletion drizzle-kit/tests/pg-checks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test('create table with check', async (t) => {
} as JsonCreateTableStatement);

expect(sqlStatements.length).toBe(1);
expect(sqlStatements[0]).toBe(`CREATE TABLE IF NOT EXISTS "users" (
expect(sqlStatements[0]).toBe(`CREATE TABLE "users" (
\t"id" serial PRIMARY KEY NOT NULL,
\t"age" integer,
\tCONSTRAINT "some_check_name" CHECK ("users"."age" > 21)
Expand Down
6 changes: 3 additions & 3 deletions drizzle-kit/tests/pg-identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test('create table: identity always/by default - no params', async () => {
},
]);
expect(sqlStatements).toStrictEqual([
'CREATE TABLE IF NOT EXISTS "users" (\n\t"id" integer GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1)\n);\n',
'CREATE TABLE "users" (\n\t"id" integer GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1)\n);\n',
]);
});

Expand Down Expand Up @@ -95,7 +95,7 @@ test('create table: identity always/by default - few params', async () => {
},
]);
expect(sqlStatements).toStrictEqual([
'CREATE TABLE IF NOT EXISTS "users" (\n\t"id" integer GENERATED BY DEFAULT AS IDENTITY (sequence name "custom_seq" INCREMENT BY 4 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1)\n);\n',
'CREATE TABLE "users" (\n\t"id" integer GENERATED BY DEFAULT AS IDENTITY (sequence name "custom_seq" INCREMENT BY 4 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1)\n);\n',
]);
});

Expand Down Expand Up @@ -140,7 +140,7 @@ test('create table: identity always/by default - all params', async () => {
},
]);
expect(sqlStatements).toStrictEqual([
'CREATE TABLE IF NOT EXISTS "users" (\n\t"id" integer GENERATED BY DEFAULT AS IDENTITY (sequence name "custom_seq" INCREMENT BY 4 MINVALUE 3 MAXVALUE 1000 START WITH 3 CACHE 200)\n);\n',
'CREATE TABLE "users" (\n\t"id" integer GENERATED BY DEFAULT AS IDENTITY (sequence name "custom_seq" INCREMENT BY 4 MINVALUE 3 MAXVALUE 1000 START WITH 3 CACHE 200)\n);\n',
]);
});

Expand Down
64 changes: 24 additions & 40 deletions drizzle-kit/tests/pg-tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ test('add table #8: geometry types', async () => {
expect(statements.length).toBe(1);

expect(sqlStatements).toStrictEqual([
`CREATE TABLE IF NOT EXISTS "users" (\n\t"geom" geometry(point) NOT NULL,\n\t"geom1" geometry(point) NOT NULL\n);\n`,
`CREATE TABLE "users" (\n\t"geom" geometry(point) NOT NULL,\n\t"geom1" geometry(point) NOT NULL\n);\n`,
]);
});

Expand Down Expand Up @@ -360,7 +360,7 @@ test('add table #8: column with pgvector', async () => {
const { sqlStatements } = await diffTestSchemas(from, to, []);

expect(sqlStatements[0]).toBe(
`CREATE TABLE IF NOT EXISTS "users2" (\n\t"id" serial PRIMARY KEY NOT NULL,\n\t"name" vector(3)\n);
`CREATE TABLE "users2" (\n\t"id" serial PRIMARY KEY NOT NULL,\n\t"name" vector(3)\n);
`,
);
});
Expand Down Expand Up @@ -671,8 +671,8 @@ test('create table with tsvector', async () => {
const { statements, sqlStatements } = await diffTestSchemas(from, to, []);

expect(sqlStatements).toStrictEqual([
'CREATE TABLE IF NOT EXISTS "posts" (\n\t"id" serial PRIMARY KEY NOT NULL,\n\t"title" text NOT NULL,\n\t"description" text NOT NULL\n);\n',
`CREATE INDEX IF NOT EXISTS "title_search_index" ON "posts" USING gin (to_tsvector('english', "title"));`,
'CREATE TABLE "posts" (\n\t"id" serial PRIMARY KEY NOT NULL,\n\t"title" text NOT NULL,\n\t"description" text NOT NULL\n);\n',
`CREATE INDEX "title_search_index" ON "posts" USING gin (to_tsvector('english', "title"));`,
]);
});

Expand All @@ -693,7 +693,7 @@ test('composite primary key', async () => {
const { sqlStatements } = await diffTestSchemas(from, to, []);

expect(sqlStatements).toStrictEqual([
'CREATE TABLE IF NOT EXISTS "works_to_creators" (\n\t"work_id" integer NOT NULL,\n\t"creator_id" integer NOT NULL,\n\t"classification" text NOT NULL,\n\tCONSTRAINT "works_to_creators_work_id_creator_id_classification_pk" PRIMARY KEY("work_id","creator_id","classification")\n);\n',
'CREATE TABLE "works_to_creators" (\n\t"work_id" integer NOT NULL,\n\t"creator_id" integer NOT NULL,\n\t"classification" text NOT NULL,\n\tCONSTRAINT "works_to_creators_work_id_creator_id_classification_pk" PRIMARY KEY("work_id","creator_id","classification")\n);\n',
]);
});

Expand Down Expand Up @@ -772,7 +772,7 @@ test('add index with op', async () => {
const { sqlStatements } = await diffTestSchemas(from, to, []);

expect(sqlStatements).toStrictEqual([
'CREATE INDEX IF NOT EXISTS "users_name_index" ON "users" USING gin ("name" gin_trgm_ops);',
'CREATE INDEX "users_name_index" ON "users" USING gin ("name" gin_trgm_ops);',
]);
});

Expand Down Expand Up @@ -829,7 +829,7 @@ test('optional db aliases (snake case)', async () => {

const { sqlStatements } = await diffTestSchemas(from, to, [], false, 'snake_case');

const st1 = `CREATE TABLE IF NOT EXISTS "t1" (
const st1 = `CREATE TABLE "t1" (
"t1_id1" integer PRIMARY KEY NOT NULL,
"t1_col2" integer NOT NULL,
"t1_col3" integer NOT NULL,
Expand All @@ -841,35 +841,27 @@ test('optional db aliases (snake case)', async () => {
);
`;

const st2 = `CREATE TABLE IF NOT EXISTS "t2" (
const st2 = `CREATE TABLE "t2" (
"t2_id" serial PRIMARY KEY NOT NULL
);
`;

const st3 = `CREATE TABLE IF NOT EXISTS "t3" (
const st3 = `CREATE TABLE "t3" (
"t3_id1" integer,
"t3_id2" integer,
CONSTRAINT "t3_t3_id1_t3_id2_pk" PRIMARY KEY("t3_id1","t3_id2")
);
`;

const st4 = `DO $$ BEGIN
ALTER TABLE "t1" ADD CONSTRAINT "t1_t2_ref_t2_t2_id_fk" FOREIGN KEY ("t2_ref") REFERENCES "public"."t2"("t2_id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
`;
const st4 =
`ALTER TABLE "t1" ADD CONSTRAINT "t1_t2_ref_t2_t2_id_fk" FOREIGN KEY ("t2_ref") REFERENCES "public"."t2"("t2_id") ON DELETE no action ON UPDATE no action;`;

const st5 = `DO $$ BEGIN
ALTER TABLE "t1" ADD CONSTRAINT "t1_t1_col2_t1_col3_t3_t3_id1_t3_id2_fk" FOREIGN KEY ("t1_col2","t1_col3") REFERENCES "public"."t3"("t3_id1","t3_id2") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
`;
const st5 =
`ALTER TABLE "t1" ADD CONSTRAINT "t1_t1_col2_t1_col3_t3_t3_id1_t3_id2_fk" FOREIGN KEY ("t1_col2","t1_col3") REFERENCES "public"."t3"("t3_id1","t3_id2") ON DELETE no action ON UPDATE no action;`;

const st6 = `CREATE UNIQUE INDEX IF NOT EXISTS "t1_uni_idx" ON "t1" USING btree ("t1_uni_idx");`;
const st6 = `CREATE UNIQUE INDEX "t1_uni_idx" ON "t1" USING btree ("t1_uni_idx");`;

const st7 = `CREATE INDEX IF NOT EXISTS "t1_idx" ON "t1" USING btree ("t1_idx") WHERE "t1"."t1_idx" > 0;`;
const st7 = `CREATE INDEX "t1_idx" ON "t1" USING btree ("t1_idx") WHERE "t1"."t1_idx" > 0;`;

expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6, st7]);
});
Expand Down Expand Up @@ -927,7 +919,7 @@ test('optional db aliases (camel case)', async () => {

const { sqlStatements } = await diffTestSchemas(from, to, [], false, 'camelCase');

const st1 = `CREATE TABLE IF NOT EXISTS "t1" (
const st1 = `CREATE TABLE "t1" (
"t1Id1" integer PRIMARY KEY NOT NULL,
"t1Col2" integer NOT NULL,
"t1Col3" integer NOT NULL,
Expand All @@ -939,35 +931,27 @@ test('optional db aliases (camel case)', async () => {
);
`;

const st2 = `CREATE TABLE IF NOT EXISTS "t2" (
const st2 = `CREATE TABLE "t2" (
"t2Id" serial PRIMARY KEY NOT NULL
);
`;

const st3 = `CREATE TABLE IF NOT EXISTS "t3" (
const st3 = `CREATE TABLE "t3" (
"t3Id1" integer,
"t3Id2" integer,
CONSTRAINT "t3_t3Id1_t3Id2_pk" PRIMARY KEY("t3Id1","t3Id2")
);
`;

const st4 = `DO $$ BEGIN
ALTER TABLE "t1" ADD CONSTRAINT "t1_t2Ref_t2_t2Id_fk" FOREIGN KEY ("t2Ref") REFERENCES "public"."t2"("t2Id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
`;
const st4 =
`ALTER TABLE "t1" ADD CONSTRAINT "t1_t2Ref_t2_t2Id_fk" FOREIGN KEY ("t2Ref") REFERENCES "public"."t2"("t2Id") ON DELETE no action ON UPDATE no action;`;

const st5 = `DO $$ BEGIN
ALTER TABLE "t1" ADD CONSTRAINT "t1_t1Col2_t1Col3_t3_t3Id1_t3Id2_fk" FOREIGN KEY ("t1Col2","t1Col3") REFERENCES "public"."t3"("t3Id1","t3Id2") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
`;
const st5 =
`ALTER TABLE "t1" ADD CONSTRAINT "t1_t1Col2_t1Col3_t3_t3Id1_t3Id2_fk" FOREIGN KEY ("t1Col2","t1Col3") REFERENCES "public"."t3"("t3Id1","t3Id2") ON DELETE no action ON UPDATE no action;`;

const st6 = `CREATE UNIQUE INDEX IF NOT EXISTS "t1UniIdx" ON "t1" USING btree ("t1UniIdx");`;
const st6 = `CREATE UNIQUE INDEX "t1UniIdx" ON "t1" USING btree ("t1UniIdx");`;

const st7 = `CREATE INDEX IF NOT EXISTS "t1Idx" ON "t1" USING btree ("t1Idx") WHERE "t1"."t1Idx" > 0;`;
const st7 = `CREATE INDEX "t1Idx" ON "t1" USING btree ("t1Idx") WHERE "t1"."t1Idx" > 0;`;

expect(sqlStatements).toStrictEqual([st1, st2, st3, st4, st5, st6, st7]);
});
Loading

0 comments on commit a44af76

Please sign in to comment.