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

Commit 37e84022 by Harshith Kashyap Committed by Sushant

MSSQL removeColumn method fix for primaryKey column (#7081)

* Added getPrimaryKeyConstraint method to drop primarykey constraint before dropping primarykey column

* Added changelog entry, exclude primaryKey constraint for mysql removeColumn method

* Review fixes

* Fixes failing test
1 parent 0b28d6a0
# Future # Future
- [FIXED] `removeColumn` method to support dropping primaryKey column (MSSQL) [#7081](https://github.com/sequelize/sequelize/pull/7081)
- [ADDED] Filtered Indexes support for SQL Server [#7016](https://github.com/sequelize/sequelize/issues/7016) - [ADDED] Filtered Indexes support for SQL Server [#7016](https://github.com/sequelize/sequelize/issues/7016)
- [FIXED] Set `timestamps` and `paranoid` options from through model on `belongsToMany` association - [FIXED] Set `timestamps` and `paranoid` options from through model on `belongsToMany` association
- [FIXED] Properly apply paranoid condition when `groupedLimit.on` association is `paranoid` - [FIXED] Properly apply paranoid condition when `groupedLimit.on` association is `paranoid`
......
...@@ -680,6 +680,24 @@ var QueryGenerator = { ...@@ -680,6 +680,24 @@ var QueryGenerator = {
return sql; return sql;
}, },
getPrimaryKeyConstraintQuery(table, attributeName) {
const tableName = wrapSingleQuote(table.tableName || table);
return [
'SELECT K.TABLE_NAME AS tableName,',
'K.COLUMN_NAME AS columnName,',
'K.CONSTRAINT_NAME AS constraintName',
'FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS C',
'JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K',
'ON C.TABLE_NAME = K.TABLE_NAME',
'AND C.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG',
'AND C.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA',
'AND C.CONSTRAINT_NAME = K.CONSTRAINT_NAME',
'WHERE C.CONSTRAINT_TYPE = \'PRIMARY KEY\'',
`AND K.COLUMN_NAME = ${wrapSingleQuote(attributeName)}`,
`AND K.TABLE_NAME = ${tableName};`
].join(' ');
},
dropForeignKeyQuery(tableName, foreignKey) { dropForeignKeyQuery(tableName, foreignKey) {
return Utils._.template('ALTER TABLE <%= table %> DROP <%= key %>')({ return Utils._.template('ALTER TABLE <%= table %> DROP <%= key %>')({
table: this.quoteTable(tableName), table: this.quoteTable(tableName),
......
...@@ -46,6 +46,18 @@ var removeColumn = function(tableName, attributeName, options) { ...@@ -46,6 +46,18 @@ var removeColumn = function(tableName, attributeName, options) {
var dropForeignKeySql = self.QueryGenerator.dropForeignKeyQuery(tableName, results[0].constraint_name); var dropForeignKeySql = self.QueryGenerator.dropForeignKeyQuery(tableName, results[0].constraint_name);
return self.sequelize.query(dropForeignKeySql, { raw: true, logging: options.logging}); return self.sequelize.query(dropForeignKeySql, { raw: true, logging: options.logging});
}) })
.then(() => {
//Check if the current column is a primaryKey
const primaryKeyConstraintSql = self.QueryGenerator.getPrimaryKeyConstraintQuery(tableName, attributeName);
return self.sequelize.query(primaryKeyConstraintSql, { raw: true, logging: options.logging });
})
.spread(result => {
if (!result.length) {
return;
}
const dropConstraintSql = self.QueryGenerator.dropConstraintQuery(tableName, result[0].constraintName);
return self.sequelize.query(dropConstraintSql, { raw: true, logging: options.logging});
})
.then(function() { .then(function() {
var removeSql = self.QueryGenerator.removeColumnQuery(tableName, attributeName); var removeSql = self.QueryGenerator.removeColumnQuery(tableName, attributeName);
return self.sequelize.query(removeSql, { raw: true, logging: options.logging}); return self.sequelize.query(removeSql, { raw: true, logging: options.logging});
......
...@@ -30,7 +30,8 @@ function removeColumn(tableName, columnName, options) { ...@@ -30,7 +30,8 @@ function removeColumn(tableName, columnName, options) {
_.assign({ raw: true }, options) _.assign({ raw: true }, options)
) )
.spread(results => { .spread(results => {
if (!results.length) { //Exclude primary key constraint
if (!results.length || results[0].constraint_name === 'PRIMARY') {
// No foreign key constraints found, so we can remove the column // No foreign key constraints found, so we can remove the column
return; return;
} }
......
...@@ -638,6 +638,20 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() { ...@@ -638,6 +638,20 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
expect(table).to.not.have.property('manager'); expect(table).to.not.have.property('manager');
}); });
}); });
it('should be able to remove a column with primaryKey', function () {
return this.queryInterface.removeColumn('users', 'manager').bind(this).then(function() {
return this.queryInterface.describeTable('users');
}).then(function(table) {
expect(table).to.not.have.property('manager');
return this.queryInterface.removeColumn('users', 'id');
}).then(function() {
return this.queryInterface.describeTable('users');
}).then(function(table) {
expect(table).to.not.have.property('id');
});
});
}); });
describe('(with a schema)', function() { describe('(with a schema)', function() {
...@@ -692,6 +706,20 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() { ...@@ -692,6 +706,20 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
expect(table).to.not.have.property('lastName'); expect(table).to.not.have.property('lastName');
}); });
}); });
it('should be able to remove a column with primaryKey', function () {
return this.queryInterface.removeColumn({
tableName: 'users',
schema: 'archive'
}, 'id').bind(this).then(function() {
return this.queryInterface.describeTable({
tableName: 'users',
schema: 'archive'
});
}).then(function(table) {
expect(table).to.not.have.property('id');
});
});
}); });
}); });
......
...@@ -99,5 +99,11 @@ if (current.dialect.name === 'mssql') { ...@@ -99,5 +99,11 @@ if (current.dialect.name === 'mssql') {
mssql: "SELECT TOP 100 PERCENT id, name FROM (SELECT TOP 10 * FROM (SELECT ROW_NUMBER() OVER (ORDER BY [id]) as row_num, * FROM myTable AS myOtherName) AS myOtherName WHERE row_num > 10) AS myOtherName" mssql: "SELECT TOP 100 PERCENT id, name FROM (SELECT TOP 10 * FROM (SELECT ROW_NUMBER() OVER (ORDER BY [id]) as row_num, * FROM myTable AS myOtherName) AS myOtherName WHERE row_num > 10) AS myOtherName"
}); });
}); });
test('getPrimaryKeyConstraintQuery', function () {
expectsql(QueryGenerator.getPrimaryKeyConstraintQuery('myTable', 'myColumnName'), {
mssql: 'SELECT K.TABLE_NAME AS tableName, K.COLUMN_NAME AS columnName, K.CONSTRAINT_NAME AS constraintName FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS C JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS K ON C.TABLE_NAME = K.TABLE_NAME AND C.CONSTRAINT_CATALOG = K.CONSTRAINT_CATALOG AND C.CONSTRAINT_SCHEMA = K.CONSTRAINT_SCHEMA AND C.CONSTRAINT_NAME = K.CONSTRAINT_NAME WHERE C.CONSTRAINT_TYPE = \'PRIMARY KEY\' AND K.COLUMN_NAME = \'myColumnName\' AND K.TABLE_NAME = \'myTable\';'
});
});
}); });
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!