gpt4 book ai didi

android - 测试房间迁移时身份哈希检查失败

转载 作者:行者123 更新时间:2023-11-28 20:27:40 28 4
gpt4 key购买 nike

我正在写一个测试 these instructions .我的测试类有这个规则:

@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是从数据库中加载的,是版本9;而mIdentityHashrunMigrationsAndValidate使用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();
}
};

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

关于android - 测试房间迁移时身份哈希检查失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48532760/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com