Я пытаюсь перенести базу данных комнаты в свой проект и пишу запрос на миграцию.
Добавьте два новых столбца в существующую таблицу. Вот код Это правильно?
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE pages ADD COLUMN sources TEXT NOT NULL default '')");
}
};
Вот код AppDatabase:
@Database(entities = {DayInfoEntity.class, UserEntryEntity.class, CycleInfoEntity.class, CycleInfoTempEntity.class, PagesEntity.class}, version = 2, exportSchema = true)
@TypeConverters({DateConverter.class})
public abstract class AppDatabase extends RoomDatabase {
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE pages ADD COLUMN sources TEXT NOT NULL default ''");
}
};
private static AppDatabase appDatabase;
@VisibleForTesting
private static final String DATABASE_NAME = "app_name";
public abstract PagesDao pagesDao();
private final MutableLiveData<Boolean> mIsDatabaseCreated = new MutableLiveData<>();
public static AppDatabase getInstance(final Context context) {
if (appDatabase == null) {
synchronized (AppDatabase.class) {
if (appDatabase == null) {
appDatabase = buildDatabase(context.getApplicationContext());
appDatabase.updateDatabaseCreated(context.getApplicationContext());
}
}
}
return appDatabase;
}
private static AppDatabase buildDatabase(final Context appContext) {
return Room.databaseBuilder(appContext, AppDatabase.class, DATABASE_NAME)
.allowMainThreadQueries()
.addMigrations(MIGRATION_1_2)
.build();
}
private void updateDatabaseCreated(final Context context) {
if (context.getDatabasePath(DATABASE_NAME).exists()) {
setDatabaseCreated();
}
}
private void setDatabaseCreated() {
mIsDatabaseCreated.postValue(true);
}
}
Когда я пытаюсь запустить в это время, я получаю эту ошибку .
Migration didn't properly handle: pages.
Expected:
TableInfo{name='pages', columns={help=Column{name='help', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, privacyPolicy=Column{name='privacyPolicy', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, legend=Column{name='legend', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, info=Column{name='info', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, backstory=Column{name='backstory', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, language=Column{name='language', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, termsConditions=Column{name='termsConditions', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, sources=Column{name='sources', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='pages', columns={help=Column{name='help', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, legend=Column{name='legend', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, privacyPolicy=Column{name='privacyPolicy', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, info=Column{name='info', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, language=Column{name='language', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, sources=Column{name='sources', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue=''''}, termsConditions=Column{name='termsConditions', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}