17

It might be a bit early to ask, but is it possible and how to migrate/upgrade an existing SQLite database application to a new Android Room Persistance Library?

Marko Gajić
  • 431
  • 4
  • 15

2 Answers2

16

Assuming your room entities match your current table schemas, you can keep using the same database/tables.

Room manages a master table which is initialized on creation or upgrade of the database, so you need to increment your database version and provide a dummy migration:

@Database(entities = SomeEntity.class, version = EXISTING_VERSION + 1)
public class MyDatabase extends RoomDatabase {
    // ...
}

MyDatabase db = Room.databaseBuilder(context, MyDatabase.class, "db_name")
                    .addMigrations(new Migration(EXISTING_VERSION, EXISTING_VERSION + 1) {
                        @Override
                        public void migrate(SupportSQLiteDatabase database) {
                            // NOOP
                        }
                    }).build();
Gubbel
  • 2,327
  • 1
  • 24
  • 35
  • Haven't tested it yet, but I'll mark it as an answer, since database name parameter probably allows you to use any previous and already existing database. – Marko Gajić May 28 '17 at 16:25
  • Do you know a way to test this migration? I've looked at https://developer.android.com/topic/libraries/architecture/room.html#testing but I'm not sure how to test a migration from existing sqlite into room. – AdamMc331 Jul 22 '17 at 16:03
  • @Gubbel If I use the above dummy migration, can I keep all of my existing SQLite queries in my existing SQLite database class or do I need to somehow migrate them also? – AJW Jan 23 '19 at 15:03
1

For those who are wondering if there is any way to migrate from SQLite to Room even if your schema does not match, the answer is YES, you can migrate from SQLite to room even if the schema does not match.

It is possible, but a requires quite careful conversions. As the process requires so many steps to cover, I will just leave references you can follow.

Incrementally migrate from SQLite to Room

Hope it will be helpful for a few.

musooff
  • 6,412
  • 3
  • 36
  • 65
  • @unexplored removed the duplicate link. I am not able to find the old link, probably it got deleted – musooff May 23 '21 at 01:30