Skip to content

Commit b0a7aed

Browse files
authored
fix: EXPOSED-580 MigrationsUtils.statementsRequiredForDatabaseMigration throws an error when a table is passed that does not already exist in the database (#2271)
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 `statementsRequiredForDatabaseMigration` function by passing only tables that exist when invoking the `mappingConsistenceRequiredStatements` function.
1 parent 58d96f0 commit b0a7aed

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

exposed-migration/src/main/kotlin/MigrationUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ object MigrationUtils {
8282

8383
val modifyTablesStatements = logTimeSpent("Checking mapping consistence", withLogs) {
8484
mappingConsistenceRequiredStatements(
85-
tables = tables,
85+
tables = tablesToAlter.toTypedArray(),
8686
withLogs
8787
).filter { it !in (createStatements + alterStatements) }
8888
}

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 ${addIfNotExistsIfSupported()}${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)