我在生产应用中设置了房间。 我对应用程序中的多个表进行了许多与架构相关的更改。
在交付生产版本之前,我已经确定以下内容
exportSchema = false
注释行中有@Database
<application android:allowBackup="false" />
中备份为false .fallbackToDestructiveMigration()
.fallbackToDestructiveMigrationOnDowngrade()
.enableMultiInstanceInvalidation()
应用程序崩溃并显示以下堆栈跟踪信息。
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.
对于开发,我可以清除应用程序缓存和数据,但是如何在生产中解决此问题? 我不希望编写迁移步骤,因为架构和表中有多个更改。
我已经读过excellent article,但这没有帮助。我有什么选择?
stacktrace
Fatal Exception: java.lang.RuntimeException: Exception while computing database live data.
at androidx.room.RoomTrackingLiveData$1.run + 92(RoomTrackingLiveData.java:92)
at java.util.concurrent.ThreadPoolExecutor.runWorker + 1167(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run + 641(ThreadPoolExecutor.java:641)
at java.lang.Thread.run + 764(Thread.java:764)
////////////////////////////
Caused by java.lang.IllegalStateException: A migration from 19 to 20 was required but not found. Please provide the necessary Migration path via RoomDatabase.Builder.addMigration(Migration ...) or allow for destructive migrations via one of the RoomDatabase.Builder.fallbackToDestructiveMigration* methods.
at androidx.room.RoomOpenHelper.onUpgrade + 101(RoomOpenHelper.java:101)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade + 124(FrameworkSQLiteOpenHelper.java:124)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked + 398(SQLiteOpenHelper.java:398)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase + 298(SQLiteOpenHelper.java:298)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase + 92(FrameworkSQLiteOpenHelper.java:92)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase + 53(FrameworkSQLiteOpenHelper.java:53)
at androidx.room.RoomDatabase.inTransaction + 452(RoomDatabase.java:452)
at androidx.room.RoomDatabase.assertNotSuspendingTransaction + 275(RoomDatabase.java:275)
at androidx.room.RoomDatabase.query + 304(RoomDatabase.java:304)
at androidx.room.util.DBUtil.query + 54(DBUtil.java:54)
at com.xxx.data.local.banner.BannerDao_Impl$4.call + 218(BannerDao_Impl.java:218)
at com.xxx.data.local.banner.BannerDao_Impl$4.call + 215(BannerDao_Impl.java:215)
at androidx.room.RoomTrackingLiveData$1.run + 90(RoomTrackingLiveData.java:90)
at java.util.concurrent.ThreadPoolExecutor.runWorker + 1167(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run + 641(ThreadPoolExecutor.java:641)
at java.lang.Thread.run + 764(Thread.java:764)
答案 0 :(得分:1)
我使用以下方法解决了会议室迁移问题。
Room.databaseBuilder(context, AppDatabase::class.java, "xxx.db")
.fallbackToDestructiveMigration()
.fallbackToDestructiveMigrationOnDowngrade()
.fallbackToDestructiveMigrationFrom(16, 17, 18, 19)
.build()
必须添加fallbackToDestructiveMigrationFrom()
方法,该方法强制删除表并使用新模式重新创建表。
因此,数据库版本为16, 17, 18, 19
的varargs将为方法fallbackToDestructiveMigrationFrom(...)
的这些版本号重新创建db
不确定该方法为何有效,但fallbackToDestructiveMigration()
不起作用。
为什么这么奇怪?