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 Dec 17, 2022
1 parent 2092d3b commit 53f9cff
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ object SchemaUtils {
if (tables.isEmpty()) 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 @@ -33,9 +31,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 @@ -51,7 +47,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 @@ -76,7 +72,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 @@ -414,7 +414,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 @@ -494,7 +494,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 53f9cff

Please sign in to comment.