Skip to content

Commit

Permalink
fix: bug when default value in mssql were not updated if previous def…
Browse files Browse the repository at this point in the history
…ault was already set
  • Loading branch information
pleerock committed Feb 13, 2020
1 parent 87b161f commit 9fc8329
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/driver/sqlserver/SqlServerQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ export class SqlServerQueryRunner extends BaseQueryRunner implements QueryRunner
oldColumn.name = newColumn.name;
}

if (this.isColumnChanged(oldColumn, newColumn)) {
if (this.isColumnChanged(oldColumn, newColumn, false)) {
upQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} ALTER COLUMN ${this.buildCreateColumnSql(table, newColumn, true, false)}`));
downQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} ALTER COLUMN ${this.buildCreateColumnSql(table, oldColumn, true, false)}`));
}
Expand Down Expand Up @@ -940,16 +940,19 @@ export class SqlServerQueryRunner extends BaseQueryRunner implements QueryRunner
}

if (newColumn.default !== oldColumn.default) {
if (newColumn.default !== null && newColumn.default !== undefined) {
const defaultName = this.connection.namingStrategy.defaultConstraintName(table.name, newColumn.name);
upQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${defaultName}" DEFAULT ${newColumn.default} FOR "${newColumn.name}"`));
downQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${defaultName}"`));

} else if (oldColumn.default !== null && oldColumn.default !== undefined) {
// (note) if there is a previous default, we need to drop its constraint first
if (oldColumn.default !== null && oldColumn.default !== undefined) {
const defaultName = this.connection.namingStrategy.defaultConstraintName(table.name, oldColumn.name);
upQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${defaultName}"`));
downQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${defaultName}" DEFAULT ${oldColumn.default} FOR "${oldColumn.name}"`));
}

if (newColumn.default !== null && newColumn.default !== undefined) {
const defaultName = this.connection.namingStrategy.defaultConstraintName(table.name, newColumn.name);
upQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} ADD CONSTRAINT "${defaultName}" DEFAULT ${newColumn.default} FOR "${newColumn.name}"`));
downQueries.push(new Query(`ALTER TABLE ${this.escapePath(table)} DROP CONSTRAINT "${defaultName}"`));
}
}

await this.executeQueries(upQueries, downQueries);
Expand Down

0 comments on commit 9fc8329

Please sign in to comment.