Skip to content

Commit

Permalink
fix: Change the way modifyColumn constructs its statements because th…
Browse files Browse the repository at this point in the history
…e previous version was not including all necessary modifications

Every alter statement will be a separate item in the list returned by the modifyColumn function. The statement to alter the default value of a column will combine both the DROP CONSTRAINT and ADD CONSTRAINT statements in one String item, separated by a semicolon. The statement to alter the type and nullability will be part of one String item since that syntax is allowed in SQL Server, but it seems like it's not allowed for adding and dropping constraints.
  • Loading branch information
joc-a committed Aug 9, 2023
1 parent 325c125 commit ecf3c6a
Showing 1 changed file with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,30 +251,52 @@ open class SQLServerDialect : VendorDialect(dialectName, SQLServerDataTypeProvid
return columnDefault !in nonAcceptableDefaults
}

override fun modifyColumn(column: Column<*>, columnDiff: ColumnDiff): List<String> =
super.modifyColumn(column, columnDiff).map { statement ->
if (columnDiff.defaults) {
val transaction = TransactionManager.current()
val tableName = transaction.identity(column.table)
val colName = transaction.identity(column)

val dropConstraint = "DROP CONSTRAINT IF EXISTS DF_${tableName}_$colName"

column.dbDefaultValue?.let {
buildString {
append(statement.substringBefore("MODIFY COLUMN") + dropConstraint)
override fun modifyColumn(column: Column<*>, columnDiff: ColumnDiff): List<String> {
val transaction = TransactionManager.current()
val tableName = transaction.identity(column.table)
val colName = transaction.identity(column)

val alterTablePart = "ALTER TABLE $tableName "

val statements = mutableListOf<String>()

statements.add(
buildString {
append(alterTablePart + "ALTER COLUMN $colName ${column.columnType.sqlType()}")

if (columnDiff.nullability) {
val defaultValue = column.dbDefaultValue
val isPKColumn = column.table.primaryKey?.columns?.contains(column) == true

if (column.columnType.nullable ||
(defaultValue != null && column.defaultValueFun == null && !currentDialect.isAllowedAsColumnDefault(defaultValue))) {
append(" NULL")
} else if (!isPKColumn) {
append(" NOT NULL")
}
}
})

if (columnDiff.defaults) {
val dropConstraint = "DROP CONSTRAINT IF EXISTS DF_${tableName}_$colName"

statements.add(
buildString {
column.dbDefaultValue?.let {
append(alterTablePart + dropConstraint)
append("; ")
append(
statement.substringBefore("MODIFY COLUMN") +
alterTablePart +
"ADD CONSTRAINT DF_${tableName}_$colName DEFAULT ${SQLServerDataTypeProvider.processForDefaultValue(it)} for $colName"
)
}
} ?: (statement.substringBefore("MODIFY COLUMN") + dropConstraint)
} else {
statement.replace("MODIFY COLUMN", "ALTER COLUMN")
}
} ?: append(alterTablePart + dropConstraint)
}
)
}

return statements
}

override fun createDatabase(name: String): String = "CREATE DATABASE ${name.inProperCase()}"

override fun dropDatabase(name: String) = "DROP DATABASE ${name.inProperCase()}"
Expand Down

0 comments on commit ecf3c6a

Please sign in to comment.