diff --git a/drizzle-kit/src/sqlgenerator.ts b/drizzle-kit/src/sqlgenerator.ts index a35c001fd..26adaf531 100644 --- a/drizzle-kit/src/sqlgenerator.ts +++ b/drizzle-kit/src/sqlgenerator.ts @@ -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]; @@ -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}";`; } } @@ -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 }); } } @@ -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; } } @@ -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; } } @@ -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!)})` : '' @@ -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}";`; } } @@ -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}\`;`; } } diff --git a/drizzle-kit/tests/indexes/pg.test.ts b/drizzle-kit/tests/indexes/pg.test.ts index b9ff36020..57f77c103 100644 --- a/drizzle-kit/tests/indexes/pg.test.ts +++ b/drizzle-kit/tests/indexes/pg.test.ts @@ -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);`, ); }, @@ -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;', ]); }, @@ -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);`, ); }, }; diff --git a/drizzle-kit/tests/libsql-statements.test.ts b/drizzle-kit/tests/libsql-statements.test.ts index a7cbc0602..636496c45 100644 --- a/drizzle-kit/tests/libsql-statements.test.ts +++ b/drizzle-kit/tests/libsql-statements.test.ts @@ -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;`, @@ -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;`, diff --git a/drizzle-kit/tests/pg-checks.test.ts b/drizzle-kit/tests/pg-checks.test.ts index 50a01a6c1..8033aacef 100644 --- a/drizzle-kit/tests/pg-checks.test.ts +++ b/drizzle-kit/tests/pg-checks.test.ts @@ -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) diff --git a/drizzle-kit/tests/pg-identity.test.ts b/drizzle-kit/tests/pg-identity.test.ts index 9f6ce8ba7..efb481da3 100644 --- a/drizzle-kit/tests/pg-identity.test.ts +++ b/drizzle-kit/tests/pg-identity.test.ts @@ -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', ]); }); @@ -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', ]); }); @@ -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', ]); }); diff --git a/drizzle-kit/tests/pg-tables.test.ts b/drizzle-kit/tests/pg-tables.test.ts index 6ea6e472a..4ca01f1fe 100644 --- a/drizzle-kit/tests/pg-tables.test.ts +++ b/drizzle-kit/tests/pg-tables.test.ts @@ -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`, ]); }); @@ -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); `, ); }); @@ -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"));`, ]); }); @@ -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', ]); }); @@ -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);', ]); }); @@ -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, @@ -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]); }); @@ -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, @@ -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]); }); diff --git a/drizzle-kit/tests/pg-views.test.ts b/drizzle-kit/tests/pg-views.test.ts index 002004c47..4f24cd776 100644 --- a/drizzle-kit/tests/pg-views.test.ts +++ b/drizzle-kit/tests/pg-views.test.ts @@ -45,7 +45,7 @@ test('create table and view #1', async () => { }); expect(sqlStatements.length).toBe(2); - expect(sqlStatements[0]).toBe(`CREATE TABLE IF NOT EXISTS "users" ( + expect(sqlStatements[0]).toBe(`CREATE TABLE "users" ( \t"id" integer PRIMARY KEY NOT NULL );\n`); expect(sqlStatements[1]).toBe(`CREATE VIEW "public"."some_view" AS (select "id" from "users");`); @@ -93,7 +93,7 @@ test('create table and view #2', async () => { }); expect(sqlStatements.length).toBe(2); - expect(sqlStatements[0]).toBe(`CREATE TABLE IF NOT EXISTS "users" ( + expect(sqlStatements[0]).toBe(`CREATE TABLE "users" ( \t"id" integer PRIMARY KEY NOT NULL );\n`); expect(sqlStatements[1]).toBe(`CREATE VIEW "public"."some_view" AS (SELECT * FROM "users");`); @@ -169,7 +169,7 @@ test('create table and view #3', async () => { }); expect(sqlStatements.length).toBe(3); - expect(sqlStatements[0]).toBe(`CREATE TABLE IF NOT EXISTS "users" ( + expect(sqlStatements[0]).toBe(`CREATE TABLE "users" ( \t"id" integer PRIMARY KEY NOT NULL );\n`); expect(sqlStatements[1]).toBe( @@ -258,7 +258,7 @@ test('create table and view #4', async () => { expect(sqlStatements.length).toBe(4); expect(sqlStatements[0]).toBe(`CREATE SCHEMA "new_schema";\n`); - expect(sqlStatements[1]).toBe(`CREATE TABLE IF NOT EXISTS "new_schema"."users" ( + expect(sqlStatements[1]).toBe(`CREATE TABLE "new_schema"."users" ( \t"id" integer PRIMARY KEY NOT NULL );\n`); expect(sqlStatements[2]).toBe( @@ -328,7 +328,7 @@ test('create table and view #6', async () => { }); expect(sqlStatements.length).toBe(2); - expect(sqlStatements[0]).toBe(`CREATE TABLE IF NOT EXISTS "users" ( + expect(sqlStatements[0]).toBe(`CREATE TABLE "users" ( \t"id" integer PRIMARY KEY NOT NULL );\n`); expect(sqlStatements[1]).toBe( @@ -398,7 +398,7 @@ test('create table and materialized view #1', async () => { }); expect(sqlStatements.length).toBe(2); - expect(sqlStatements[0]).toBe(`CREATE TABLE IF NOT EXISTS "users" ( + expect(sqlStatements[0]).toBe(`CREATE TABLE "users" ( \t"id" integer PRIMARY KEY NOT NULL );\n`); expect(sqlStatements[1]).toBe(`CREATE MATERIALIZED VIEW "public"."some_view" AS (select "id" from "users");`); @@ -446,7 +446,7 @@ test('create table and materialized view #2', async () => { }); expect(sqlStatements.length).toBe(2); - expect(sqlStatements[0]).toBe(`CREATE TABLE IF NOT EXISTS "users" ( + expect(sqlStatements[0]).toBe(`CREATE TABLE "users" ( \t"id" integer PRIMARY KEY NOT NULL );\n`); expect(sqlStatements[1]).toBe(`CREATE MATERIALIZED VIEW "public"."some_view" AS (SELECT * FROM "users");`); @@ -544,7 +544,7 @@ test('create table and materialized view #3', async () => { }); expect(sqlStatements.length).toBe(3); - expect(sqlStatements[0]).toBe(`CREATE TABLE IF NOT EXISTS "users" ( + expect(sqlStatements[0]).toBe(`CREATE TABLE "users" ( \t"id" integer PRIMARY KEY NOT NULL );\n`); expect(sqlStatements[1]).toBe( @@ -617,7 +617,7 @@ test('create table and materialized view #5', async () => { }); expect(sqlStatements.length).toBe(2); - expect(sqlStatements[0]).toBe(`CREATE TABLE IF NOT EXISTS "users" ( + expect(sqlStatements[0]).toBe(`CREATE TABLE "users" ( \t"id" integer PRIMARY KEY NOT NULL );\n`); expect(sqlStatements[1]).toBe( diff --git a/drizzle-kit/tests/push/libsql.test.ts b/drizzle-kit/tests/push/libsql.test.ts index 460809d9e..2ae2e3811 100644 --- a/drizzle-kit/tests/push/libsql.test.ts +++ b/drizzle-kit/tests/push/libsql.test.ts @@ -185,7 +185,7 @@ test('added, dropped index', async (t) => { expect(sqlStatements.length).toBe(2); expect(sqlStatements[0]).toBe( - `DROP INDEX IF EXISTS \`customers_address_unique\`;`, + `DROP INDEX \`customers_address_unique\`;`, ); expect(sqlStatements[1]).toBe( `CREATE UNIQUE INDEX \`customers_is_confirmed_unique\` ON \`customers\` (\`is_confirmed\`);`, @@ -963,7 +963,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;`, @@ -1035,10 +1035,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;`, diff --git a/drizzle-kit/tests/push/pg.test.ts b/drizzle-kit/tests/push/pg.test.ts index 44ec786b6..a7bed413d 100644 --- a/drizzle-kit/tests/push/pg.test.ts +++ b/drizzle-kit/tests/push/pg.test.ts @@ -318,10 +318,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);`, ); }, @@ -547,7 +547,7 @@ const pgSuite: DialectSuite = { }, ]); expect(sqlStatements).toStrictEqual([ - 'CREATE TABLE IF NOT EXISTS "users" (\n\t"id" integer,\n\t"id2" integer,\n\t"name" text,\n\t"gen_name" text GENERATED ALWAYS AS ("users"."name" || \'hello\') STORED\n);\n', + 'CREATE TABLE "users" (\n\t"id" integer,\n\t"id2" integer,\n\t"name" text,\n\t"gen_name" text GENERATED ALWAYS AS ("users"."name" || \'hello\') STORED\n);\n', ]); }, @@ -616,20 +616,20 @@ const pgSuite: DialectSuite = { const { statements, sqlStatements } = await diffTestSchemasPush(client, schema1, schema2, [], false, ['public']); expect(sqlStatements).toStrictEqual([ - 'DROP INDEX IF EXISTS "changeName";', - 'DROP INDEX IF EXISTS "addColumn";', - 'DROP INDEX IF EXISTS "changeExpression";', - 'DROP INDEX IF EXISTS "changeUsing";', - 'DROP INDEX IF EXISTS "changeWith";', - 'DROP INDEX IF EXISTS "removeColumn";', - 'DROP INDEX IF EXISTS "removeExpression";', - 'CREATE INDEX IF NOT EXISTS "newName" ON "users" USING btree ("name" DESC NULLS LAST,name) WITH (fillfactor=70);', - 'CREATE INDEX IF NOT EXISTS "addColumn" ON "users" USING btree ("name" DESC NULLS LAST,"id") WITH (fillfactor=70);', - 'CREATE INDEX IF NOT EXISTS "changeExpression" ON "users" USING btree ("id" DESC NULLS LAST,name desc);', - 'CREATE INDEX IF NOT EXISTS "changeUsing" ON "users" USING hash ("name");', - 'CREATE INDEX IF NOT EXISTS "changeWith" ON "users" USING btree ("name") WITH (fillfactor=90);', - 'CREATE INDEX IF NOT EXISTS "removeColumn" ON "users" USING btree ("name");', - 'CREATE INDEX CONCURRENTLY IF NOT EXISTS "removeExpression" ON "users" USING btree ("name" DESC NULLS LAST);', + 'DROP INDEX "changeName";', + 'DROP INDEX "addColumn";', + 'DROP INDEX "changeExpression";', + 'DROP INDEX "changeUsing";', + 'DROP INDEX "changeWith";', + 'DROP INDEX "removeColumn";', + 'DROP INDEX "removeExpression";', + 'CREATE INDEX "newName" ON "users" USING btree ("name" DESC NULLS LAST,name) WITH (fillfactor=70);', + 'CREATE INDEX "addColumn" ON "users" USING btree ("name" DESC NULLS LAST,"id") WITH (fillfactor=70);', + 'CREATE INDEX "changeExpression" ON "users" USING btree ("id" DESC NULLS LAST,name desc);', + 'CREATE INDEX "changeUsing" ON "users" USING hash ("name");', + 'CREATE INDEX "changeWith" ON "users" USING btree ("name") WITH (fillfactor=90);', + 'CREATE INDEX "removeColumn" ON "users" USING btree ("name");', + 'CREATE INDEX CONCURRENTLY "removeExpression" ON "users" USING btree ("name" DESC NULLS LAST);', ]); }, @@ -667,7 +667,7 @@ const pgSuite: DialectSuite = { }); expect(sqlStatements.length).toBe(1); - expect(sqlStatements[0]).toBe(`DROP INDEX IF EXISTS "users_name_id_index";`); + expect(sqlStatements[0]).toBe(`DROP INDEX "users_name_id_index";`); }, async indexesToBeNotTriggered() { @@ -958,7 +958,7 @@ const pgSuite: DialectSuite = { }, ]); expect(sqlStatements).toStrictEqual([ - 'CREATE TABLE IF NOT EXISTS "table" (\n\t"col1" integer NOT NULL,\n\t"col2" integer NOT NULL,\n\tCONSTRAINT "table_col1_col2_pk" PRIMARY KEY("col1","col2")\n);\n', + 'CREATE TABLE "table" (\n\t"col1" integer NOT NULL,\n\t"col2" integer NOT NULL,\n\tCONSTRAINT "table_col1_col2_pk" PRIMARY KEY("col1","col2")\n);\n', ]); }, @@ -1304,7 +1304,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\t"id1" bigint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id1_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),\n\t"id2" smallint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id2_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 32767 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\t"id1" bigint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id1_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1),\n\t"id2" smallint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id2_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 32767 START WITH 1 CACHE 1)\n);\n', ]); for (const st of sqlStatements) { @@ -1367,7 +1367,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 "users_id_seq" INCREMENT BY 4 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),\n\t"id1" bigint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id1_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 17000 START WITH 120 CACHE 1),\n\t"id2" smallint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id2_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 32767 START WITH 1 CACHE 1 CYCLE)\n);\n', + 'CREATE TABLE "users" (\n\t"id" integer GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id_seq" INCREMENT BY 4 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),\n\t"id1" bigint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id1_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 17000 START WITH 120 CACHE 1),\n\t"id2" smallint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id2_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 32767 START WITH 1 CACHE 1 CYCLE)\n);\n', ]); for (const st of sqlStatements) { @@ -1436,7 +1436,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 "users_id_seq" INCREMENT BY 4 MINVALUE 100 MAXVALUE 2147483647 START WITH 100 CACHE 1),\n\t"id1" bigint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id1_seq" INCREMENT BY 3 MINVALUE 1 MAXVALUE 17000 START WITH 120 CACHE 100 CYCLE),\n\t"id2" smallint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id2_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 32767 START WITH 1 CACHE 1 CYCLE)\n);\n', + 'CREATE TABLE "users" (\n\t"id" integer GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id_seq" INCREMENT BY 4 MINVALUE 100 MAXVALUE 2147483647 START WITH 100 CACHE 1),\n\t"id1" bigint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id1_seq" INCREMENT BY 3 MINVALUE 1 MAXVALUE 17000 START WITH 120 CACHE 100 CYCLE),\n\t"id2" smallint GENERATED BY DEFAULT AS IDENTITY (sequence name "users_id2_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 32767 START WITH 1 CACHE 1 CYCLE)\n);\n', ]); for (const st of sqlStatements) { @@ -2258,7 +2258,7 @@ test('Column with same name as enum', async () => { }, ]); expect(sqlStatements).toStrictEqual([ - 'CREATE TABLE IF NOT EXISTS "table2" (\n\t"id" serial PRIMARY KEY NOT NULL,\n\t"status" "status" DEFAULT \'inactive\'\n);\n', + 'CREATE TABLE "table2" (\n\t"id" serial PRIMARY KEY NOT NULL,\n\t"status" "status" DEFAULT \'inactive\'\n);\n', 'ALTER TABLE "table1" ADD COLUMN "status" "status" DEFAULT \'inactive\';', ]); }); @@ -3591,7 +3591,7 @@ test('create table with a policy', async (t) => { ); expect(sqlStatements).toStrictEqual([ - 'CREATE TABLE IF NOT EXISTS "users2" (\n\t"id" integer PRIMARY KEY NOT NULL\n);\n', + 'CREATE TABLE "users2" (\n\t"id" integer PRIMARY KEY NOT NULL\n);\n', 'ALTER TABLE "users2" ENABLE ROW LEVEL SECURITY;', 'CREATE POLICY "test" ON "users2" AS PERMISSIVE FOR ALL TO public;', ]); diff --git a/drizzle-kit/tests/push/sqlite.test.ts b/drizzle-kit/tests/push/sqlite.test.ts index dd1d88fe3..e2c85233a 100644 --- a/drizzle-kit/tests/push/sqlite.test.ts +++ b/drizzle-kit/tests/push/sqlite.test.ts @@ -185,7 +185,7 @@ test('dropped, added unique index', async (t) => { expect(sqlStatements.length).toBe(2); expect(sqlStatements[0]).toBe( - `DROP INDEX IF EXISTS \`customers_address_unique\`;`, + `DROP INDEX \`customers_address_unique\`;`, ); expect(sqlStatements[1]).toBe( `CREATE UNIQUE INDEX \`customers_is_confirmed_unique\` ON \`customers\` (\`is_confirmed\`);`, diff --git a/drizzle-kit/tests/rls/pg-policy.test.ts b/drizzle-kit/tests/rls/pg-policy.test.ts index b42385e3e..3d5dcbd14 100644 --- a/drizzle-kit/tests/rls/pg-policy.test.ts +++ b/drizzle-kit/tests/rls/pg-policy.test.ts @@ -587,7 +587,7 @@ test('create table with a policy', async (t) => { const { statements, sqlStatements } = await diffTestSchemas(schema1, schema2, []); expect(sqlStatements).toStrictEqual([ - 'CREATE TABLE IF NOT EXISTS "users2" (\n\t"id" integer PRIMARY KEY NOT NULL\n);\n', + 'CREATE TABLE "users2" (\n\t"id" integer PRIMARY KEY NOT NULL\n);\n', 'ALTER TABLE "users2" ENABLE ROW LEVEL SECURITY;', 'CREATE POLICY "test" ON "users2" AS PERMISSIVE FOR ALL TO public;', ]); @@ -720,12 +720,10 @@ test('create table with rls enabled', async (t) => { const { statements, sqlStatements } = await diffTestSchemas(schema1, schema2, []); expect(sqlStatements).toStrictEqual([ - `CREATE TABLE IF NOT EXISTS "users" (\n\t"id" integer PRIMARY KEY NOT NULL\n); + `CREATE TABLE "users" (\n\t"id" integer PRIMARY KEY NOT NULL\n); `, 'ALTER TABLE "users" ENABLE ROW LEVEL SECURITY;', ]); - - console.log(statements); }); test('enable rls force', async (t) => {