Skip to content

Commit

Permalink
fix: EXPOSED-173 UPDATE_RULE read incorrectly for Oracle
Browse files Browse the repository at this point in the history
Oracle does not support UPDATE_RULE and returns NULL for it, but getInt returns 0 instead of NULL, which represents CASCADE.
  • Loading branch information
joc-a committed Sep 21, 2023
1 parent 29ed38c commit f22296b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ class JdbcDatabaseMetadataImpl(database: String, val metadata: DatabaseMetaData)
val targetColumn = allTables[targetTableName]?.columns?.firstOrNull {
identifierManager.quoteIdentifierWhenWrongCaseOrNecessary(it.nameInDatabaseCase()) == targetColumnName
} ?: return@iterate null // Do not crash if there are missing fields in Exposed's tables
val constraintUpdateRule = ReferenceOption.resolveRefOptionFromJdbc(getInt("UPDATE_RULE"))
val constraintUpdateRule = getObject("UPDATE_RULE")?.toString()?.toIntOrNull()?.let {
ReferenceOption.resolveRefOptionFromJdbc(it)
}
val constraintDeleteRule = ReferenceOption.resolveRefOptionFromJdbc(getInt("DELETE_RULE"))
ForeignKeyConstraint(
target = targetColumn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
import org.jetbrains.exposed.sql.tests.shared.Category
import org.jetbrains.exposed.sql.tests.shared.DEFAULT_CATEGORY_ID
import org.jetbrains.exposed.sql.tests.shared.Item
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.expectException
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.junit.Assume
import org.junit.Test

Expand Down Expand Up @@ -184,4 +186,66 @@ class ForeignKeyConstraintTests : DatabaseTestsBase() {
}
}
}

@Test
fun testUpdateRuleReadCorrectlyWhenNotSpecifiedInChildTable() {
val category = object : Table("Category") {
val id = integer("id")

override val primaryKey = PrimaryKey(id)
}

val item = object : Table("Item") {
val id = integer("id")
val categoryId = integer("categoryId").references(category.id)

override val primaryKey = PrimaryKey(id)
}

withTables(category, item) { testDb ->
if (currentDialect.supportsOnUpdate) {
val constraints = connection.metadata {
tableConstraints(listOf(item))
}
constraints.values.forEach { list ->
list.forEach {
when (testDb) {
TestDB.H2_ORACLE, TestDB.H2_SQLSERVER ->
assertEquals(ReferenceOption.RESTRICT, it.updateRule)
else -> assertEquals(currentDialectTest.defaultReferenceOption, it.updateRule)
}
}
}
}
}
}

@Test
fun testUpdateRuleReadCorrectlyWhenSpecifiedInChildTable() {
val category = object : Table("Category") {
val id = integer("id")

override val primaryKey = PrimaryKey(id)
}

val item = object : Table("Item") {
val id = integer("id")
val categoryId = integer("categoryId").references(category.id, onUpdate = ReferenceOption.CASCADE)

override val primaryKey = PrimaryKey(id)
}

withTables(category, item) {
if (currentDialectTest.supportsOnUpdate) {
val constraints = connection.metadata {
tableConstraints(listOf(item))
}
constraints.values.forEach { list ->
list.forEach { constraint ->
assertEquals(ReferenceOption.CASCADE, constraint.updateRule)
}
}
}
}
}
}

0 comments on commit f22296b

Please sign in to comment.