测试房间迁移时身份哈希检查失败
Identity hash check failed when testing Room migrations
我正在编写 these instructions 之后的测试。我的测试 class 有这个规则:
@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 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.
这是错误的。我已经追踪到发生了什么:
- 使用版本 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
是从DB加载的,是9版本的;而mIdentityHash
是runMigrationsAndValidate
使用version参数直接加载的,就是10.
当然失败了。
我想知道为什么它在执行迁移之前检查身份哈希。
这是迁移,即使我认为它与这里无关:
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();
}
};
框架进行了 运行 升级,但回滚了。
我正在编写 these instructions 之后的测试。我的测试 class 有这个规则:
@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 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.
这是错误的。我已经追踪到发生了什么:
- 使用版本 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
是从DB加载的,是9版本的;而mIdentityHash
是runMigrationsAndValidate
使用version参数直接加载的,就是10.
当然失败了。
我想知道为什么它在执行迁移之前检查身份哈希。
这是迁移,即使我认为它与这里无关:
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();
}
};
框架进行了 运行 升级,但回滚了。