测试会议室迁移时标识哈希检查失败



我正在按照这些说明编写一个测试。我的测试类有这个规则:

@Rule
public MigrationTestHelper testHelper = new MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
AppDatabase.class.getCanonicalName(),
new FrameworkSQLiteOpenHelperFactory()
);

我的测试如下:

@Test
public void testMigration9_10() throws IOException {
// Create the database with version 9
SupportSQLiteDatabase db = testHelper.createDatabase(TEST_DB_NAME, 9);
// Insert before migration
ContentValues values = new ContentValues();
values.put("rowid", 1);
...
db.insert("foo", SQLiteDatabase.CONFLICT_FAIL, values);
// Check inserted data
Cursor c = db.query("SELECT * FROM foo WHERE rowid = " + values.get("rowid"));
Assert.assertTrue(c.moveToFirst());
Assert.assertEquals(c.getString(c.getColumnIndex("rowid")), values.get("rowid"));
// Migrate
db = testHelper.runMigrationsAndValidate(TEST_DB_NAME, 10, true, DatabaseCreator.MIGRATION_9_10);
...
}

但这在最后一行失败了(前面的断言进展顺利),说:

java.lang.IllegalStateException:Room 无法验证数据完整性。看起来您更改了架构,但忘记更新版本号。您可以通过增加版本号来简单地解决此问题。

这是错误的。我已经追踪了发生的事情:

  • 使用版本 9 标识哈希创建数据库
  • 插入数据
  • 检查数据
  • 调用runMigrationsAndValidate,其中:
    • 打开数据库
    • 根据版本 10 检查标识哈希,但失败

检查标识哈希时,它会:

private void checkIdentity(SupportSQLiteDatabase db) {
createMasterTableIfNotExists(db);
String identityHash = "";
Cursor cursor = db.query(new SimpleSQLiteQuery(RoomMasterTable.READ_QUERY));
//noinspection TryFinallyCanBeTryWithResources
try {
if (cursor.moveToFirst()) {
identityHash = cursor.getString(0);
}
} finally {
cursor.close();
}
if (!mIdentityHash.equals(identityHash)) {
throw new IllegalStateException("Room cannot verify the data integrity. Looks like"
+ " you've changed schema but forgot to update the version number. You can"
+ " simply fix this by increasing the version number.");
}
}

因此,加载的identityHash是从数据库加载的,即版本 9;mIdentityHash是使用 version 参数 10 通过runMigrationsAndValidate直接加载的。

所以它当然失败了。

我想知道为什么它在进行迁移之前检查身份哈希。

这是迁移,即使我认为它在这里无关紧要:

public static final Migration MIGRATION_9_10 = new Migration(9, 10) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.beginTransaction();
database.execSQL("ALTER TABLE DeclarationEntity ADD latitude REAL NOT NULL DEFAULT 0.0");
database.execSQL("ALTER TABLE DeclarationEntity ADD longitude REAL NOT NULL DEFAULT 0.0");
database.endTransaction();
}
};

我做错了什么吗?

PS:这是完整的堆栈跟踪,以防这很有趣:

at android.arch.persistence.room.RoomOpenHelper.checkIdentity(RoomOpenHelper.java:119)
at android.arch.persistence.room.RoomOpenHelper.onOpen(RoomOpenHelper.java:100)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onOpen(FrameworkSQLiteOpenHelper.java:133)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:282)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:175)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:93)
at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
at android.arch.persistence.room.testing.MigrationTestHelper.openDatabase(MigrationTestHelper.java:203)
at android.arch.persistence.room.testing.MigrationTestHelper.runMigrationsAndValidate(MigrationTestHelper.java:193)

我正在使用(versions.arch是 1.0.0):

implementation "android.arch.persistence.room:runtime:${versions.arch}"
annotationProcessor "android.arch.persistence.room:compiler:${versions.arch}"
androidTestImplementation "android.arch.persistence.room:testing:${versions.arch}"

哦,好吧。原来我很愚蠢。

public static final Migration MIGRATION_9_10 = new Migration(9, 10) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
database.beginTransaction();
database.execSQL("ALTER TABLE DeclarationEntity ADD latitude REAL NOT NULL DEFAULT 0.0");
database.execSQL("ALTER TABLE DeclarationEntity ADD longitude REAL NOT NULL DEFAULT 0.0");
database.setTransactionSuccessful(); // <--- TA-DAAAH
database.endTransaction();
}
};

框架确实运行了升级,但将其回滚。

最新更新