This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging
background
In a project, it is inevitable to use a database, and the three way framework is various, in this case I chose the Google Jetpack component Room, the previous article briefly introduced the use of Room @Query annotation. The previous articles have basically covered the basic functions of Room. However, due to the iteration and upgrading of requirements, sometimes the database structure of the first version cannot meet the current requirements. Changing database fields involves database upgrading
Migration
For database upgrade,room provides the Migration class and addMigrations method to support, the Migration class source code as follows:
public abstract class Migration {
public final int startVersion;
public final int endVersion;
public Migration(int startVersion, int endVersion) {
this.startVersion = startVersion;
this.endVersion = endVersion;
}
public abstract void migrate(@NonNull SupportSQLiteDatabase database);
}
Copy the code
From the source, Migration provides only one constructor. From the comments, Migration provides direct Migration between new and old versions. The constructor needs to pass two parameters, the start version and the end version
/**
* Creates a new migration between {@code startVersion} and {@code endVersion}.
*
* @param startVersion The start version of the database.
* @param endVersion The end version of the database after this migration is applied.
*/
Copy the code
implementation
If you want to see the format of your code, you can inherit Migration and implement Migrate. For example, upgrade from version 1 to Version 2
Update Version Changes the version in the @Database annotation of AppDatabase
private static final Migration MIGRATION_1_2 = new Migration(1.2)
{
@Override
public void migrate(@NonNull SupportSQLiteDatabase database)
{
// Perform upgrade operations}};Copy the code
In the same way, upgrade version 2 to version 3 as follows
private static final Migration MIGRATION_2_3 = new Migration(2.3)
{
@Override
public void migrate(@NonNull SupportSQLiteDatabase database)
{
// Perform upgrade operations}};Copy the code
Add the upgrade logic in buildDB using the addMigrations() method, as follows
AddMigrations (MIGRATION_1_2,MIGRATION_2_3,MIGRATION_3_4,….) The addMigrations method accepts a variable parameter. If you need to add more than one upgrade logic, separate the parameters with a comma,.addmigrations (MIGRATION_1_2,MIGRATION_2_3,MIGRATION_3_4,….)
private static AppDatabase buildDB(Context context) {
return Room.databaseBuilder(context, AppDatabase.class, DATABASE_NAME)
.addMigrations(MIGRATION_1_2)
.allowMainThreadQueries()
.build();
}
Copy the code
abnormal
Each version upgrade requires adding Migration logic. If you forget to add Migration logic, an IllegalStateException will be thrown. This is because the database upgrade fails and the application crashes. To prevent this from happening, We can through fallbackToDestructiveMigration () method to avoid, this method can be recreated when the database upgrade abnormal data table, to avoid upgrade failure cause a Crash of the application, but due to create the database, is leading to loss of data, the demand is what kind, before we decide to do you want Add this method
private static AppDatabase buildDB(Context context) {
return Room.databaseBuilder(context, AppDatabase.class, DATABASE_NAME)
.fallbackToDestructiveMigration()
.addMigrations(MIGRATION_1_2)
.allowMainThreadQueries()
.build();
}
Copy the code