Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room database schema update

My application is in release mode and I am using room database and my previous database version was 2 with fallback to destructive migration enabled.

@Database(entities = {
        User.class,ApplicationSetting.class},
        version = 2,
        exportSchema = false)
abstract public class DatabaseContext extends RoomDatabase {

    private static final Object sLock = new Object();
    private static DatabaseContext INSTANCE;
    public static String DATABASE_NAME = AppConstants.DATABASE_NAME;

    public static DatabaseContext getInstance(Context context) {
        synchronized (sLock) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        DatabaseContext.class, DATABASE_NAME)
                        .fallbackToDestructiveMigration()
                        .build();
            }
            return INSTANCE;
        }
    }

I have added a new column in table and change version to 3. Now I want to provide migration from version 2 to version 3 so that data not lost. But I am confused because in version 2 I have enabled fallback destructive migration, and now in version 3 I want to keep user data and remove fallback destruction.

How can I achieve this?

like image 562
Coding Geek Avatar asked Nov 01 '19 18:11

Coding Geek


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. onUpgrade method. This is how you tell SQLite what it needs to do when going from an old version to a new version.

What is export schema in room?

Export schemas Room can export your database's schema information into a JSON file at compile time. To export the schema, set the room. schemaLocation annotation processor argument via a [ CommandLineArgumentProvider ][15]{:. external} in app/build. gradle file.

Is room a database or ORM?

Room is what's called an ORM (Object Relational Mapping) library, which as the name implies, maps the tables in a relational database to objects usable in Kotlin code.

What is migration in Android?

A migration can handle more than 1 version (e.g. if you have a faster path to choose when going version 3 to 5 without going to version 4). If Room opens a database at version 3 and latest version is >= 5, Room will use the migration object that can migrate from 3 to 5 instead of 3 to 4 and 4 to 5.


1 Answers

When using fallbackToDestructiveMigration, it only destroys (drops the tables and recreates them) if there is not a defined migration for the migration so you could add a 2 to 3 Migration.

That is, if a Migration is provided then it is used and bypasses fallback/destruction.

An alternative, which is recommended, is to use fallbacktodestructivemigrationfrom, this can be used to define specific missing migrations where fallback is to be applied.

e.g. you could use .fallbackToDestructiveMigrationFrom(1,7)

  • 1 to allow destructive from 1 to 2
  • 7 to allow destructive from 7 to 8 (added just to show that multiple start versions can be provided)
  • other's 2 to 3, 3 to 4 etc will need a Migration.
  • Note that this is more specific, so if a Migration for 1 to 2 or 7 to 8 is provided an exception will result.
like image 109
MikeT Avatar answered Sep 28 '22 09:09

MikeT