Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3112

When new table is added in room db from version 1 to version 2

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.

Upvotes: 3

Views: 3245

Answers (1)

brocky34
brocky34

Reputation: 178

  1. 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.

  2. 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!

Upvotes: 2

Related Questions