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

Commit 446b3d01 by Harshith Kashyap Committed by Sushant

V3 Port - MSSQL removeColumn method fix for primaryKey column (#7082)

* Fixes removeColumn method when primarykey column is specified

* Review fixes

* Fixes failing test
1 parent 9cdd4d6b
# Future
- [FIXED] `removeColumn` method to support dropping primaryKey column (MSSQL) [#7081](https://github.com/sequelize/sequelize/pull/7081)
# 3.29.0
- [FIXED] Transaction Name too long, transaction savepoints for SQL Server [#6972](https://github.com/sequelize/sequelize/pull/6972)
......
......@@ -690,6 +690,26 @@ var QueryGenerator = {
return sql;
},
getPrimaryKeyConstraintQuery: function(table, attributeName) {
var 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: function(tableName, foreignKey) {
return Utils._.template('ALTER TABLE <%= table %> DROP <%= key %>')({
table: this.quoteTable(tableName),
......
......@@ -44,6 +44,18 @@ var removeColumn = function (tableName, attributeName, options) {
var dropForeignKeySql = self.QueryGenerator.dropForeignKeyQuery(tableName, results[0].constraint_name);
return self.sequelize.query(dropForeignKeySql , { raw: true, logging: options.logging});
})
.then(function() {
//Check if the current column is a primaryKey
var primaryKeyConstraintSql = self.QueryGenerator.getPrimaryKeyConstraintQuery(tableName, attributeName);
return self.sequelize.query(primaryKeyConstraintSql, { raw: true, logging: options.logging });
})
.spread(function(result) {
if (!result.length) {
return;
}
var dropConstraintSql = self.QueryGenerator.dropConstraintQuery(tableName, result[0].constraintName);
return self.sequelize.query(dropConstraintSql, { raw: true, logging: options.logging});
})
.then(function () {
var removeSql = self.QueryGenerator.removeColumnQuery(tableName, attributeName);
return self.sequelize.query(removeSql, { raw: true, logging: options.logging});
......
......@@ -28,7 +28,8 @@ var removeColumn = function (tableName, columnName, options) {
_.assign({ raw: true }, options)
)
.spread(function (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
return;
}
......
......@@ -604,6 +604,19 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
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() {
......@@ -658,6 +671,20 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
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') {
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!