2

I have room setup on production app. I have made many schema related changes to multiple tables in my app.

Before shipping to production build, I have made sure of the following

  1. Increase database version
  2. have exportSchema = false in @Database annotation line
  3. allowBackup to false in manifest <application android:allowBackup="false" />
  4. have .fallbackToDestructiveMigration() while creating database
  5. have .fallbackToDestructiveMigrationOnDowngrade() while creating database
  6. have .enableMultiInstanceInvalidation() while creating database

App crashes with the following stacktrace.

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.

For development I can clear app cache and data, but how to fix this in production? I do not wish to write migration steps as there are multiple changes in schema and tables.

I have read excellent article, but it's not helpful. What are my options?

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)
halfer
  • 19,824
  • 17
  • 99
  • 186
Rinav
  • 2,527
  • 8
  • 33
  • 55

1 Answers1

1

I solved the Room migration issue using the below method.

 Room.databaseBuilder(context, AppDatabase::class.java, "xxx.db")
        .fallbackToDestructiveMigration()
        .fallbackToDestructiveMigrationOnDowngrade()
        .fallbackToDestructiveMigrationFrom(16, 17, 18, 19)
        .build() 

Had to add fallbackToDestructiveMigrationFrom() method which forced dropping tables and recreating tables with new schema.

So varargs of db version eg 16, 17, 18, 19 will recreate db for these version numbers for method fallbackToDestructiveMigrationFrom(...)

Not sure why this method works butfallbackToDestructiveMigration() does not work.

Why is this soo weird?

Rinav
  • 2,527
  • 8
  • 33
  • 55