Skip to content

Commit

Permalink
fix: EXPOSED-580 MigrationsUtils.statementsRequiredForDatabaseMigrati…
Browse files Browse the repository at this point in the history
…on throws an error when a table is passed that does not already exist in the database

The problem was in the `existingIndices` function. For some dialects, querying indices for a table that does not exist throws an error. This is fixed in the `existingIndices` function by filtering the tables and using only those that exist.
  • Loading branch information
joc-a committed Oct 10, 2024
1 parent 9697dbc commit 1a2bac3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ abstract class VendorDialect(
}

override fun existingIndices(vararg tables: Table): Map<Table, List<Index>> =
TransactionManager.current().db.metadata { existingIndices(*tables) }
TransactionManager.current().db.metadata { existingIndices(tables = tables.filter { it.exists() }.toTypedArray()) }

override fun existingPrimaryKeys(vararg tables: Table): Map<Table, PrimaryKeyMetadata?> =
TransactionManager.current().db.metadata { existingPrimaryKeys(*tables) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ class DatabaseMigrationTests : DatabaseTestsBase() {
}
}

@Test
fun testCreateStatementsGeneratedForTablesThatDoNotExist() {
val tester = object : Table("tester") {
val bar = char("bar")
}

withDb {
val statements = MigrationUtils.statementsRequiredForDatabaseMigration(tester, withLogs = false)
assertEquals(1, statements.size)
assertEquals(
"CREATE TABLE${" IF NOT EXISTS".takeIf { currentDialectTest.supportsIfNotExists } ?: ""} ${tester.nameInDatabaseCase()} " +
"(${"bar".inProperCase()} CHAR NOT NULL)",
statements.first()
)
}
}

@Test
fun testDropUnmappedColumnsStatementsIdentical() {
val t1 = object : Table("foo") {
Expand Down

0 comments on commit 1a2bac3

Please sign in to comment.