Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate existing SQLite application to Room Persistance Library?

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?

like image 941
Marko Gajić Avatar asked May 20 '17 02:05

Marko Gajić


People also ask

How we can do migration in room DB?

You can use AutoMigrationSpec to give Room the additional information that it needs to correctly generate migration paths. Define a static class that implements AutoMigrationSpec in your RoomDatabase class and annotate it with one or more of the following: @DeleteTable. @RenameTable.

Is room better than SQLite?

Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. Room is now considered as a better approach for data persistence than SQLiteDatabase.

Is SQLite database persistent?

Sqlite Database commonly used to persist the data in most Android applications. Sqlite file is created in your package folder. Sqlite data is persisted after application close and even phone switch off.


2 Answers

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();
like image 105
Gubbel Avatar answered Oct 12 '22 05:10

Gubbel


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.

like image 44
musooff Avatar answered Oct 12 '22 05:10

musooff