-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct migration callback AfterVersion range filter (#2394)
Introduced in 1.5.0, SQLDelight had an off-by-one error for determining when to invoke an AfterMigration callback. This commit corrects the version range for which callbacks are invoked. Closes #2393.
- Loading branch information
1 parent
cd68564
commit 6db0d68
Showing
3 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
runtime/src/commonTest/kotlin/com/squareup/sqldelight/logs/SqlDriverMigrationCallbackTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.squareup.sqldelight.logs | ||
|
||
import com.squareup.sqldelight.db.AfterVersion | ||
import com.squareup.sqldelight.db.SqlDriver | ||
import com.squareup.sqldelight.db.migrateWithCallbacks | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class SqlDriverMigrationCallbackTest { | ||
|
||
@Test fun migrationCallbackInvokedOnCorrectVersion() { | ||
val schema = fakeSchema() | ||
|
||
var callbackInvoked = false | ||
|
||
schema.migrateWithCallbacks( | ||
driver = FakeSqlDriver(), | ||
oldVersion = 1, | ||
newVersion = 2, | ||
AfterVersion(1) { callbackInvoked = true } | ||
) | ||
|
||
assertTrue(callbackInvoked) | ||
} | ||
|
||
@Test fun migrationCallbacks() { | ||
val schema = fakeSchema() | ||
|
||
var callback1Invoked = false | ||
var callback2Invoked = false | ||
var callback3Invoked = false | ||
|
||
schema.migrateWithCallbacks( | ||
driver = FakeSqlDriver(), | ||
oldVersion = 0, | ||
newVersion = 4, | ||
AfterVersion(1) { callback1Invoked = true }, | ||
AfterVersion(2) { callback2Invoked = true }, | ||
AfterVersion(3) { callback3Invoked = true }, | ||
) | ||
|
||
assertTrue(callback1Invoked) | ||
assertTrue(callback2Invoked) | ||
assertTrue(callback3Invoked) | ||
} | ||
|
||
@Test fun migrationCallbackLessThanOldVersionNotCalled() { | ||
val schema = fakeSchema() | ||
|
||
var callback1Invoked = false | ||
var callback2Invoked = false | ||
var callback3Invoked = false | ||
|
||
schema.migrateWithCallbacks( | ||
driver = FakeSqlDriver(), | ||
oldVersion = 2, | ||
newVersion = 4, | ||
AfterVersion(1) { callback1Invoked = true }, | ||
AfterVersion(2) { callback2Invoked = true }, | ||
AfterVersion(3) { callback3Invoked = true }, | ||
) | ||
|
||
assertFalse(callback1Invoked) | ||
assertTrue(callback2Invoked) | ||
assertTrue(callback3Invoked) | ||
} | ||
|
||
@Test fun migrationCallbackGreaterThanNewVersionNotCalled() { | ||
val schema = fakeSchema() | ||
|
||
var callback1Invoked = false | ||
var callback2Invoked = false | ||
var callback3Invoked = false | ||
var callback4Invoked = false | ||
|
||
schema.migrateWithCallbacks( | ||
driver = FakeSqlDriver(), | ||
oldVersion = 0, | ||
newVersion = 4, | ||
AfterVersion(1) { callback1Invoked = true }, | ||
AfterVersion(2) { callback2Invoked = true }, | ||
AfterVersion(3) { callback3Invoked = true }, | ||
AfterVersion(4) { callback4Invoked = true }, | ||
) | ||
|
||
assertTrue(callback1Invoked) | ||
assertTrue(callback2Invoked) | ||
assertTrue(callback3Invoked) | ||
assertFalse(callback4Invoked) | ||
} | ||
|
||
private fun fakeSchema() = object : SqlDriver.Schema { | ||
override val version: Int = 1 | ||
override fun create(driver: SqlDriver) = Unit | ||
override fun migrate(driver: SqlDriver, oldVersion: Int, newVersion: Int) = Unit | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters