Skip to content

Commit

Permalink
fix tables creation depending on each other via foreignKey constraint…
Browse files Browse the repository at this point in the history
…: fix table sorting by references, simplify code
  • Loading branch information
naftalmm committed Aug 13, 2023
1 parent 73dd153 commit cf1f837
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 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 @@ -19,7 +19,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 @@ -540,7 +540,7 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() {
}

@Test fun testCreateTableWithReferenceMultipleTimes() {
withTables(PlayerTable, SessionsTable) {
withTables(SessionsTable, PlayerTable) {
SchemaUtils.createMissingTablesAndColumns(PlayerTable, SessionsTable)
SchemaUtils.createMissingTablesAndColumns(PlayerTable, SessionsTable)
}
Expand Down Expand Up @@ -620,7 +620,7 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() {

@Test
fun testCreateCompositePrimaryKeyTableAndCompositeForeignKeyTableMultipleTimes() {
withTables(CompositePrimaryKeyTable, CompositeForeignKeyTable) {
withTables(CompositeForeignKeyTable, CompositePrimaryKeyTable) {
SchemaUtils.createMissingTablesAndColumns(CompositePrimaryKeyTable, CompositeForeignKeyTable)
SchemaUtils.createMissingTablesAndColumns(CompositePrimaryKeyTable, CompositeForeignKeyTable)
}
Expand Down

0 comments on commit cf1f837

Please sign in to comment.