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

Commit 1f1604d6 by Sushant

fix(#966) : Fixed dialects to properly generate ADD CONSTRAINT when using changeColumn

1 parent b98af0bd
# Future # Future
- [FIXED] Migrations failed to add foreign key [#966](https://github.com/sequelize/sequelize/issues/966)
- [FIXED] Prevent race condition after transaction finished [#5222](https://github.com/sequelize/sequelize/issues/5222) - [FIXED] Prevent race condition after transaction finished [#5222](https://github.com/sequelize/sequelize/issues/5222)
- [FIXED] Fixed Instance.reload issues ([#4844](https://github.com/sequelize/sequelize/issues/4844) and [#4452](https://github.com/sequelize/sequelize/issues/4452)) - [FIXED] Fixed Instance.reload issues ([#4844](https://github.com/sequelize/sequelize/issues/4844) and [#4452](https://github.com/sequelize/sequelize/issues/4452))
- [FIXED] Fix upsert when primary key contains `.field` (internal API change for `queryInterface.upsert`) [#4755](https://github.com/sequelize/sequelize/issues/4755) - [FIXED] Fix upsert when primary key contains `.field` (internal API change for `queryInterface.upsert`) [#4755](https://github.com/sequelize/sequelize/issues/4755)
......
...@@ -181,14 +181,19 @@ var QueryGenerator = { ...@@ -181,14 +181,19 @@ var QueryGenerator = {
var query = 'ALTER TABLE <%= tableName %> ALTER COLUMN <%= attributes %>;'; var query = 'ALTER TABLE <%= tableName %> ALTER COLUMN <%= attributes %>;';
var attrString = []; var attrString = [];
for (var attrName in attributes) { for (var attributeName in attributes) {
var definition = attributes[attrName]; var definition = attributes[attributeName];
if (definition.match(/REFERENCES/)) {
query = query.replace('ALTER COLUMN', '');
definition = definition.replace(/.+?(?=REFERENCES)/,'');
attrString.push('ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_foreign_idx') + ' FOREIGN KEY (' + this.quoteIdentifier(attributeName) + ') ' + definition);
} else {
attrString.push(Utils._.template('<%= attrName %> <%= definition %>')({ attrString.push(Utils._.template('<%= attrName %> <%= definition %>')({
attrName: this.quoteIdentifier(attrName), attrName: this.quoteIdentifier(attributeName),
definition: definition definition: definition
})); }));
} }
}
return Utils._.template(query)({ return Utils._.template(query)({
tableName: this.quoteTable(tableName), tableName: this.quoteTable(tableName),
......
...@@ -123,16 +123,19 @@ var QueryGenerator = { ...@@ -123,16 +123,19 @@ var QueryGenerator = {
changeColumnQuery: function(tableName, attributes) { changeColumnQuery: function(tableName, attributes) {
var query = 'ALTER TABLE <%= tableName %> CHANGE <%= attributes %>;'; var query = 'ALTER TABLE <%= tableName %> CHANGE <%= attributes %>;';
var attrString = []; var attrString = [];
for (var attributeName in attributes) {
for (var attrName in attributes) { var definition = attributes[attributeName];
var definition = attributes[attrName]; if (definition.match(/REFERENCES/)) {
query = query.replace('CHANGE', '');
definition = definition.replace(/.+?(?=REFERENCES)/,'');
attrString.push('ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_foreign_idx') + ' FOREIGN KEY (' + this.quoteIdentifier(attributeName) + ') ' + definition);
} else {
attrString.push(Utils._.template('`<%= attrName %>` `<%= attrName %>` <%= definition %>')({ attrString.push(Utils._.template('`<%= attrName %>` `<%= attrName %>` <%= definition %>')({
attrName: attrName, attrName: attributeName,
definition: definition definition: definition
})); }));
} }
}
return Utils._.template(query)({ tableName: this.quoteTable(tableName), attributes: attrString.join(', ') }); return Utils._.template(query)({ tableName: this.quoteTable(tableName), attributes: attrString.join(', ') });
}, },
......
...@@ -257,10 +257,18 @@ var QueryGenerator = { ...@@ -257,10 +257,18 @@ var QueryGenerator = {
}); });
} }
if (definition.match(/REFERENCES/)) {
definition = definition.replace(/.+?(?=REFERENCES)/,'');
attrSql += Utils._.template(query.replace('ALTER COLUMN', ''))({
tableName: this.quoteTable(tableName),
query: 'ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_foreign_idx') + ' FOREIGN KEY (' + this.quoteIdentifier(attributeName) + ') ' + definition
});
} else {
attrSql += Utils._.template(query)({ attrSql += Utils._.template(query)({
tableName: this.quoteTable(tableName), tableName: this.quoteTable(tableName),
query: this.quoteIdentifier(attributeName) + ' TYPE ' + definition query: this.quoteIdentifier(attributeName) + ' TYPE ' + definition
}); });
}
sql.push(attrSql); sql.push(attrSql);
} }
......
...@@ -411,26 +411,33 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() { ...@@ -411,26 +411,33 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
}); });
}); });
it('should be able to change to foreign key reference', function() { //SQlite navitely doesnt support ALTER Foreign key
return this.queryInterface.createTable('level', { if (dialect !== 'sqlite') {
describe('should support foreign keys', function() {
beforeEach(function() {
return this.queryInterface.createTable('users', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
autoIncrement: true autoIncrement: true
},
level_id: {
type: DataTypes.INTEGER,
allowNull: false
} }
}).bind(this).then(function() { })
this.queryInterface.createTable('users', { .bind(this).then(function() {
return this.queryInterface.createTable('level', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
autoIncrement: true autoIncrement: true
},
level_id: {
type: DataTypes.INTEGER
} }
}); });
}) });
.bind(this).then(function() { });
it('able to change column to foreign key', function() {
return this.queryInterface.changeColumn('users', 'level_id', { return this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: { references: {
...@@ -438,15 +445,16 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() { ...@@ -438,15 +445,16 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
key: 'id' key: 'id'
}, },
onUpdate: 'cascade', onUpdate: 'cascade',
onDelete: 'set null' onDelete: 'cascade'
}, {logging: log}); }, {logging: log}).then(function() {
}).then(function() {
expect(count).to.be.equal(1); expect(count).to.be.equal(1);
count = 0; count = 0;
}); });
}); });
}); });
}
});
describe('addColumn', function() { describe('addColumn', function() {
beforeEach(function() { beforeEach(function() {
......
'use strict';
/* jshint -W030, -W110 */
var Support = require(__dirname + '/../support')
, DataTypes = require('../../../lib/data-types')
, expectsql = Support.expectsql
, sinon = require('sinon')
, current = Support.sequelize
, Promise = current.Promise;
if (current.dialect.name !== 'sqlite') {
describe(Support.getTestDialectTeaser('SQL'), function() {
describe('changeColumn', function () {
var Model = current.define('users', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
level_id: {
type: DataTypes.INTEGER
}
}, { timestamps: false });
before(function () {
this.stub = sinon.stub(current, 'query', function (sql) {
return Promise.resolve(sql);
});
});
beforeEach(function () {
this.stub.reset();
});
after(function () {
this.stub.restore();
});
it('properly generate alter queries', function(){
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 issue multiple queries when changing schema to drop default indexes
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;',
});
});
});
});
});
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!