I drop the table and want to test with it.
MIGRATION_1_2 does drop 'b' table which is associated with BDao. So AppDatabase can't get BDao instance. Also, B::class is removed from entities.
@Database(entities = [A::class/*, B::class*/], version = 2)
abstract class AppDatabase : RoomDatabse() {
abstract aDao: ADao
// abstract bDao: BDao
companion object {
fun getDatabase(context: Context): AppDatabse {
...
Room.databaseBuilder(context.applicationContext,
AppDatabase::class.java, DATABASE_NAME)
.addMigrations(MIGRATION_1_2)
.build()
...
}
val MIGRATION_1_2 = object Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("""
DROP TABLE 'b'
""")
}
}
}
}
Below is the test code. I can get A Dao but can't get B Dao. How to verify b table is dropped?
@RunWith(AndroidJUnit4::class)
class MigrationTest {
@Rule
@JvmField
val helper = MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
AppDatabase::class.java.canonicalName,
FrameworkSQLiteOpenHelperFactory()
)
@Test
fun migrate1To2() {
val db = helper.createDatabase(TEST_DB, 1)
insertAData(db)
isnertBData(db)
db.close()
helper.runMigrationsAndValidate(TEST_DB, 2, true, AppDatabase.MIGRATION_1_2)
helper.closeWhenFinished(database)
// I can test with A.
val adao = database.aDao()
// But I can't test with B.
// val bdao = database.bDao()
}
}