Skip to content

Commit

Permalink
fix mssql alter column must have its own query (#4317)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhensby committed Feb 25, 2021
1 parent 371864d commit 7db2d18
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
15 changes: 15 additions & 0 deletions lib/dialects/mssql/schema/mssql-tablecompiler.js
Expand Up @@ -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;
Expand Down
20 changes: 19 additions & 1 deletion test/unit/schema-builder/mssql.js
Expand Up @@ -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)'
);
});

Expand Down

0 comments on commit 7db2d18

Please sign in to comment.