0phelia
12/4/2019 - 3:01 PM

Room Db Migration

// add to app build file
 kapt {
        arguments {
            arg("room.schemaLocation", "$projectDir/schemas".toString())
        }
    }
    
// set expostSchema to true#
...exportSchema = true

// copypaste new scheme and add to migration variable
val MIGRATION_1_2: Migration = object : Migration(1, 2) {
    override fun migrate(database: SupportSQLiteDatabase) {
        database.execSQL("CREATE TABLE IF NOT EXIS..........")
    }
}

// provide migatin variable to DB
fun getInstance(context: Context): AppDb? {
            if (sInstance == null) {
                synchronized(LOCK) {
                    sInstance = Room.databaseBuilder(
                        context.applicationContext,
                        AppDb::class.java, DbConfig.DB_NAME
                    )
                    .addMigrations(MIGRATION_1_2)
                    .build()
                }
            }
            return sInstance
        }