不要怂,就是干,撸起袖子干!

Commit 360d621d by Seshadri Mahalingam Committed by Sushant

V3 backport: Fixes QueryInterface#changeColumn for enums (#7456)

* Backport #7455

Fixes QueryInterface#changeColumn for enums

* Prevent test from running in MSSQL

* Adds a changelog entry
1 parent aa39f96e
# Future # Future
- [FIXED] Passing parameters to model getters [#7404](https://github.com/sequelize/sequelize/issues/7404) - [FIXED] Passing parameters to model getters [#7404](https://github.com/sequelize/sequelize/issues/7404)
- [FIXED] `changeColumn` generates incorrect query with ENUM type [#7456](https://github.com/sequelize/sequelize/pull/7456)
# 3.30.3 # 3.30.3
- [ADDED] Ability to run transactions on a read-replica by marking transactions as read only [#7323](https://github.com/sequelize/sequelize/issues/7323) - [ADDED] Ability to run transactions on a read-replica by marking transactions as read only [#7323](https://github.com/sequelize/sequelize/issues/7323)
......
...@@ -243,9 +243,9 @@ var QueryGenerator = { ...@@ -243,9 +243,9 @@ var QueryGenerator = {
} }
if (attributes[attributeName].match(/^ENUM\(/)) { if (attributes[attributeName].match(/^ENUM\(/)) {
query = this.pgEnum(tableName, attributeName, attributes[attributeName]) + query; attrSql += this.pgEnum(tableName, attributeName, attributes[attributeName]);
definition = definition.replace(/^ENUM\(.+\)/, this.quoteIdentifier('enum_' + tableName + '_' + attributeName)); definition = definition.replace(/^ENUM\(.+\)/, this.pgEnumName(tableName, attributeName, { schema: false }));
definition += ' USING (' + this.quoteIdentifier(attributeName) + '::' + this.quoteIdentifier(definition) + ')'; definition += ' USING (' + this.quoteIdentifier(attributeName) + '::' + this.pgEnumName(tableName, attributeName) + ')';
} }
if (definition.match(/UNIQUE;*$/)) { if (definition.match(/UNIQUE;*$/)) {
......
...@@ -253,6 +253,18 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() { ...@@ -253,6 +253,18 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
}); });
}); });
it('should work with enums (4)', function() {
return this.queryInterface.createSchema('archive').bind(this).then(function() {
return this.queryInterface.createTable('SomeTable', {
someEnum: {
type: DataTypes.ENUM,
values: ['value1', 'value2', 'value3'],
field: 'otherName'
}
}, { schema: 'archive' });
});
});
it('should work with schemas', function() { it('should work with schemas', function() {
var self = this; var self = this;
return self.sequelize.createSchema('hero').then(function() { return self.sequelize.createSchema('hero').then(function() {
...@@ -433,6 +445,40 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() { ...@@ -433,6 +445,40 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
} }
}); });
}); });
// MSSQL doesn't support using a modified column in a check constraint.
// https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql
if (dialect !== 'mssql') {
it('should work with enums', function() {
return this.queryInterface.createTable({
tableName: 'users'
}, {
firstName: DataTypes.STRING
}).bind(this).then(function() {
return this.queryInterface.changeColumn('users', 'firstName', {
type: DataTypes.ENUM(['value1', 'value2', 'value3'])
});
});
});
it('should work with enums with schemas', function() {
return this.sequelize.createSchema('archive').bind(this).then(function() {
return this.queryInterface.createTable({
tableName: 'users',
schema: 'archive'
}, {
firstName: DataTypes.STRING
});
}).bind(this).then(function() {
return this.queryInterface.changeColumn({
tableName: 'users',
schema: 'archive'
}, 'firstName', {
type: DataTypes.ENUM(['value1', 'value2', 'value3'])
});
});
});
}
}); });
//SQlite navitely doesnt support ALTER Foreign key //SQlite navitely doesnt support ALTER Foreign key
......
...@@ -250,6 +250,16 @@ if (dialect.match(/^postgres/)) { ...@@ -250,6 +250,16 @@ if (dialect.match(/^postgres/)) {
} }
], ],
changeColumnQuery: [
{
arguments: ['myTable', {
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");'
}
],
selectQuery: [ selectQuery: [
{ {
arguments: ['myTable'], arguments: ['myTable'],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!