Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android room persistent library - How to change database version

Im not clear on how to use room after i have updated the database version.

For example, lets say i originally had the following database defined in room:

@Database(entities = {Event.class}, version = 1)
@TypeConverters(DateTypeConverter.class)
public abstract class EventDatabase extends RoomDatabase {

   public abstract EventDao eventDao();

}

and then i change the version so that it looks like this now:

@Database(entities = {Event.class}, version = 2)
@TypeConverters(DateTypeConverter.class)
public abstract class EventDatabase extends RoomDatabase {

   public abstract EventDao eventDao();

}

when i saw change the version i mean that i may have added or deleted columns in the database so its not same. my questions are the following:

do i need to maintain two databases now ? v1 and v2 ? and is there a way to copy entities easily over to v2 ? also when changing the version is it enough to simply change it from 1 to 2 or do i have to create another class called EventDatabase2 for example ?

also here is the version of room i am using:android.arch.persistence.room:runtime:1.0.0-alpha1

like image 880
j2emanue Avatar asked May 30 '17 23:05

j2emanue


People also ask

When should I change the Room database version?

More precisely, every time you alter your schema by adding, removing or modifying tables, you have to increase the database version number and update your implementation of SQLiteOpenHelper.

What is Room database version?

Note: Room supports automated migrations in version 2.4. 0-alpha01 and higher. If your app uses a lower version of Room, you must define your migrations manually. // Database class before the version update.

Is Room database deprecated?

This method is deprecated. Deletes all rows from all the tables that are registered to this database as Database. entities .

Is Room database persistent?

Room is a persistence library that's part of Android Jetpack. Room is an abstraction layer on top of a SQLite database. SQLite uses a specialized language (SQL) to perform database operations.


1 Answers

So lets say i have a new app version and a new database version also. I simply need to change the version = 2 like this:

@Database(entities = {Event.class}, version = 2)
@TypeConverters(DateTypeConverter.class)
public abstract class EventDatabase extends RoomDatabase {

   public abstract EventDao eventDao();

}

and then provide a migration policy like this:

Room.databaseBuilder(getApplicationContext(), MyDb.class, "database-name")
        .addMigrations(MIGRATION_1_2).build();

static final Migration MIGRATION_1_2 = new Migration(1, 2) {
    @Override
    public void migrate(SupportSQLiteDatabase database) {
        database.execSQL("CREATE TABLE `Fruit` (`id` INTEGER, "
                + "`name` TEXT, PRIMARY KEY(`id`))");
    }
};

the key thing here is if a migration policy is not provided it seem the entire database is rebuilt (so your user would loose all previous data).

this is according to @commonsWare update link provided .

like image 180
j2emanue Avatar answered Oct 22 '22 14:10

j2emanue