Raheem
Raheem

Reputation: 634

Should I increment the value of Room database version when migration strategy is fallback to destructive?

Should I increment the value of the Room database version if I change the content of the database when its migration strategy is set to "fallback to destructive migration"?

Following changes made to database:

  1. Some columns are removed.
  2. Content of some rows is updated in the database file that is stored in assets.

Upvotes: 0

Views: 993

Answers (2)

HaroldSer
HaroldSer

Reputation: 2065

Yes. You should update the version of the database even with fallback to destructive migration strategy. Room uniquely identifies every database version with an "identity hash string", which is kept in a configuration table.

Keeping the database version unchanged will cause the app to crash with an IllegalStateException. Internally Room checks the identity of the database comparing the identity hash of the current version vs the the one stored in the table called "room_master_table".

You can learn more about this in the below article which also explains in detail how to handle version change and testing.

Reference: https://medium.com/androiddevelopers/understanding-migrations-with-room-f01e04b07929

Upvotes: 2

Raheem
Raheem

Reputation: 634

UPDATE: It is interesting that the app crashed in some users, but for some users including me, it didn't crash. So it's better to update the version of the database even though its migration strategy is fallback to destructive.

From my experience, it seems we don't have to increment the value of Room database version when its migration strategy is set to "fallback to destructive migration".

I changed the content of the database and removed some columns from the database file.

Then I installed the previous version of the app (unchanged database). Then I used the application and made sure a database instance is created.

Then I installed the new version of the app (changed database). And it didn't crash and everything worked fine. Changes applied to the database.

Upvotes: 0

Related Questions