feat(data): migrate Room schemas instead of dropping the database#59
Merged
Conversation
Replace fallbackToDestructiveMigration with real Room migrations so the offline cache (news, wallet, notifications) survives schema changes instead of being wiped on every version bump. - Enable exportSchema and the room.schemaLocation KSP arg; commit the exported v5 schema (and the derived v4 schema for migration testing). - Add MIGRATION_4_5, which creates the game_round table introduced with the games card. - The builder runs the migration and only falls back to a destructive recreate on a downgrade (no forward path for installing an older build). Covered by the instrumented StackDatabaseMigrationTest, which seeds a v4 database with a cached news article, runs MIGRATION_4_5, and asserts the article survives and game_round exists. (The migration test could not be executed on the test device, which blocks instrumented-test APK installs; it compiles and validates against the exported schema.)
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Switches the Room database from
fallbackToDestructiveMigration()to real migrations, so the offline cache (news feed, wallet, notifications) survives app updates instead of being wiped on every schema version bump. This is the durability fix behind "show the downloaded news offline" - the news code was already offline-first, but the destructive migration dropped the cache when the games card bumped the schema to v5.Rationale
StackDatabasenow setsexportSchema = true; the build exports a JSON schema per version underapp/schemas/(KSProom.schemaLocation). The v5 schema is generated by Room; the v4 schema is the same set minusgame_round, kept so the migration test can build the old database.MIGRATION_4_5creates thegame_roundtable (the only schema change since v4); itsCREATE TABLEmatches the entity exactly, sorunMigrationsAndValidateaccepts it.addMigrations(MIGRATION_4_5)and onlyfallbackToDestructiveMigrationOnDowngrade(dropAllTables = true)- upgrades preserve data; a downgrade (a developer installing an older build) has no forward path and recreates the DB.Migration+ an exported schema, and Room verifies them.Note: this helps from v5 onward. Users who already went through the earlier destructive v4 -> v5 wipe don't get that data back (it already happened); the schema is stable now.
Verification
./gradlew testDebugUnitTest detekt ktlintCheck assembleDebug compileDebugAndroidTestKotlingreen locally (JDK 17).StackDatabaseMigrationTest: seeds a v4 DB with a cachednews_article, runsMIGRATION_4_5, and asserts the article survives andgame_roundexists. It compiles and validates against the exported schema; it could not be executed on the test device (Xiaomi blocks instrumented-test APK installs over USB), so it should run in CI / an emulator.Test plan
StackDatabaseMigrationTestpasses: news cache survives 4 -> 5 andgame_roundis created.Checklist
feat/,fix/,chore/, etc.).[Unreleased].room-testingmoved toandroidTestImplementationfor MigrationTestHelper.)