Skip to content

Commit

Permalink
chore: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
joc-a committed May 30, 2023
1 parent becf053 commit 818ece7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,17 @@ data class ForeignKeyConstraint(
}
append("FOREIGN KEY ($fromColumns) REFERENCES $targetTableName($targetColumns)")
if (deleteRule != ReferenceOption.NO_ACTION) {
append(" ON DELETE $deleteRule")
if (deleteRule == ReferenceOption.SET_DEFAULT && (currentDialect as? MysqlDialect)?.isMysql8 == false) {
exposedLogger.warn("This MySQL version doesn't support FOREIGN KEY with SET DEFAULT reference option with ON DELETE clause. Please check your $fromTableName table.")
} else {
append(" ON DELETE $deleteRule")
}
}
if (updateRule != ReferenceOption.NO_ACTION) {
if (currentDialect is OracleDialect || currentDialect.h2Mode == H2Dialect.H2CompatibilityMode.Oracle) {
exposedLogger.warn("Oracle doesn't support FOREIGN KEY with ON UPDATE clause. Please check your $fromTableName table.")
} else if (deleteRule == ReferenceOption.SET_DEFAULT && (currentDialect as? MysqlDialect)?.isMysql8 == false) {
exposedLogger.warn("This MySQL version doesn't support FOREIGN KEY with SET DEFAULT reference option with ON UPDATE clause. Please check your $fromTableName table.")
} else {
append(" ON UPDATE $updateRule")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.vendors.SQLiteDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.junit.Test
import java.math.BigDecimal
import java.util.*
import kotlin.test.assertFails

Expand Down Expand Up @@ -572,6 +573,50 @@ class CreateTableTests : DatabaseTestsBase() {
}
}

@Test
fun testOnDeleteSetDefault() {
val category = object : Table("Category") {
val id = integer("id")
val name = varchar(name = "name", length = 20)

override val primaryKey = PrimaryKey(id)
}

val defaultCategoryId = 0

val item = object : Table("Item") {
val id = integer("id")
val name = varchar(name = "name", length = 20)
val categoryId = integer("categoryId")
.default(defaultCategoryId)
.references(
category.id,
onUpdate = ReferenceOption.SET_DEFAULT,
onDelete = ReferenceOption.SET_DEFAULT
)

override val primaryKey = PrimaryKey(id)
}

withTables(category, item) { testDb ->
val isSetDefaultSupported = testDb != TestDB.MYSQL || this.db.isVersionCovers(BigDecimal("8.0"))
val onDeleteSetDefaultPart = if (isSetDefaultSupported) " ON DELETE SET DEFAULT" else ""
val onUpdateSetDefaultPart = if (isSetDefaultSupported && testDb !in listOf(TestDB.ORACLE, TestDB.H2_ORACLE)) " ON UPDATE SET DEFAULT" else ""

val expected = listOf(
"CREATE TABLE " + addIfNotExistsIfSupported() + "${this.identity(item)} (" +
"${item.columns.joinToString { it.descriptionDdl(false) }}," +
" CONSTRAINT ${"fk_Item_categoryId__id".inProperCase()}" +
" FOREIGN KEY (${this.identity(item.categoryId)})" +
" REFERENCES ${this.identity(category)}(${this.identity(category.id)})" +
onDeleteSetDefaultPart +
onUpdateSetDefaultPart +
")"
)
assertEqualCollections(item.ddl, expected)
}
}

object OneTable : IntIdTable("one")
object OneOneTable : IntIdTable("one.one")

Expand Down

0 comments on commit 818ece7

Please sign in to comment.