mirror of
https://github.com/knex/knex.git
synced 2025-12-27 06:58:39 +00:00
fix mssql alter column must have its own query (#4317)
This commit is contained in:
parent
371864d1d4
commit
7db2d18877
@ -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;
|
||||
|
||||
@ -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)'
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user