Migrate existing sqlite database to Room, where to put existing migration scripts? - android

I have an existing sqlite database on version 3. My SqliteOpenHelper manages upgrading from v1 to 3. I'd like to migrate to Room. I understand making a new Room Migration to go from my current v3 to the new Roomified v4, but where do my existing migration scripts go?
If user upgrades app from v2 of the database straight to v4 of the database, Room will handle v3 to 4, but what handles v2 to 3?
I tried porting migration scripts into Room Migration objects, but they fail to run because no json schema exists (room was used on those schemas).
I tried keeping my old SqliteOpenHelper around and using it to upgrade from 1 to 3 before building the Room database. This works first run, but subsequent runs crash the SqliteOpenHelper saying it can't downgrade the db (Room successfully upgrades to v4, but open helper only knows about v3. Using it to ensure db upgraded to v3 causes it to try and downgrade)

Generally you can move all your old migrations to Room's Migration objects, so in your case create all of : Migration(1, 2), Migration(2, 3), Migration(3, 4) migrations.
There are also a few things to know about:
migrations will be executed lazily - when the first query is
executed, not when Room instance is retrieved
if your current version is 3 you will need to create migration to version 4, even empty, as you already noticed
if you want to speed up the migration process, you can create direct Migrations like Migration(1,4)
Take a look at this example which is well described in this article and let me know if you have any more questions.

Related

Will Room migrations be run for new users?

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.

android does migrating to room needs an uninstall of the app

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.

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

Getting past db data when realm migration

I have used realm database(iOS and Android both). It worked well still now and migration. When migration, I could add table and additional columns in updated schema. By the way, I have no idea whether I'm capable of extracting previous table data and move to new schema table. Please let me know about this.
Just to confirm, you've successfully managed to perform a schema migration with Realm on both the iOS and Android versions of your app, but your question is whether you can extract data from a table in an older schema version and move it to a new table in your latest schema version. Is this correct?
If this is correct, then yes. At the time of performing a migration, you can run a loop inside your MigrationBlock block/RealmMigration object to manually copy the data from an older table to a newly created one.
Examples of this logic can be found in the sample code of both the iOS and Java repositories on Realm's GitHub account.
Unfortunately, once a migration has completed on a Realm file and the previous table has been deleted, then it's not possible to back-track and extract the data at a later time.
You could do something like this:
RealmSchema schema = realm.getSchema();
schema.get("OldTableName").renameField("OldFieldName", "NewFieldName");
schema.rename("OldTableName", "NewTableName");

Versioning objects in firebase

I am using firebase as a database for a mobile application. Mobile application version 1 using a certain DB structure. But in version 2 I have a major schema changes. I could not find any specific documentation which would mention the best practices for managing DB upgrades. So I am thinking of following steps, which looks good on paper.
Application version 1 is in production using firebase/v1
Copy version 1 schema firebase/v1 to firebase/v2
Upgrade firebase/v2 schema
Disable write operations on firebase/v1
Distribute application v2 pointing to firebase/v2
With these steps users with older versions app would be able to only read the data. So unless they dont upgrade the app they wont be able to modify any data.
Do I going in the right direction in managing my schema updates? Or is there any better way to do this.
Use cloud function database functions to migrate data from db/v1 to db/v2. On update event in db/v1, you can write to db/v2 in parallel, so all active user data can move into db/v2.
https://firebase.google.com/docs/functions/database-events
Migrate data asap!

Categories

Resources