Skip to content

Commit

Permalink
Consider foreign key table constraints when sorting initialization st…
Browse files Browse the repository at this point in the history
…atements (sqldelight#5325)
  • Loading branch information
TheMrMilchmann authored Jul 2, 2024
1 parent 28b600b commit 6060eed
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private fun ArrayList<SqlCreateTableStmt>.buildGraph(): Graph<SqlCreateTableStmt
this.forEach { table ->
graph.addVertex(table)
table.columnDefList.forEach { column ->
column.columnConstraintList.mapNotNull { it.foreignKeyClause?.foreignTable }.forEach { fk ->
(column.columnConstraintList.mapNotNull { it.foreignKeyClause?.foreignTable } + table.tableConstraintList.mapNotNull { it.foreignKeyClause?.foreignTable }).forEach { fk ->
try {
val foreignTable = namedStatements[fk.name]
graph.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,85 @@ class QueryWrapperTest {
)
}

@Test fun `queryWrapper puts foreign key constraint in correct order`() {
val result = FixtureCompiler.compileSql(
"""
CREATE TABLE child(
parent_id INTEGER,
FOREIGN KEY (parent_id) REFERENCES parent(id)
);
CREATE TABLE parent(
id INTEGER PRIMARY KEY
);
""".trimIndent(),
tempFolder,
overrideDialect = PostgreSqlDialect(),
)

assertThat(result.errors).isEmpty()

val queryWrapperFile = result.compilerOutput[File(result.outputDirectory, "com/example/testmodule/TestDatabaseImpl.kt")]

assertThat(queryWrapperFile).isNotNull()
assertThat(queryWrapperFile.toString()).isEqualTo(
"""
|package com.example.testmodule
|
|import app.cash.sqldelight.TransacterImpl
|import app.cash.sqldelight.db.AfterVersion
|import app.cash.sqldelight.db.QueryResult
|import app.cash.sqldelight.db.SqlDriver
|import app.cash.sqldelight.db.SqlSchema
|import com.example.TestDatabase
|import kotlin.Long
|import kotlin.Unit
|import kotlin.reflect.KClass
|
|internal val KClass<TestDatabase>.schema: SqlSchema<QueryResult.Value<Unit>>
| get() = TestDatabaseImpl.Schema
|
|internal fun KClass<TestDatabase>.newInstance(driver: SqlDriver): TestDatabase =
| TestDatabaseImpl(driver)
|
|private class TestDatabaseImpl(
| driver: SqlDriver,
|) : TransacterImpl(driver),
| TestDatabase {
| public object Schema : SqlSchema<QueryResult.Value<Unit>> {
| override val version: Long
| get() = 1
|
| override fun create(driver: SqlDriver): QueryResult.Value<Unit> {
| driver.execute(null, ""${'"'}
| |CREATE TABLE parent(
| | id INTEGER PRIMARY KEY
| |)
| ""${'"'}.trimMargin(), 0)
| driver.execute(null, ""${'"'}
| |CREATE TABLE child(
| | parent_id INTEGER,
| |
| | FOREIGN KEY (parent_id) REFERENCES parent(id)
| |)
| ""${'"'}.trimMargin(), 0)
| return QueryResult.Unit
| }
|
| override fun migrate(
| driver: SqlDriver,
| oldVersion: Long,
| newVersion: Long,
| vararg callbacks: AfterVersion,
| ): QueryResult.Value<Unit> = QueryResult.Unit
| }
|}
|
""".trimMargin(),
)
}

@Test fun `queryWrapper generates with migration statements`() {
FixtureCompiler.writeSql(
"""
Expand Down

0 comments on commit 6060eed

Please sign in to comment.