Skip to content

feat(data): migrate Room schemas instead of dropping the database#59

Merged
netqo merged 1 commit into
devfrom
feat/db-migrations
Jun 24, 2026
Merged

feat(data): migrate Room schemas instead of dropping the database#59
netqo merged 1 commit into
devfrom
feat/db-migrations

Conversation

@netqo

@netqo netqo commented Jun 24, 2026

Copy link
Copy Markdown
Owner

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

  • StackDatabase now sets exportSchema = true; the build exports a JSON schema per version under app/schemas/ (KSP room.schemaLocation). The v5 schema is generated by Room; the v4 schema is the same set minus game_round, kept so the migration test can build the old database.
  • MIGRATION_4_5 creates the game_round table (the only schema change since v4); its CREATE TABLE matches the entity exactly, so runMigrationsAndValidate accepts it.
  • The builder runs addMigrations(MIGRATION_4_5) and only fallbackToDestructiveMigrationOnDowngrade(dropAllTables = true) - upgrades preserve data; a downgrade (a developer installing an older build) has no forward path and recreates the DB.
  • Going forward, each schema bump adds one 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 compileDebugAndroidTestKotlin green locally (JDK 17).
  • New instrumented test StackDatabaseMigrationTest: seeds a v4 DB with a cached news_article, runs MIGRATION_4_5, and asserts the article survives and game_round exists. 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.
  • Installed the app build on the device: it opens cleanly over the existing v5 database (no Room identity-hash / migration errors in logcat), confirming the new builder config is a no-op for an already-current database.

Test plan

  • App opens over an existing database without errors.
  • (CI/emulator) StackDatabaseMigrationTest passes: news cache survives 4 -> 5 and game_round is created.

Checklist

  • Commit messages follow Conventional Commits (see CONTRIBUTING.md).
  • Branch name follows the naming convention (feat/, fix/, chore/, etc.).
  • The change is scoped: no unrelated edits sneaked into this PR.
  • If user-visible behavior changed, CHANGELOG.md has been updated under [Unreleased].
  • If a new dependency was added, it is justified in the PR description. (No new dependencies; room-testing moved to androidTestImplementation for MigrationTestHelper.)

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.)
@netqo netqo merged commit edc9fcc into dev Jun 24, 2026
3 checks passed
@netqo netqo deleted the feat/db-migrations branch June 24, 2026 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant