Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix tables creation depending on each other via foreignKey constraint #1649

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading