我正在使用room数据库来创建本地存储,我尝试迁移数据库,但在调用迁移函数多次时出现问题。
以下是我的代码片段。
以下是我的代码片段。
class DbInstance {
companion object {
private var db: AppDatabase? = null
fun getDbInstance(context: Context): AppDatabase {
if (db == null)
db = Room.databaseBuilder(
context,
AppDatabase::class.java, "mydb"
)
.addMigrations(MIGRATION_1_2, MIGRATION_2_3)
.build()
return db!!
}
private val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
}
}
private val MIGRATION_2_3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
println("------> called for MIGRATION_2_3")
database.execSQL(
"Alter table PaymentDB add column ui_name text"
)
}
}
}
}
@Database(entities = arrayOf(PaymentDB::class), version = 3)
@TypeConverters(DbTypeConverter::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun paymentDao(): PaymentDao
}
在哪里println("------> called for MIGRATION_2_3")
被多次打印了两次。
Room.databaseBuilder(context.applicationContext, ...
- Shreyash.K