Skip to content

Commit

Permalink
Correct comparison of defaults for String type columns in PostgreSQL (#…
Browse files Browse the repository at this point in the history
…1589) / Fix Oracle tests
  • Loading branch information
Tapac committed Nov 14, 2022
1 parent 8cad9f1 commit b1b6cf7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ object SchemaUtils {
.mapValues { (col, existingCol) ->
val columnType = col.columnType
val incorrectNullability = existingCol.nullable != columnType.nullable
val incorrectAutoInc = existingCol.autoIncrement != columnType.isAutoInc
// Exposed doesn't support changing sequences on columns
val incorrectAutoInc = existingCol.autoIncrement != columnType.isAutoInc && col.autoIncColumnType?.autoincSeq == null
val incorrectDefaults =
existingCol.defaultDbValue != col.dbDefaultValue?.let { dataTypeProvider.dbDefaultToString(col, it) }
val incorrectCaseSensitiveName = existingCol.name.inProperCase() != col.nameInDatabaseCase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,24 @@ class JdbcPreparedStatementImpl(val statement: PreparedStatement, val wasGenerat
}

override fun setNull(index: Int, columnType: IColumnType) {
if (columnType is BinaryColumnType || columnType is BlobColumnType)
if (columnType is BinaryColumnType || columnType is BlobColumnType) {
statement.setNull(index, Types.LONGVARBINARY)
else
} else {
statement.setObject(index, null)
}
}

override fun setInputStream(index: Int, inputStream: InputStream) {
statement.setBinaryStream(index, inputStream, inputStream.available())
}

override fun closeIfPossible() {
if (!statement.isClosed)
statement.close()
if (!statement.isClosed) statement.close()
}

override fun executeBatch(): List<Int> = statement.executeBatch().toList()

override fun cancel() {
if (!statement.isClosed)
statement.cancel()
if (!statement.isClosed) statement.cancel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.junit.Test
import java.math.BigDecimal
import java.util.*
import kotlin.properties.Delegates

class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() {

Expand Down Expand Up @@ -257,14 +258,24 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() {

@Test
fun `columns with default values that haven't changed shouldn't trigger change`() {
val table = object : Table("varchar_test") {
val varchar = varchar("varchar_column", 255).default("")
val text = text("text_column").default("")
}

// MySQL doesn't support default values on text columns, hence excluded
withDb(excludeSettings = listOf(TestDB.MYSQL)) {
var table by Delegates.notNull<Table>()
withDb { testDb ->
try {
// MySQL doesn't support default values on text columns, hence excluded
table = if(testDb != TestDB.MYSQL) {
object : Table("varchar_test") {
val varchar = varchar("varchar_column", 255).default(" ")
val text = text("text_column").default(" ")
}
} else {

object : Table("varchar_test") {
val varchar = varchar("varchar_column", 255).default(" ")
}
}

// MySQL doesn't support default values on text columns, hence excluded

SchemaUtils.create(table)
val actual = SchemaUtils.statementsRequiredToActualizeScheme(table)
assertEqualLists(emptyList(), actual)
Expand Down Expand Up @@ -310,16 +321,11 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() {
assertEquals(" ", whiteSpaceTable.select { whiteSpaceTable.id eq whiteSpaceId }.single()[whiteSpaceTable.column])

val actual = SchemaUtils.statementsRequiredToActualizeScheme(emptyTable)
// Both columns should be considered as changed, since "" != " "
val expected = when (testDb) {
TestDB.ORACLE, TestDB.H2_ORACLE -> 2
else -> 1
}

assertEquals(expected, actual.size)
assertEquals(1, actual.size)

// SQL Server requires drop/create constraint to change defaults, unsupported for now
if (testDb != TestDB.SQLSERVER) {
// Oracle treat '' as NULL column and can't alter from NULL to NULL
if (testDb !in listOf(TestDB.SQLSERVER, TestDB.ORACLE)) {
// Apply changes
actual.forEach { exec(it) }
} else {
Expand Down

0 comments on commit b1b6cf7

Please sign in to comment.