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

Commit 2909ec1e by Seshadri Mahalingam Committed by Sushant

Fixes QueryInterface#changeColumn for enums (#7455)

1 parent d4cfa840
# Future
- [FIXED] `changeColumn` generates incorrect query with ENUM type [#7455](https://github.com/sequelize/sequelize/pull/7455)
- [ADDED] `options.alter` to sequelize.sync() to alter existing tables.[#537](https://github.com/sequelize/sequelize/issues/537)
- [ADDED] Ability to run transactions on a read-replica by marking transactions as read only [#7323](https://github.com/sequelize/sequelize/issues/7323)
- [FIXED] Show a reasonable message when using renameColumn with a missing column [#6606](https://github.com/sequelize/sequelize/issues/6606)
......
......@@ -295,8 +295,8 @@ const QueryGenerator = {
if (attributes[attributeName].match(/^ENUM\(/)) {
attrSql += this.pgEnum(tableName, attributeName, attributes[attributeName]);
definition = definition.replace(/^ENUM\(.+\)/, this.quoteIdentifier('enum_' + tableName + '_' + attributeName));
definition += ' USING (' + this.quoteIdentifier(attributeName) + '::' + this.quoteIdentifier(definition) + ')';
definition = definition.replace(/^ENUM\(.+\)/, this.pgEnumName(tableName, attributeName, { schema: false }));
definition += ' USING (' + this.quoteIdentifier(attributeName) + '::' + this.pgEnumName(tableName, attributeName) + ')';
}
if (definition.match(/UNIQUE;*$/)) {
......
......@@ -305,6 +305,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() {
var self = this;
return self.sequelize.createSchema('hero').then(function() {
......@@ -472,33 +484,67 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
});
it('should change columns', function() {
return this.queryInterface.createTable({
tableName: 'users'
}, {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
currency: DataTypes.INTEGER
}).bind(this).then(function() {
return this.queryInterface.changeColumn('users', 'currency', {
type: DataTypes.FLOAT,
allowNull: true
});
}).then(function() {
return this.queryInterface.describeTable({
tableName: 'users'
});
}).then(function (table) {
if (dialect === 'postgres' || dialect === 'postgres-native') {
expect(table.currency.type).to.equal('DOUBLE PRECISION');
} else {
expect(table.currency.type).to.equal('FLOAT');
}
});
});
// 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'
}, {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
currency: DataTypes.INTEGER
firstName: DataTypes.STRING
}).bind(this).then(function() {
return this.queryInterface.changeColumn('users', 'currency', {
type: DataTypes.FLOAT,
allowNull: true
return this.queryInterface.changeColumn('users', 'firstName', {
type: DataTypes.ENUM(['value1', 'value2', 'value3'])
});
}).then(function() {
return this.queryInterface.describeTable({
tableName: 'users'
});
}).then(function (table) {
if (dialect === 'postgres' || dialect === 'postgres-native') {
expect(table.currency.type).to.equal('DOUBLE PRECISION');
} else {
expect(table.currency.type).to.equal('FLOAT');
}
});
});
});
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
if (dialect !== 'sqlite') {
......
......@@ -229,7 +229,7 @@ if (dialect.match(/^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;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");'
}
],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!