0

I have an DB migration:

val MIGRATION_8_9 = object : Migration(8, 9) {
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("ALTER TABLE RideEntity RENAME frontVideoPresent TO frontVideoState")
            database.execSQL("ALTER TABLE RideEntity RENAME rearVideoPresent TO rearVideoState")
  }
}

When testing this migration on local Samsung phones it worked fine. The in production with help of craslytics I seen this crash:

Fatal Exception: java.lang.RuntimeException
Exception while computing database live data.

Caused by android.database.sqlite.SQLiteException
near "frontVideoPresent": syntax error (Sqlite code 1 SQLITE_ERROR): , while 
compiling: ALTER TABLE RideEntity RENAME frontVideoPresent TO frontVideoState, (OS 
error - 11:Try again)

This is happened on Huawei Mate 20 phone. How to understand better this crash? This is OS related? I can not remove the rename now, because many users that updated the app the column renaming worked, but users with Huawei phones may suffer this crash. I am open to your suggestions...

Dim
  • 4,527
  • 15
  • 80
  • 139

1 Answers1

2

Looks like version of sqlite on some devices doesn't support column renaming because android app use build-in version on sqlite library to android OS. That's why version of sqlite depends on android's api level version (where app running). According to sqlite release notes (paragraph 2) the support for renaming columns was added in version 3.25.0 and according to google docs (and other answer on stackoverflow)the column's renaming on android supports since android api level 30.

To solve the problem of fragmentation of slqlite library you can use android-requery which allows to use last version of sqlile in all android versions(since API level 14). It's easy to use this library with room.

Artem Viter
  • 804
  • 7
  • 12
  • Thank you for the answer. I've added "openHelperFactory(RequerySQLiteOpenHelperFactory())". But how do I adopt the migration? The override fun migrate(database: SupportSQLiteDatabase) expects interface SupportSQLiteDatabase from androidx.sqlite.db – Dim Jun 17 '21 at 20:37
  • @Dim RequerySQLiteOpenHelperFactory creates `androidx.sqlite.db. SupportSQLiteDatabase` and migration should be works fine - out of box . – Artem Viter Jun 17 '21 at 20:52
  • @Dim Do you have some problems with it ? – Artem Viter Jun 17 '21 at 20:52
  • So I just need to add ".openHelperFactory(RequerySQLiteOpenHelperFactory())" and keep the "import androidx.sqlite.db.SupportSQLiteDatabase"? – Dim Jun 17 '21 at 21:07
  • Thank you so much! – Dim Jun 17 '21 at 21:13
  • @Dim If my answer helped you to solve your problem , please mark it as accepted . – Artem Viter Jun 17 '21 at 21:15