Android 文件室新功能自动迁移错误:不兼容类型无法转换为列表<;迁移>;
我尝试使用Room自动迁移功能(),但当我尝试创建自动测试时,这不起作用。因此,我在TaskEntity的新类别列中添加了Android 文件室新功能自动迁移错误:不兼容类型无法转换为列表<;迁移>;,android,android-room,auto-migration,Android,Android Room,Auto Migration,我尝试使用Room自动迁移功能(),但当我尝试创建自动测试时,这不起作用。因此,我在TaskEntity的新类别列中添加了 @Entity(tableName = "tasks") data class TaskEntity( @PrimaryKey val id: String, val title: String, val shortDescription: String, val fullDescription: String,
@Entity(tableName = "tasks")
data class TaskEntity(
@PrimaryKey
val id: String,
val title: String,
val shortDescription: String,
val fullDescription: String,
val deadline: Date?,
// This is new column
@ColumnInfo(defaultValue = "DEFAULT")
val category: TaskCategory
)
enum class TaskCategory {
WORK, SPORT, SHOPPING, EDUCATION, DEFAULT
}
在此之后,我更新了数据库,如下所示:
@Database(
version = 3,
entities = [TaskEntity::class],
autoMigrations = [AutoMigration(from = 2, to = 3)],
exportSchema = true
)
@TypeConverters(DateConverter::class)
abstract class ToDoDatabase : RoomDatabase() {
companion object {
fun newTestDatabase(context: Context) = Room.inMemoryDatabaseBuilder(
context,
ToDoDatabase::class.java
)
.addMigrations(MIGRATION_1_2)
.build()
}
abstract fun tasks(): TodoDao
}
val db = migrationTestHelper.runMigrationsAndValidate(
DB_NAME,
2,
true,
MIGRATION_1_2
)
其中3-是新版本,自动迁移(from=2,to=3)是新的自动迁移
我以前的迁徙
@VisibleForTesting
internal val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE tasks ADD COLUMN deadline INT")
}
}
当我运行androidTest时,我得到了这样的错误:
error: incompatible types: List<ToDoDatabase_AutoMigration_2_3_Impl> cannot be converted to List<Migration>
return Arrays.asList(new ToDoDatabase_AutoMigration_2_3_Impl());
}
还有一个问题是,我应该向migrationTestHelper.runMigrationsAndValidate添加什么?我试过这个
val db = migrationTestHelper.runMigrationsAndValidate(
DB_NAME,
3,
true,
ToDoDatabase_AutoMigration_2_3_Impl()
)
我添加了todatabase\u AutoMigration\u 2\u 3\u Impl(),但仍然不起作用。当我使用手动迁移时,看起来是这样的:
@Database(
version = 3,
entities = [TaskEntity::class],
autoMigrations = [AutoMigration(from = 2, to = 3)],
exportSchema = true
)
@TypeConverters(DateConverter::class)
abstract class ToDoDatabase : RoomDatabase() {
companion object {
fun newTestDatabase(context: Context) = Room.inMemoryDatabaseBuilder(
context,
ToDoDatabase::class.java
)
.addMigrations(MIGRATION_1_2)
.build()
}
abstract fun tasks(): TodoDao
}
val db = migrationTestHelper.runMigrationsAndValidate(
DB_NAME,
2,
true,
MIGRATION_1_2
)