fix mssql alter column must have its own query (#4317)

This commit is contained in:
Daniel Hensby 2021-02-25 17:34:22 +00:00 committed by GitHub
parent 371864d1d4
commit 7db2d18877
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View File

@ -52,6 +52,21 @@ class TableCompiler_MSSQL extends TableCompiler {
}
}
alterColumns(columns, colBuilder) {
// in SQL server only one column can be altered at a time
columns.sql.forEach((sql) => {
this.pushQuery({
sql:
(this.lowerCase ? 'alter table ' : 'ALTER TABLE ') +
this.tableName() +
' ' +
(this.lowerCase ? this.alterColumnPrefix.toLowerCase() : this.alterColumnPrefix) +
sql,
bindings: columns.bindings,
})
});
}
// Compiles column drop. Multiple columns need only one DROP clause (not one DROP per column) so core dropColumn doesn't work. #1348
dropColumn() {
const _this2 = this;

View File

@ -165,7 +165,25 @@ describe('MSSQL SchemaBuilder', function () {
'ALTER TABLE [users] ADD [bar] nvarchar(255)'
);
expect(tableSql[1].sql).to.equal(
'ALTER TABLE [users] alter column [foo] nvarchar(255)'
'ALTER TABLE [users] ALTER COLUMN [foo] nvarchar(255)'
);
});
it('should alter multiple columns over multiple queries', function () {
tableSql = client
.schemaBuilder()
.table('users', function () {
this.string('foo').alter();
this.string('bar').alter();
})
.toSQL();
equal(2, tableSql.length);
expect(tableSql[0].sql).to.equal(
'ALTER TABLE [users] ALTER COLUMN [foo] nvarchar(255)'
);
expect(tableSql[1].sql).to.equal(
'ALTER TABLE [users] ALTER COLUMN [bar] nvarchar(255)'
);
});