[英]Should my room database start at version 0?
I have realised that my database migration strategy was incorrect and have rewritten it.我意识到我的数据库迁移策略不正确并重写了它。 At the moment it looks like this:
目前它看起来像这样:
@Database(entities = {SaveData.class, Achievement.class}, version = 2, exportSchema = false)
@TypeConverters(MapConverters.class)
public abstract class AppDatabase extends RoomDatabase {
public abstract SaveDataDAO saveDataDAO();
public abstract AchievementDAO achievementDAO();
}
With a helper class that looks like this:使用如下所示的助手 class:
public class AppDatabaseHelper {
private static AppDatabase db;
private AppDatabaseHelper() {
// static utility
}
private static final Migration MIGRATION_1_2 =
new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
Set<AchievementType> achievementTypes = EnumSet.allOf(AchievementType.class);
String values = achievementTypes.stream().map(AppDatabaseHelper::createRow).collect(Collectors.joining(", "));
String fullQuery = "INSERT OR IGNORE INTO Achievement(name, current, total) VALUES " + values;
database.execSQL(fullQuery);
}
};
private static String createRow(AchievementType achievementType) {
String name = achievementType.name();
int total = achievementType.getTotal();
return String.format("('%s',0,%s)", name, total);
}
public static AppDatabase getDatabase(Context context) {
if (db == null) {
db = Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class, "game")
.addMigrations(MIGRATION_1_2)
.build();
}
return db;
}
}
When I delete the database and run the app, accessing the database for the first time does not run this migration as expected.当我删除数据库并运行应用程序时,第一次访问数据库不会按预期运行此迁移。 Instead, I can see that the version of the database inside
SQLLiteOpenHelper.java
is 0, attempting to migrate to 2. Because of this specific part of the Android helper class:相反,我可以看到
SQLLiteOpenHelper.java
中的数据库版本为 0,试图迁移到 2。由于 Android 助手 ZA2F2ED4F8EBC2CBB4C21A29DC4 的这个特定部分:
db.beginTransaction();
try {
if (version == 0) {
onCreate(db);
} else {
if (version > mNewVersion) {
onDowngrade(db, version, mNewVersion);
} else {
onUpgrade(db, version, mNewVersion);
}
}
db.setVersion(mNewVersion);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
inside the getDatabaseLocked
method, I can see that it will not run any migrations if it starts at version 0, and will instead just set the version to be whatever you are migrating up to.在
getDatabaseLocked
方法中,我可以看到如果它从版本 0 开始,它将不会运行任何迁移,而是将版本设置为您要迁移的任何版本。
This behaviour doesn't make any sense to me, so I assume I am missing something.这种行为对我来说没有任何意义,所以我认为我遗漏了一些东西。 I have tried a hacky workaround of just forcing the migration to go to 1 (by creating a different
RoomDatabase
class) then running the above which works.我尝试了一个 hacky 解决方法,即强制迁移到 go 到 1(通过创建不同的
RoomDatabase
类)然后运行上面的工作。 I have also tried manually setting the database version to be 1, but this doesn't seem to be the right solution either.我也尝试过手动将数据库版本设置为 1,但这似乎也不是正确的解决方案。
Let me know if you need more code for context如果您需要更多上下文代码,请告诉我
I am not sure I've got right your trouble.我不确定我是否解决了你的问题。 Sorry if my answer below - it's not the case.
对不起,如果我在下面回答 - 事实并非如此。
It's not clear for me what kind of migration you expect (in Migration there is some block with insert some values).我不清楚您期望什么样的迁移(在迁移中有一些插入一些值的块)。 If the only change in this migration is -inserting some predefining values into some table - then maybe you should prepopulate your initial database from assets or from file ?
如果此迁移中的唯一更改是 - 将一些预定义值插入到某个表中 - 那么也许您应该从资产或文件中预填充初始数据库?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.