1

So I am implementing Room in an app which uses SQLiteOpenHelper. I only want to migrate 1 of 15 tables over to Room. This works fine if the table already exists, I can use the same database name in Room and as long as the columns and column types are the same in the entity class the data can be accessed with Room and everything works. However if I install the new app as a fresh install then it will crash when I try to access the table with the following error:

java.lang.IllegalStateException: Pre-packaged database has an invalid schema: footable((packagepath).FooTable).
 Expected:
TableInfo{name='footable', columns={description=Column{name='description'}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='footable', columns={}, foreignKeys=[], indices=[]}

I thought Room would be able to create this table in the same database but guess not. How to fix this? (It works if I use a different database name but I want to migrate the data over)

Also, do I need to rewrite the SQLiteOpenHelper implementation to SupportSqliteOpenHelper and how do I even do that? The table is only going to be accessed with Room. I cannot find any documentation on SupportSqliteOpenHelper aside from a brief mention in an old blog post but my guess is this isn't my issue?: https://medium.com/androiddevelopers/incrementally-migrate-from-sqlite-to-room-66c2f655b377

Here is my Database class(49 is the current version so I upped it to 50 when implementing Room):

@Database(entities = [FooTable::class], version = 50)
abstract class ExampleDatabase : RoomDatabase() {

    abstract fun fooTableDao(): FooTableDao

    companion object {
        @Volatile
        private var instance: ExampleDatabase? = null

        @Synchronized
        fun getInstance(context: Context): ExampleDatabase =
                instance ?: Room.databaseBuilder(context.applicationContext,
                        ExampleDatabase::class.java, "example.db")
                        .addMigrations(MIGRATION_49_50)
                        .build().also { instance = it }

        val MIGRATION_49_50 = object : Migration(49, 50) {
            override fun migrate(database: SupportSQLiteDatabase) {}
        }
    }
}

I also tried adding a callback to the Room builder and override onCreate where I would create the table if it didn't exist but the callback is never called.

I can get this to work if I before using Room create the table with SqliteOpenHelper in case it does not exist, but this seems really hacky, isn't there a better way?

axlrtr
  • 535
  • 6
  • 23

0 Answers0