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 10, 2023
1 parent d454ff2 commit cad293d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,30 +251,56 @@ 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 alterTablePart = "ALTER TABLE ${transaction.identity(column.table)} "

val statements = mutableListOf<String>()

statements.add(
buildString {
append(alterTablePart + "ALTER COLUMN ${transaction.identity(column)} ${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 tableName = column.table.tableName
val columnName = column.name
val constraintName = "DF_${tableName}_$columnName"

val dropConstraint = "DROP CONSTRAINT IF EXISTS $constraintName"

statements.add(
buildString {
column.dbDefaultValue?.let {
append(alterTablePart + dropConstraint)
append("; ")
append(
statement.substringBefore("MODIFY COLUMN") +
"ADD CONSTRAINT DF_${tableName}_$colName DEFAULT ${SQLServerDataTypeProvider.processForDefaultValue(it)} for $colName"
alterTablePart +
"ADD CONSTRAINT $constraintName DEFAULT ${SQLServerDataTypeProvider.processForDefaultValue(it)} for ${transaction.identity(column)}"
)
}
} ?: (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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.jetbrains.exposed.sql.statements.BatchDataInconsistentException
import org.jetbrains.exposed.sql.statements.BatchInsertStatement
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.constraintNamePart
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.inProperCase
import org.jetbrains.exposed.sql.tests.shared.assertEqualCollections
Expand Down Expand Up @@ -246,20 +247,20 @@ class DefaultsTest : DatabaseTestsBase() {
val baseExpression = "CREATE TABLE " + addIfNotExistsIfSupported() +
"${"t".inProperCase()} (" +
"${"id".inProperCase()} ${currentDialectTest.dataTypeProvider.integerAutoincType()} PRIMARY KEY, " +
"${"s".inProperCase()} $varcharType DEFAULT 'test' NOT NULL, " +
"${"sn".inProperCase()} $varcharType DEFAULT 'testNullable' NULL, " +
"${"l".inProperCase()} ${currentDialectTest.dataTypeProvider.longType()} DEFAULT 42 NOT NULL, " +
"$q${"c".inProperCase()}$q CHAR DEFAULT 'X' NOT NULL, " +
"${"t1".inProperCase()} $dtType ${currentDT.itOrNull()}, " +
"${"t2".inProperCase()} $dtType ${nowExpression.itOrNull()}, " +
"${"t3".inProperCase()} $dtType ${dtLiteral.itOrNull()}, " +
"${"t4".inProperCase()} DATE ${dLiteral.itOrNull()}, " +
"${"t5".inProperCase()} $dtType ${tsLiteral.itOrNull()}, " +
"${"t6".inProperCase()} $dtType ${tsLiteral.itOrNull()}, " +
"${"t7".inProperCase()} $longType ${durLiteral.itOrNull()}, " +
"${"t8".inProperCase()} $longType ${durLiteral.itOrNull()}, " +
"${"t9".inProperCase()} $timeType ${tLiteral.itOrNull()}, " +
"${"t10".inProperCase()} $timeType ${tLiteral.itOrNull()}" +
"${"s".inProperCase()} $varcharType${testTable.s.constraintNamePart()} DEFAULT 'test' NOT NULL, " +
"${"sn".inProperCase()} $varcharType${testTable.sn.constraintNamePart()} DEFAULT 'testNullable' NULL, " +
"${"l".inProperCase()} ${currentDialectTest.dataTypeProvider.longType()}${testTable.l.constraintNamePart()} DEFAULT 42 NOT NULL, " +
"$q${"c".inProperCase()}$q CHAR${testTable.c.constraintNamePart()} DEFAULT 'X' NOT NULL, " +
"${"t1".inProperCase()} $dtType${testTable.t1.constraintNamePart()} ${currentDT.itOrNull()}, " +
"${"t2".inProperCase()} $dtType${testTable.t2.constraintNamePart()} ${nowExpression.itOrNull()}, " +
"${"t3".inProperCase()} $dtType${testTable.t3.constraintNamePart()} ${dtLiteral.itOrNull()}, " +
"${"t4".inProperCase()} DATE${testTable.t4.constraintNamePart()} ${dLiteral.itOrNull()}, " +
"${"t5".inProperCase()} $dtType${testTable.t5.constraintNamePart()} ${tsLiteral.itOrNull()}, " +
"${"t6".inProperCase()} $dtType${testTable.t6.constraintNamePart()} ${tsLiteral.itOrNull()}, " +
"${"t7".inProperCase()} $longType${testTable.t7.constraintNamePart()} ${durLiteral.itOrNull()}, " +
"${"t8".inProperCase()} $longType${testTable.t8.constraintNamePart()} ${durLiteral.itOrNull()}, " +
"${"t9".inProperCase()} $timeType${testTable.t9.constraintNamePart()} ${tLiteral.itOrNull()}, " +
"${"t10".inProperCase()} $timeType${testTable.t10.constraintNamePart()} ${tLiteral.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect || currentDialectTest.h2Mode == H2Dialect.H2CompatibilityMode.Oracle) {
Expand Down Expand Up @@ -422,8 +423,8 @@ class DefaultsTest : DatabaseTestsBase() {
val baseExpression = "CREATE TABLE " + addIfNotExistsIfSupported() +
"${"t".inProperCase()} (" +
"${"id".inProperCase()} ${currentDialectTest.dataTypeProvider.integerAutoincType()} PRIMARY KEY, " +
"${"t1".inProperCase()} $timestampWithTimeZoneType ${timestampWithTimeZoneLiteral.itOrNull()}, " +
"${"t2".inProperCase()} $timestampWithTimeZoneType ${timestampWithTimeZoneLiteral.itOrNull()}" +
"${"t1".inProperCase()} $timestampWithTimeZoneType${testTable.t1.constraintNamePart()} ${timestampWithTimeZoneLiteral.itOrNull()}, " +
"${"t2".inProperCase()} $timestampWithTimeZoneType${testTable.t2.constraintNamePart()} ${timestampWithTimeZoneLiteral.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.jetbrains.exposed.sql.jodatime.*
import org.jetbrains.exposed.sql.statements.BatchDataInconsistentException
import org.jetbrains.exposed.sql.statements.BatchInsertStatement
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.constraintNamePart
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.inProperCase
import org.jetbrains.exposed.sql.tests.shared.assertEqualCollections
Expand Down Expand Up @@ -172,25 +173,21 @@ class JodaTimeDefaultsTest : JodaTimeBaseTest() {
else -> "NULL"
}

fun constraintNamePart(columnName: String) = (currentDialect as? SQLServerDialect)?.let {
" CONSTRAINT DF_t_$columnName"
} ?: ""

withTables(listOf(TestDB.SQLITE), testTable) {
val dtType = currentDialectTest.dataTypeProvider.dateTimeType()
val varCharType = currentDialectTest.dataTypeProvider.varcharType(100)
val q = db.identifierManager.quoteString
val baseExpression = "CREATE TABLE " + addIfNotExistsIfSupported() +
"${"t".inProperCase()} (" +
"${"id".inProperCase()} ${currentDialectTest.dataTypeProvider.integerAutoincType()} PRIMARY KEY, " +
"${"s".inProperCase()} $varCharType${constraintNamePart("s")} DEFAULT 'test' NOT NULL, " +
"${"sn".inProperCase()} $varCharType${constraintNamePart("sn")} DEFAULT 'testNullable' NULL, " +
"${"l".inProperCase()} ${currentDialectTest.dataTypeProvider.longType()}${constraintNamePart("l")} DEFAULT 42 NOT NULL, " +
"$q${"c".inProperCase()}$q CHAR${constraintNamePart("c")} DEFAULT 'X' NOT NULL, " +
"${"t1".inProperCase()} $dtType${constraintNamePart("t1")} ${currentDT.itOrNull()}, " +
"${"t2".inProperCase()} $dtType${constraintNamePart("t2")} ${nowExpression.itOrNull()}, " +
"${"t3".inProperCase()} $dtType${constraintNamePart("t3")} ${dtLiteral.itOrNull()}, " +
"${"t4".inProperCase()} DATE${constraintNamePart("t4")} ${dtLiteral.itOrNull()}" +
"${"s".inProperCase()} $varCharType${testTable.s.constraintNamePart()} DEFAULT 'test' NOT NULL, " +
"${"sn".inProperCase()} $varCharType${testTable.sn.constraintNamePart()} DEFAULT 'testNullable' NULL, " +
"${"l".inProperCase()} ${currentDialectTest.dataTypeProvider.longType()}${testTable.l.constraintNamePart()} DEFAULT 42 NOT NULL, " +
"$q${"c".inProperCase()}$q CHAR${testTable.c.constraintNamePart()} DEFAULT 'X' NOT NULL, " +
"${"t1".inProperCase()} $dtType${testTable.t1.constraintNamePart()} ${currentDT.itOrNull()}, " +
"${"t2".inProperCase()} $dtType${testTable.t2.constraintNamePart()} ${nowExpression.itOrNull()}, " +
"${"t3".inProperCase()} $dtType${testTable.t3.constraintNamePart()} ${dtLiteral.itOrNull()}, " +
"${"t4".inProperCase()} DATE${testTable.t4.constraintNamePart()} ${dtLiteral.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect || currentDialectTest.h2Mode == H2Dialect.H2CompatibilityMode.Oracle) {
Expand Down Expand Up @@ -393,8 +390,8 @@ class JodaTimeDefaultsTest : JodaTimeBaseTest() {
val baseExpression = "CREATE TABLE " + addIfNotExistsIfSupported() +
"${"t".inProperCase()} (" +
"${"id".inProperCase()} ${currentDialectTest.dataTypeProvider.integerAutoincType()} PRIMARY KEY, " +
"${"t1".inProperCase()} $timestampWithTimeZoneType ${timestampWithTimeZoneLiteral.itOrNull()}, " +
"${"t2".inProperCase()} $timestampWithTimeZoneType ${timestampWithTimeZoneLiteral.itOrNull()}" +
"${"t1".inProperCase()} $timestampWithTimeZoneType${testTable.t1.constraintNamePart()} ${timestampWithTimeZoneLiteral.itOrNull()}, " +
"${"t2".inProperCase()} $timestampWithTimeZoneType${testTable.t2.constraintNamePart()} ${timestampWithTimeZoneLiteral.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.jetbrains.exposed.sql.statements.BatchDataInconsistentException
import org.jetbrains.exposed.sql.statements.BatchInsertStatement
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.constraintNamePart
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.inProperCase
import org.jetbrains.exposed.sql.tests.shared.Category.defaultExpression
Expand Down Expand Up @@ -249,20 +250,20 @@ class DefaultsTest : DatabaseTestsBase() {
val baseExpression = "CREATE TABLE " + addIfNotExistsIfSupported() +
"${"t".inProperCase()} (" +
"${"id".inProperCase()} ${currentDialectTest.dataTypeProvider.integerAutoincType()} PRIMARY KEY, " +
"${"s".inProperCase()} $varCharType DEFAULT 'test' NOT NULL, " +
"${"sn".inProperCase()} $varCharType DEFAULT 'testNullable' NULL, " +
"${"l".inProperCase()} ${currentDialectTest.dataTypeProvider.longType()} DEFAULT 42 NOT NULL, " +
"$q${"c".inProperCase()}$q CHAR DEFAULT 'X' NOT NULL, " +
"${"t1".inProperCase()} $dtType ${currentDT.itOrNull()}, " +
"${"t2".inProperCase()} $dtType ${nowExpression.itOrNull()}, " +
"${"t3".inProperCase()} $dtType ${dtLiteral.itOrNull()}, " +
"${"t4".inProperCase()} DATE ${dLiteral.itOrNull()}, " +
"${"t5".inProperCase()} $dtType ${tsLiteral.itOrNull()}, " +
"${"t6".inProperCase()} $dtType ${tsLiteral.itOrNull()}, " +
"${"t7".inProperCase()} $longType ${durLiteral.itOrNull()}, " +
"${"t8".inProperCase()} $longType ${durLiteral.itOrNull()}, " +
"${"t9".inProperCase()} $timeType ${tLiteral.itOrNull()}, " +
"${"t10".inProperCase()} $timeType ${tLiteral.itOrNull()}" +
"${"s".inProperCase()} $varCharType${testTable.s.constraintNamePart()} DEFAULT 'test' NOT NULL, " +
"${"sn".inProperCase()} $varCharType${testTable.sn.constraintNamePart()} DEFAULT 'testNullable' NULL, " +
"${"l".inProperCase()} ${currentDialectTest.dataTypeProvider.longType()}${testTable.l.constraintNamePart()} DEFAULT 42 NOT NULL, " +
"$q${"c".inProperCase()}$q CHAR${testTable.c.constraintNamePart()} DEFAULT 'X' NOT NULL, " +
"${"t1".inProperCase()} $dtType${testTable.t1.constraintNamePart()} ${currentDT.itOrNull()}, " +
"${"t2".inProperCase()} $dtType${testTable.t2.constraintNamePart()} ${nowExpression.itOrNull()}, " +
"${"t3".inProperCase()} $dtType${testTable.t3.constraintNamePart()} ${dtLiteral.itOrNull()}, " +
"${"t4".inProperCase()} DATE${testTable.t4.constraintNamePart()} ${dLiteral.itOrNull()}, " +
"${"t5".inProperCase()} $dtType${testTable.t5.constraintNamePart()} ${tsLiteral.itOrNull()}, " +
"${"t6".inProperCase()} $dtType${testTable.t6.constraintNamePart()} ${tsLiteral.itOrNull()}, " +
"${"t7".inProperCase()} $longType${testTable.t7.constraintNamePart()} ${durLiteral.itOrNull()}, " +
"${"t8".inProperCase()} $longType${testTable.t8.constraintNamePart()} ${durLiteral.itOrNull()}, " +
"${"t9".inProperCase()} $timeType${testTable.t9.constraintNamePart()} ${tLiteral.itOrNull()}, " +
"${"t10".inProperCase()} $timeType${testTable.t10.constraintNamePart()} ${tLiteral.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect || currentDialectTest.h2Mode == H2Dialect.H2CompatibilityMode.Oracle) {
Expand Down Expand Up @@ -431,8 +432,8 @@ class DefaultsTest : DatabaseTestsBase() {
val baseExpression = "CREATE TABLE " + addIfNotExistsIfSupported() +
"${"t".inProperCase()} (" +
"${"id".inProperCase()} ${currentDialectTest.dataTypeProvider.integerAutoincType()} PRIMARY KEY, " +
"${"t1".inProperCase()} $timestampWithTimeZoneType ${timestampWithTimeZoneLiteral.itOrNull()}, " +
"${"t2".inProperCase()} $timestampWithTimeZoneType ${timestampWithTimeZoneLiteral.itOrNull()}" +
"${"t1".inProperCase()} $timestampWithTimeZoneType${testTable.t1.constraintNamePart()} ${timestampWithTimeZoneLiteral.itOrNull()}, " +
"${"t2".inProperCase()} $timestampWithTimeZoneType${testTable.t2.constraintNamePart()} ${timestampWithTimeZoneLiteral.itOrNull()}" +
")"

val expected = if (currentDialectTest is OracleDialect ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.jetbrains.exposed.sql.tests

import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.DatabaseDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import java.util.EnumSet

fun String.inProperCase(): String = TransactionManager.currentOrNull()?.db?.identifierManager?.inProperCase(this) ?: this
Expand All @@ -15,3 +17,7 @@ val currentDialectIfAvailableTest: DatabaseDialect? get() =

inline fun <reified E : Enum<E>> enumSetOf(vararg elements: E): EnumSet<E> =
elements.toCollection(EnumSet.noneOf(E::class.java))

fun <T> Column<T>.constraintNamePart() = (currentDialectTest as? SQLServerDialect)?.let {
" CONSTRAINT DF_${table.tableName}_$name"
} ?: ""
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() {
assertEquals(" ", whiteSpaceTable.select { whiteSpaceTable.id eq whiteSpaceId }.single()[whiteSpaceTable.column])

val actual = SchemaUtils.statementsRequiredToActualizeScheme(emptyTable)
assertEquals(1, actual.size)
val expected = if (testDb == TestDB.SQLSERVER) 2 else 1
assertEquals(expected, actual.size)

// Oracle treat '' as NULL column and can't alter from NULL to NULL
if (testDb != TestDB.ORACLE) {
Expand Down

0 comments on commit cad293d

Please sign in to comment.