RadiusNetworks / flybuy-android

FlyBuy SDK for Android
Other
5 stars 0 forks source link

error which results in app crashes after 1.0 to 1.1.11 update #9

Closed hatched-MaciejPrzybylski closed 4 years ago

hatched-MaciejPrzybylski commented 4 years ago

Fatal Exception: java.lang.RuntimeException Exception while computing database live data. androidx.room.RoomTrackingLiveData$1.run

AppDatabase_Impl.java line 180 com.radiusnetworks.flybuy.sdk.data.room.database.AppDatabase_Impl$1.validateMigration

Caused by java.lang.IllegalStateException Migration didn't properly handle beacon_regions(com.radiusnetworks.flybuy.sdk.data.room.domain.BeaconRegion). Expected: TableInfo{name='beacon_regions', columns={id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, major=Column{name='major', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, minor=Column{name='minor', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, uuid=Column{name='uuid', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, order_id=Column{name='order_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[ForeignKey{referenceTable='orders', onDelete='CASCADE', onUpdate='NO ACTION', columnNames=[order_id], referenceColumnNames=[id]}], indices=[]} Found: TableInfo{name='beacon_regions', columns={major=Column{name='major', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, minor=Column{name='minor', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, uuid=Column{name='uuid', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, order_id=Column{name='order_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[ForeignKey{referenceTable='oldOrdersTable', onDelete='CASCADE', onUpdate='NO ACTION', columnNames=[order_id], referenceColumnNames=[id]}], indices=[]}

botnerd commented 4 years ago

Can you please provide the following information so we can look into the issue?

hatched-mariusz commented 4 years ago

targetSdkVersion: 29 compileSdkVersion: 29

Some sample devices: Brand: Google Model: Pixel 4 XL Android: 11

Brand: Google Model: Pixel 3a Android: 11

hatched-mariusz commented 4 years ago

Not sure, but I think maybe the issue is because: in MIGRATION_11_12: database.execSQL("CREATE TABLE beacon_regions (id INTEGER NOT NULL PRIMARY KEY, uuid TEXT NOT NULL, major INTEGER, minor INTEGER, order_id INTEGER NOT NULL, FOREIGN KEY(order_id) REFERENCES orders(id) ON DELETE CASCADE)"); beacon_regions is created with FOREIGN KEY to the orders table. Then in the next migrations like MIGRATION_12_13 database.execSQL("ALTER TABLE orders RENAME TO oldOrdersTable"); the table name changes and maybe foreign key for beacon_regions is updated. https://www.sqlite.org/lang_altertable.html And then db is deleted: database.execSQL("DROP TABLE oldOrdersTable"); And because of that

Migration didn't properly handle beacon_regions: BeaconRegion). Expected: foreignKeys=[ForeignKey{referenceTable='orders', onDelete='CASCADE', onUpdate='NO ACTION', columnNames=[order_id], referenceColumnNames=[id]}], indices=[]}

But Found: foreignKeys=[ForeignKey{referenceTable='oldOrdersTable', onDelete='CASCADE', onUpdate='NO ACTION', columnNames=[order_id], referenceColumnNames=[id]}], indices=[]}

hatched-mariusz commented 4 years ago

Update:

Please adjust and use that code, this code deleting DB and create again when something with migration is wrong:

abstract class AppDatabase : RoomDatabase() {

    companion object {

        private var INSTANCE: AppDatabase? = null

        fun init(context: Context) : AppDatabase {
            INSTANCE = init_(context)
            //force db opening and if it fails, we try to destroy and recreate the db
            try {
                INSTANCE!!.openHelper.writableDatabase
            } catch (e: Exception) {
                Timber.e(e, "Database there was an error during DB opening => trying to destroy and recreate")
                INSTANCE!!.openHelper.close()
                val dbPath = context.getDatabasePath("app_db")
                if (SQLiteDatabase.deleteDatabase(dbPath)) {
                    INSTANCE = init_(context)
                    INSTANCE!!.openHelper.writableDatabase
                }
            }
            return INSTANCE!!
        }

        private fun init_(context: Context): AppDatabase {
            return buildDatabase(context)
        }

        fun getInstance(context: Context): AppDatabase = INSTANCE ?: synchronized(this) {
            INSTANCE ?: init(context).also { INSTANCE = it }
        }

        private fun buildDatabase(context: Context) =
            Room.databaseBuilder(
                context.applicationContext,
                AppDatabase::class.java, "app_db"
            ).addMigrations(MIGRATION_your_migrations)
                .fallbackToDestructiveMigration()
                .build()
botnerd commented 4 years ago

Thank you for the detailed information. We're looking into the issue.

botnerd commented 4 years ago

v1.1.12 has been released that should fix the DB migration issue and implements the code sample you provided to prevent crashes if a future DB migration fails. Please let us know if this fixes the crash, and we will close the issue.

hatched-mariusz commented 4 years ago

well so far, but let's wait another week before the build spreads more

hatched-mariusz commented 4 years ago

ok, looks the issues are solved.

botnerd commented 4 years ago

Thank you for letting us know. I will close the issue.