Reputation: 1083
Currently my database version is 5, I have added column to database table and changed the database version to 6, and also added migration from version 5 to 6 like this
private static final Migration MIGRATION_5_6 = new Migration(5, 6) {
@Override
public void migrate(SupportSQLiteDatabase database) {
try {
database.execSQL("ALTER TABLE generate_data "
+ " ADD COLUMN generateImgPath TEXT");
}catch (Exception e){
e.printStackTrace();
}
}
};
Question: Do I need to make migration objects for every version?
Like
MIGRATION_1_6
MIGRATION_2_6
MIGRATION_3_6
MIGRATION_4_6
MIGRATION_5_6
Upvotes: 2
Views: 1701
Reputation: 3486
Do I need to make migration objects for every version?
No, you don't have to create migration objects for all previous versions to the latest one. You just have to create a migration object from the second last version to the latest one.
But, for migrating users who're using an older database version, you have to add migration
for every consecutive version like
MIGRATION_1_2
MIGRATION_2_3
MIGRATION_3_4
MIGRATION_4_5
MIGRATION_5_6
So that, if any user is on the oldest version of the database
, then Room
will run all consecutive migrations
and update it to the latest version.
If you are just adding a new column
to the table
, going from version 5 to version 6 of the database. You can use autoMigration
so you don't have to manually define the migration
strategy.
For using autoMigration
, like from version 5 to version 6, change version to 6, and add autoMigration
to the database
annotation.
@Database(
version = 5,
entities = [ table.class ],
autoMigrations = [
AutoMigration (from = 5, to = 6)
]
)
abstract class YourDatabase: RoomDatabase { }
Say, you add a new column in the future version 7 too, then you can just add another AutoMigration
object to the autoMigrations
autoMigrations = [
AutoMigration (from = 5, to = 6),
AutoMigration (from = 6, to = 7)
]
Upvotes: 4