Our app has DB version 14 and uses the android sqllite helpers. Now we are moving to use the room db and migrating all the DB stuff to room. we have defined all the migrations from 1_2, 2_3 .. to 13_14 and give all these int the build
.databaseBuilder(
app,
Database::class.java,
"my.db"
).addMigrations(arrayOf(
MIGRATION_1_2,
MIGRATION_2_3,
.....
MIGRATION_10_11,
MIGRATION_11_12,
MIGRATION_12_13,
MIGRATION_13_14
))
.build()
Is this right way of doing. If i have the non room version of the app installed with DB version 14 and i try to update it to room version of the app with same db version 14, I am getting the error :
java.lang.IllegalStateException: Room cannot verify the data
integrity. Looks like you've changed schema but forgot to update the
version number. You can simply fix this by increasing the version
number.
You only need to add a migration from your existing SQLite database to a Room-managed database. This should be an empty "do nothing" migration. This is required so that Room plays nicely with your existing database.
Related
My app is getting the following error:
Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.
I don't want to increase the version since the app I'm developing is not yet released. What should I do to prevent this error?
Just clear the app data after changing the schema.
If you do not care about the existing data in the Room DB, you can clear the tables like this:
viewModelScope.launch {
yourRoomDBInstance.clearAllTables()
}
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:
Some columns are removed.
Content of some rows is updated in the database file that is stored in assets.
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
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.
I want to update my app at play store with bug fixes. APP already uses Room DB. Do i need to migrate the DB. There is no change in the DB. There are only UI changes. Does new version effect the people DB, who are already using it. Can someone provide the details about this.
Thanks in advance.
There is no change in the DB.
The database remains and you do not need to change the version of the database (as per the #Database( .... version=?)).
You only need to change (increase) the database version if the schema has changed.
If you increase the version number and do not have a migration (migration class to call) to cover that version, then an IllegalStateException will occur unless you have .fallbackToDestructiveMigration() which WILL delete and recreate the database (effectively empty the database with the ne schema).
You can also use fallbackToDestructiveMigrationFrom()
It is the version number that determines if a database migration is required. However, if a change is made and if any of the entities mismatch the database when the .build method is run then an exception will result.
If you change an Entity (the schema) and do not increase the version number you will get an IllegalStateException with the message starting with Room cannot verify the data integrity. This is because the hash stored in the room_master_table has changed.
If you changed the database schema outside of room then you'd get an exception indicating that the expected schema (the database according to the Entities) does not match the schema found (the database according to the actual database).
In your situation I'd suggest not changing the version number.
You may wish to have a look at:-
Migrating Room Databases
#Database
fallbackToDestructiveMigration
fallbackToDestructiveMigrationFrom
Let's say I have 2 migrations (1-->2, 2-->3). If a new user comes along and installs the app, will all the migrations be run, or will they just get the version 3 schema database?
They'll just get version 3. Databases internally track their database version and that is what Room uses to check if a migration is needed. In the case of a brand new database, Room just starts the user at the version of the database you specify.
I am using Room Persistance library which android has released as a alternative for SQLite Database.
I am currently having Android app on playstore with SQLite Database and While migrating app from SQLite to Room, I am facing several errors.
First
If I create new table do I need to write migration script i.e; I need to write migration query in Room everytime I upgrade the room version?
Second
I have the DB version on playstore with version of 20 and when I upgrade it to 21. Do I need to write migration scripts from 1 to 20 and 20 to 21 or only from 20 to 21.
Because I don't know what app db version does user have (production app), it can be 10,12, 15. How will the migration scripts be?
Thank you.
Every time you add a table (or make any schema change) you need to either a) add a migration or b) call .fallbackToDestructiveMigration() ONLY IF YOU DON'T CARE ABOUT PERSISTING THE DATA. Check out https://medium.com/google-developers/understanding-migrations-with-room-f01e04b07929 for more.
You'll need to write a migration script for every database your user could be on to upgrade them to 21 (again, unless you don't care about persisting data, in which case you can use .fallbackToDestructiveMigration()). This above link should also address this question.
Hope this helps!