From 818ece7e26c6ed7713d7e467ff8e76e12865e0b4 Mon Sep 17 00:00:00 2001 From: Jocelyne Date: Thu, 25 May 2023 19:10:20 +0200 Subject: [PATCH] chore: add test --- .../org/jetbrains/exposed/sql/Constraints.kt | 8 +++- .../sql/tests/shared/ddl/CreateTableTests.kt | 45 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt index 80c45f03e5..3304364624 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt @@ -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") } diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt index ea243fbee8..0dce5b7568 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt @@ -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 @@ -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")