Skip to content

Instantly share code, notes, and snippets.

@harryhan24
Forked from rnpy/sqlite_to_room_db_init.kt
Created October 12, 2018 14:40
Show Gist options
  • Save harryhan24/0ee1c60fcef57052869d562008cc0439 to your computer and use it in GitHub Desktop.
Save harryhan24/0ee1c60fcef57052869d562008cc0439 to your computer and use it in GitHub Desktop.
private val callback = object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
try {
// First time to database was created, check if user upgraded from a previous
// version of the app that was using ORMLite and migrate data if needed.
val context = TradeMeApp.getContext()
val previousDB = context.applicationContext.getDatabasePath(PREVIOUS_DATABASE_FILE)
if (previousDB.exists()) {
MigrateToRoom(previousDB, context.component.database).execute()
}
} catch (exception: Exception) { /* log error */ }
}
}
@JvmStatic
fun getBuilder(context: Context): RoomDatabase.Builder<Database> {
return Room.databaseBuilder(
context,
Database::class.java,
DATABASE_FILE)
.addMigrations(*migrations)
.addCallback(callback)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment