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

Commit bb4315bc by Sushant

Fix(#966) : Added more test, fixed mysql,mssql,postgres changeColumn logic

Changed MySQL, MSSQL changeColumn dont overwrite query template for changeColumn
1 parent 1f1604d6
...@@ -178,15 +178,17 @@ var QueryGenerator = { ...@@ -178,15 +178,17 @@ var QueryGenerator = {
}, },
changeColumnQuery: function(tableName, attributes) { changeColumnQuery: function(tableName, attributes) {
var query = 'ALTER TABLE <%= tableName %> ALTER COLUMN <%= attributes %>;'; var query = 'ALTER TABLE <%= tableName %> <%= query %>;';
var attrString = []; var attrString = [], constraintString = [];
for (var attributeName in attributes) { for (var attributeName in attributes) {
var definition = attributes[attributeName]; var definition = attributes[attributeName];
if (definition.match(/REFERENCES/)) { if (definition.match(/REFERENCES/)) {
query = query.replace('ALTER COLUMN', ''); constraintString.push(Utils._.template('<%= fkName %> FOREIGN KEY (<%= attrName %>) <%= definition %>')({
definition = definition.replace(/.+?(?=REFERENCES)/,''); fkName: this.quoteIdentifier(attributeName + '_foreign_idx'),
attrString.push('ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_foreign_idx') + ' FOREIGN KEY (' + this.quoteIdentifier(attributeName) + ') ' + definition); attrName: this.quoteIdentifier(attributeName),
definition: definition.replace(/.+?(?=REFERENCES)/,'')
}));
} else { } else {
attrString.push(Utils._.template('<%= attrName %> <%= definition %>')({ attrString.push(Utils._.template('<%= attrName %> <%= definition %>')({
attrName: this.quoteIdentifier(attributeName), attrName: this.quoteIdentifier(attributeName),
...@@ -195,9 +197,18 @@ var QueryGenerator = { ...@@ -195,9 +197,18 @@ var QueryGenerator = {
} }
} }
var finalQuery = '';
if (attrString.length) {
finalQuery += 'ALTER COLUMN ' + attrString.join(', ');
finalQuery += constraintString.length ? ' ' : '';
}
if (constraintString.length) {
finalQuery += 'ADD CONSTRAINT ' + constraintString.join(', ');
}
return Utils._.template(query)({ return Utils._.template(query)({
tableName: this.quoteTable(tableName), tableName: this.quoteTable(tableName),
attributes: attrString.join(', ') query: finalQuery
}); });
}, },
......
...@@ -121,14 +121,17 @@ var QueryGenerator = { ...@@ -121,14 +121,17 @@ var QueryGenerator = {
}, },
changeColumnQuery: function(tableName, attributes) { changeColumnQuery: function(tableName, attributes) {
var query = 'ALTER TABLE <%= tableName %> CHANGE <%= attributes %>;'; var query = 'ALTER TABLE <%= tableName %> <%= query %>;';
var attrString = []; var attrString = [], constraintString = [];
for (var attributeName in attributes) { for (var attributeName in attributes) {
var definition = attributes[attributeName]; var definition = attributes[attributeName];
if (definition.match(/REFERENCES/)) { if (definition.match(/REFERENCES/)) {
query = query.replace('CHANGE', ''); constraintString.push(Utils._.template('<%= fkName %> FOREIGN KEY (<%= attrName %>) <%= definition %>')({
definition = definition.replace(/.+?(?=REFERENCES)/,''); fkName: this.quoteIdentifier(attributeName + '_foreign_idx'),
attrString.push('ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_foreign_idx') + ' FOREIGN KEY (' + this.quoteIdentifier(attributeName) + ') ' + definition); attrName: this.quoteIdentifier(attributeName),
definition: definition.replace(/.+?(?=REFERENCES)/,'')
}));
} else { } else {
attrString.push(Utils._.template('`<%= attrName %>` `<%= attrName %>` <%= definition %>')({ attrString.push(Utils._.template('`<%= attrName %>` `<%= attrName %>` <%= definition %>')({
attrName: attributeName, attrName: attributeName,
...@@ -136,7 +139,20 @@ var QueryGenerator = { ...@@ -136,7 +139,20 @@ var QueryGenerator = {
})); }));
} }
} }
return Utils._.template(query)({ tableName: this.quoteTable(tableName), attributes: attrString.join(', ') });
var finalQuery = '';
if (attrString.length) {
finalQuery += 'CHANGE ' + attrString.join(', ');
finalQuery += constraintString.length ? ' ' : '';
}
if (constraintString.length) {
finalQuery += 'ADD CONSTRAINT ' + constraintString.join(', ');
}
return Utils._.template(query)({
tableName: this.quoteTable(tableName),
query: finalQuery
});
}, },
renameColumnQuery: function(tableName, attrBefore, attributes) { renameColumnQuery: function(tableName, attrBefore, attributes) {
......
...@@ -221,7 +221,7 @@ var QueryGenerator = { ...@@ -221,7 +221,7 @@ var QueryGenerator = {
}); });
definition = definition.replace('NOT NULL', '').trim(); definition = definition.replace('NOT NULL', '').trim();
} else { } else if (!definition.match(/REFERENCES/)) {
attrSql += Utils._.template(query)({ attrSql += Utils._.template(query)({
tableName: this.quoteTable(tableName), tableName: this.quoteTable(tableName),
query: this.quoteIdentifier(attributeName) + ' DROP NOT NULL' query: this.quoteIdentifier(attributeName) + ' DROP NOT NULL'
...@@ -235,7 +235,7 @@ var QueryGenerator = { ...@@ -235,7 +235,7 @@ var QueryGenerator = {
}); });
definition = definition.replace(/(DEFAULT[^;]+)/, '').trim(); definition = definition.replace(/(DEFAULT[^;]+)/, '').trim();
} else { } else if (!definition.match(/REFERENCES/)) {
attrSql += Utils._.template(query)({ attrSql += Utils._.template(query)({
tableName: this.quoteTable(tableName), tableName: this.quoteTable(tableName),
query: this.quoteIdentifier(attributeName) + ' DROP DEFAULT' query: this.quoteIdentifier(attributeName) + ' DROP DEFAULT'
......
...@@ -411,6 +411,29 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() { ...@@ -411,6 +411,29 @@ 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
}, {
logging: log
}).then(function() {
expect(count).to.be.equal(1);
count = 0;
});
});
});
//SQlite navitely doesnt support ALTER Foreign key //SQlite navitely doesnt support ALTER Foreign key
if (dialect !== 'sqlite') { if (dialect !== 'sqlite') {
describe('should support foreign keys', function() { describe('should support foreign keys', function() {
......
...@@ -41,21 +41,33 @@ if (current.dialect.name !== 'sqlite') { ...@@ -41,21 +41,33 @@ if (current.dialect.name !== 'sqlite') {
it('properly generate alter queries', function(){ it('properly generate alter queries', function(){
return current.getQueryInterface().changeColumn(Model.getTableName(), 'level_id', { return current.getQueryInterface().changeColumn(Model.getTableName(), 'level_id', {
type: DataTypes.INTEGER, type: DataTypes.FLOAT,
references: { allowNull: false,
model: 'level', }).then(function(sql){
key: 'id' expectsql(sql, {
}, mssql: 'ALTER TABLE [users] ALTER COLUMN [level_id] FLOAT NOT NULL;',
onUpdate: 'cascade', mysql: 'ALTER TABLE `users` CHANGE `level_id` `level_id` FLOAT NOT NULL;',
onDelete: 'cascade' postgres: 'ALTER TABLE "users" ALTER COLUMN "level_id" SET NOT NULL;ALTER TABLE "users" ALTER COLUMN "level_id" DROP DEFAULT;ALTER TABLE "users" ALTER COLUMN "level_id" TYPE FLOAT;',
}).then(function(sql){ });
expectsql(sql, { });
mssql: 'ALTER TABLE [users] ADD CONSTRAINT [level_id_foreign_idx] FOREIGN KEY ([level_id]) REFERENCES [level] ([id]) ON DELETE CASCADE;', });
mysql: 'ALTER TABLE `users` ADD CONSTRAINT `level_id_foreign_idx` FOREIGN KEY (`level_id`) REFERENCES `level` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;',
//Postgres issue multiple queries when changing schema to drop default indexes it('properly generate alter queries for foreign keys', function(){
postgres: 'ALTER TABLE "users" ALTER COLUMN "level_id" DROP NOT NULL;ALTER TABLE "users" ALTER COLUMN "level_id" DROP DEFAULT;ALTER TABLE "users" ADD CONSTRAINT "level_id_foreign_idx" FOREIGN KEY ("level_id") REFERENCES "level" ("id") ON DELETE CASCADE ON UPDATE CASCADE;', return current.getQueryInterface().changeColumn(Model.getTableName(), 'level_id', {
}); type: DataTypes.INTEGER,
}); references: {
model: 'level',
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
}).then(function(sql){
expectsql(sql, {
mssql: 'ALTER TABLE [users] ADD CONSTRAINT [level_id_foreign_idx] FOREIGN KEY ([level_id]) REFERENCES [level] ([id]) ON DELETE CASCADE;',
mysql: 'ALTER TABLE `users` ADD CONSTRAINT `level_id_foreign_idx` FOREIGN KEY (`level_id`) REFERENCES `level` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;',
postgres: 'ALTER TABLE "users" ADD CONSTRAINT "level_id_foreign_idx" FOREIGN KEY ("level_id") REFERENCES "level" ("id") ON DELETE CASCADE ON UPDATE CASCADE;',
});
});
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!