Skip to content

Commit

Permalink
fix: Fix tables creation depending on each other via foreignKey const…
Browse files Browse the repository at this point in the history
…raint (#1649)

* fix tables creation depending on each other via foreignKey constraint: fix table sorting by references, simplify code

* add tests

in case there are two tables (parent and child), with the child table referring the parent by the means of a composite foreign key only, the reference is not taken into account by the call SchemaUtils.create and, subsequently, by SchemaUtils.sortTablesByReferences

this causes the SchemaUtils to try to create the child table first, resulting in an SQL error:
CREATE TABLE IF NOT EXISTS child ..., CONSTRAINT fk_name FOREIGN KEY (col1, col2) REFERENCES parent(col1 col2) ...
[42102-199]. Statement(s): ...
Table parent not found

---------

Co-authored-by: Jocelyne <38375996+joc-a@users.noreply.github.com>
  • Loading branch information
naftalmm and joc-a authored Sep 4, 2023
1 parent c3b998d commit f3a295a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ object SchemaUtils {
emptyMap()
} else {
tables.associateWith { t ->
t.columns.mapNotNull { c ->
c.referee?.let { it.table to c.columnType.nullable }
}.toMap()
t.foreignKeys.map { it.targetTable }
}
}
}
Expand All @@ -36,9 +34,7 @@ object SchemaUtils {

fun parseTable(table: Table) {
if (result.add(table)) {
table.columns.forEach {
it.referee?.table?.let(::parseTable)
}
table.foreignKeys.map { it.targetTable }.forEach(::parseTable)
}
}
tables.forEach(::parseTable)
Expand All @@ -54,7 +50,7 @@ object SchemaUtils {
fun traverse(table: Table) {
if (table !in visited) {
visited += table
graph.getValue(table).forEach { (t, _) ->
graph.getValue(table).forEach { t ->
if (t !in visited) {
traverse(t)
}
Expand All @@ -79,7 +75,7 @@ object SchemaUtils {
if (table in visited) return false
recursion += table
visited += table
return if (graph[table]!!.any { traverse(it.key) }) {
return if (graph[table]!!.any { traverse(it) }) {
true
} else {
recursion -= table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.PrimaryKeyMetadata
import org.junit.Test
import java.math.BigDecimal
import java.util.*
import java.util.UUID
import kotlin.properties.Delegates
import kotlin.test.assertNotNull
import kotlin.test.assertNull
Expand Down Expand Up @@ -539,7 +539,7 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() {
uniqueIndex("index2", value2, value1)
}
}

@Test
fun testCreateTableWithReferenceMultipleTimes() {
withTables(PlayerTable, SessionsTable) {
Expand Down Expand Up @@ -652,6 +652,22 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() {
}
}

@Test
fun testCreateCompositePrimaryKeyTableAndCompositeForeignKeyInVariousOrder() {
withTables(CompositeForeignKeyTable, CompositePrimaryKeyTable) {
SchemaUtils.createMissingTablesAndColumns(CompositePrimaryKeyTable, CompositeForeignKeyTable)
}
withTables(CompositeForeignKeyTable, CompositePrimaryKeyTable) {
SchemaUtils.createMissingTablesAndColumns(CompositeForeignKeyTable, CompositePrimaryKeyTable)
}
withTables(CompositePrimaryKeyTable, CompositeForeignKeyTable) {
SchemaUtils.createMissingTablesAndColumns(CompositePrimaryKeyTable, CompositeForeignKeyTable)
}
withTables(CompositePrimaryKeyTable, CompositeForeignKeyTable) {
SchemaUtils.createMissingTablesAndColumns(CompositeForeignKeyTable, CompositePrimaryKeyTable)
}
}

@Test
fun testCreateTableWithSchemaPrefix() {
val schemaName = "my_schema"
Expand Down

0 comments on commit f3a295a

Please sign in to comment.