Skip to content

Instantly share code, notes, and snippets.

@rnpy
Created September 6, 2018 01:03
Show Gist options
  • Save rnpy/a4b313d1cf7f1201fb74d2903bf0dd6b to your computer and use it in GitHub Desktop.
Save rnpy/a4b313d1cf7f1201fb74d2903bf0dd6b 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