Skip to content

Commit 1a2bac3

Browse files
committed
fix: EXPOSED-580 MigrationsUtils.statementsRequiredForDatabaseMigration 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.
1 parent 9697dbc commit 1a2bac3

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/vendors/VendorDialect.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ abstract class VendorDialect(
115115
}
116116

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

120120
override fun existingPrimaryKeys(vararg tables: Table): Map<Table, PrimaryKeyMetadata?> =
121121
TransactionManager.current().db.metadata { existingPrimaryKeys(*tables) }

exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/DatabaseMigrationTests.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,23 @@ class DatabaseMigrationTests : DatabaseTestsBase() {
133133
}
134134
}
135135

136+
@Test
137+
fun testCreateStatementsGeneratedForTablesThatDoNotExist() {
138+
val tester = object : Table("tester") {
139+
val bar = char("bar")
140+
}
141+
142+
withDb {
143+
val statements = MigrationUtils.statementsRequiredForDatabaseMigration(tester, withLogs = false)
144+
assertEquals(1, statements.size)
145+
assertEquals(
146+
"CREATE TABLE${" IF NOT EXISTS".takeIf { currentDialectTest.supportsIfNotExists } ?: ""} ${tester.nameInDatabaseCase()} " +
147+
"(${"bar".inProperCase()} CHAR NOT NULL)",
148+
statements.first()
149+
)
150+
}
151+
}
152+
136153
@Test
137154
fun testDropUnmappedColumnsStatementsIdentical() {
138155
val t1 = object : Table("foo") {

0 commit comments

Comments
 (0)