Above error we are getting because we have change the database version but not added the migration script in our Database build statement.
So first of all, add the below line in your app level build.gradle file. The below code will export the your database schema into the json format and that will be used for migration of your database.
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation":
"$projectDir/schemas".toString()]
}
}
We have three column in our database and I want to add more column to the database “About us”. So first of all we need to modify the our Profile Entity class and add the column.
public String About;
public String getAbout() {
return About;
}
public void setAbout(String about) {
About = about;
}
Next we have to update the ProfileDatabase class and need to update the database version in annotation of the database declaration as given below.
@Database(entities = {ProfileEntity.class},version = 2)
public abstract class ProfileDatabase extends RoomDatabase{
public abstract ProfileDao profileDao();
}
Finally, we have to modify our database build statement inside main class like below.
db = Room.databaseBuilder(getApplicationContext(),
ProfileDatabase.class, DB_NAME).addMigrations(MIGRATION_1_2).build();
Where MIGRATION_1_2 is the our migration script as given below.
final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE `ProfileEntity` ADD COLUMN `About` TEXT");
}
};
Room Persistence Library provide the functionality to add multiple database migration script as per requirement. We just have to provide the start and end version of the database and write the migration script.
We may have a scenario where on some of the app database version 1 is running and one some device version2 is running and now we release the new app with version 3. So in this case we need to migrate the database from 1 to 2 and then from 2 to 3, otherwise app will get crash or we will loss the earlier stored data.
So to resolve this issue we need to add the multiple migration script in database build statement. We can it by creating the Migration script like MIGRATION_2_3 and providing the input as new Migration(2,3) and then add this migration into build statement like below.
db = Room.databaseBuilder(getApplicationContext(),
ProfileDatabase.class, DB_NAME).addMigrations(MIGRATION_1_2,MIGRATION_2_3).build();
One more case we have where we want to clear the previous schema and all data from the database. So Room Persistence Library provide an option to fallback to destructive migration. We have just need to modify the statement as given below.
db=Room.databaseBuilder(getApplicationContext(),ProfileDatabase.class,DB_NAME)
.fallbackToDestructiveMigration()
.build();
If you add the above statement to build your database instance then it clear the database completely and re-create the schema.
If you want you can download the source code of this example form GitHub
Pingback: Architecture Components: Paging Library With Room Database - NPLIX