[英]Room migration testing MigrationTestHelper version
Well, i am trying to test my database migration.好吧,我正在尝试测试我的数据库迁移。 Unfortunatly something looks like wrong.
不幸的是,有些事情看起来不对劲。
@RunWith(AndroidJUnit4ClassRunner.class)
public class MigrationTest {
private static final String TEST_DB = "migration-test";
@Rule
public MigrationTestHelper helper;
public MigrationTest() {
helper = new MigrationTestHelper(InstrumentationRegistry.getInstrumentation(),
AppDatabase.class.getCanonicalName(),
new FrameworkSQLiteOpenHelperFactory());
}
@Test
public void migrateAll() throws IOException {
// Create earliest version of the database.
SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);
db.close();
// Open latest version of the database. Room will validate the schema
// once all migrations execute.
AppDatabase appDb = Room.databaseBuilder(
InstrumentationRegistry.getInstrumentation().getTargetContext(),
AppDatabase.class,
TEST_DB)
.addMigrations(ALL_MIGRATIONS).build();
appDb.getOpenHelper().getWritableDatabase();
appDb.close();
}
// Array of all migrations
private static final Migration[] ALL_MIGRATIONS = new Migration[]{MIGRATION_1_2};
}
Migration code.迁移代码。
public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE mytable ADD COLUMN reference_code TEXT");
}
};
All is working fine with real migration but in junit test case i have the following error.真正的迁移一切正常,但在 junit 测试用例中我有以下错误。
E/SQLiteLog: (1) duplicate column name: reference_code
E/TestRunner: failed: migrateAll(com.apps.MigrationTest)
E/TestRunner: ----- begin exception -----
E/TestRunner: android.database.sqlite.SQLiteException: duplicate column name: reference_code (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE mytable ADD COLUMN reference_code TEXT
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
at a
As i understand, it looks like SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);
据我了解,它看起来像
SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);
is creating the schema V2 of my database (and not the version 1).正在创建我的数据库的架构 V2(而不是版本 1)。 As a result the new column is taggued as duplicate.
结果,新列被标记为重复。
To fix it i have to rollback my version = 1
to @Database
class and then start my junit test again.要修复它,我必须将我的
version = 1
回滚到@Database
class,然后再次开始我的 junit 测试。
Can anyone help me on it?有人可以帮我吗?
I follow the google guide here: https://developer.android.com/training/data-storage/room/migrating-db-versions.html我在这里遵循谷歌指南: https://developer.android.com/training/data-storage/room/migrating-db-versions.ZFC35FDC70D5FC69D269883A822C7A53E
Well, i finally found it.嗯,我终于找到了。 It looks like a wrong schema has been generated in my assets folder.
看起来我的资产文件夹中生成了错误的架构。
To fix the issue, here is what i did.为了解决这个问题,这就是我所做的。
Here is the difference bewteen my java object and database version 1 et 2这是我的 java object 和数据库版本 1 和 2 之间的区别
@Entity(tableName = "Table")
public class Table implements Parcelable {
@ColumnInfo(name = COLUMN_REFERENCE_CODE)
private String referenceCode;
}
Hope this will help.希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.