Skip to content

Commit

Permalink
fix(postgres): make sync not fail when trying to create existing enum (
Browse files Browse the repository at this point in the history
  • Loading branch information
aristov committed Mar 7, 2023
1 parent d3f5b5a commit 1b94462
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
values = dataType.toString().match(/^ENUM\(.+\)/)[0];
}

let sql = `CREATE TYPE ${enumName} AS ${values};`;
let sql = `DO ${this.escape(`BEGIN CREATE TYPE ${enumName} AS ${values}; EXCEPTION WHEN duplicate_object THEN null; END`)};`;
if (!!options && options.force === true) {
sql = this.pgEnumDrop(tableName, attr) + sql;
}
Expand Down
18 changes: 18 additions & 0 deletions test/integration/model/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,24 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}
});
}

// TODO add support for db2 and mssql dialects
if (!['db2', 'mssql'].includes(dialect)) {
it('does not recreate existing enums (#7649)', async () => {
sequelize.define('Media', {
type: DataTypes.ENUM([
'video', 'audio'
])
});
await sequelize.sync({ alter: true });
sequelize.define('Media', {
type: DataTypes.ENUM([
'image', 'video', 'audio'
])
});
await sequelize.sync({ alter: true });
});
}
});

describe('indexes', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/dialects/postgres/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ if (dialect.startsWith('postgres')) {
col_1: "ENUM('value 1', 'value 2') NOT NULL",
col_2: "ENUM('value 3', 'value 4') NOT NULL"
}],
expectation: 'ALTER TABLE "myTable" ALTER COLUMN "col_1" SET NOT NULL;ALTER TABLE "myTable" ALTER COLUMN "col_1" DROP DEFAULT;CREATE TYPE "public"."enum_myTable_col_1" AS ENUM(\'value 1\', \'value 2\');ALTER TABLE "myTable" ALTER COLUMN "col_1" TYPE "public"."enum_myTable_col_1" USING ("col_1"::"public"."enum_myTable_col_1");ALTER TABLE "myTable" ALTER COLUMN "col_2" SET NOT NULL;ALTER TABLE "myTable" ALTER COLUMN "col_2" DROP DEFAULT;CREATE TYPE "public"."enum_myTable_col_2" AS ENUM(\'value 3\', \'value 4\');ALTER TABLE "myTable" ALTER COLUMN "col_2" TYPE "public"."enum_myTable_col_2" USING ("col_2"::"public"."enum_myTable_col_2");'
expectation: 'ALTER TABLE "myTable" ALTER COLUMN "col_1" SET NOT NULL;ALTER TABLE "myTable" ALTER COLUMN "col_1" DROP DEFAULT;DO \'BEGIN CREATE TYPE "public"."enum_myTable_col_1" AS ENUM(\'\'value 1\'\', \'\'value 2\'\'); EXCEPTION WHEN duplicate_object THEN null; END\';ALTER TABLE "myTable" ALTER COLUMN "col_1" TYPE "public"."enum_myTable_col_1" USING ("col_1"::"public"."enum_myTable_col_1");ALTER TABLE "myTable" ALTER COLUMN "col_2" SET NOT NULL;ALTER TABLE "myTable" ALTER COLUMN "col_2" DROP DEFAULT;DO \'BEGIN CREATE TYPE "public"."enum_myTable_col_2" AS ENUM(\'\'value 3\'\', \'\'value 4\'\'); EXCEPTION WHEN duplicate_object THEN null; END\';ALTER TABLE "myTable" ALTER COLUMN "col_2" TYPE "public"."enum_myTable_col_2" USING ("col_2"::"public"."enum_myTable_col_2");'
}
],

Expand Down
4 changes: 2 additions & 2 deletions test/unit/sql/enum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
describe('pgEnum', () => {
it('uses schema #3171', () => {
expectsql(sql.pgEnum(FooUser.getTableName(), 'mood', FooUser.rawAttributes.mood.type), {
postgres: 'CREATE TYPE "foo"."enum_users_mood" AS ENUM(\'happy\', \'sad\');'
postgres: 'DO \'BEGIN CREATE TYPE "foo"."enum_users_mood" AS ENUM(\'\'happy\'\', \'\'sad\'\'); EXCEPTION WHEN duplicate_object THEN null; END\';'
});
});

it('does add schema when public', () => {
expectsql(sql.pgEnum(PublicUser.getTableName(), 'theirMood', PublicUser.rawAttributes.mood.type), {
postgres: 'CREATE TYPE "public"."enum_users_theirMood" AS ENUM(\'happy\', \'sad\');'
postgres: 'DO \'BEGIN CREATE TYPE "public"."enum_users_theirMood" AS ENUM(\'\'happy\'\', \'\'sad\'\'); EXCEPTION WHEN duplicate_object THEN null; END\';'
});
});
});
Expand Down

0 comments on commit 1b94462

Please sign in to comment.