Skip to content

Commit

Permalink
fix(postgres): sync with alter method fails with dataType enum (#15738)
Browse files Browse the repository at this point in the history
* fix(postgres): sync with alter method fails with dataType enum (#7649)

* Fixing issues after code review

* Fixing issues after code review
  • Loading branch information
aristov committed Mar 5, 2023
1 parent e06270f commit 6d9a58e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ export class PostgresQueryGenerator extends PostgresQueryGeneratorTypeScript {
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 (Boolean(options) && options.force === true) {
sql = this.pgEnumDrop(tableName, attr) + sql;
}
Expand Down
18 changes: 18 additions & 0 deletions packages/core/test/integration/model/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,24 @@ describe(getTestDialectTeaser('Model.sync & Sequelize#sync'), () => {
expect(results).to.have.length(1);
});
}

// TODO add support for db2 and mssql dialects
if (dialect !== 'db2' && dialect !== 'mssql') {
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 });
});
}
});

async function getNonPrimaryIndexes(model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,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 packages/core/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.getAttributes().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.getAttributes().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 6d9a58e

Please sign in to comment.