2019-09-01 11:54:12 +02:00
|
|
|
package com.denizd.substitutionplan.database
|
2019-05-26 18:40:02 +02:00
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import androidx.room.Database
|
|
|
|
|
import androidx.room.Room
|
|
|
|
|
import androidx.room.RoomDatabase
|
2019-09-04 18:46:25 +02:00
|
|
|
import androidx.room.migration.Migration
|
|
|
|
|
import androidx.sqlite.db.SupportSQLiteDatabase
|
2019-09-01 11:54:12 +02:00
|
|
|
import com.denizd.substitutionplan.models.Food
|
|
|
|
|
import com.denizd.substitutionplan.models.Subst
|
2019-05-26 18:40:02 +02:00
|
|
|
|
2019-09-04 18:46:25 +02:00
|
|
|
@Database(entities = [Subst::class, Food::class], version = 6, exportSchema = false)
|
2019-09-01 11:54:12 +02:00
|
|
|
internal abstract class SubstDatabase : RoomDatabase() {
|
2019-05-26 18:40:02 +02:00
|
|
|
|
|
|
|
|
abstract fun substDao(): SubstDao
|
|
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
|
|
private var instance: SubstDatabase? = null
|
2019-09-04 18:46:25 +02:00
|
|
|
private val addTeacherColumn = object : Migration(5, 6) {
|
|
|
|
|
override fun migrate(database: SupportSQLiteDatabase) {
|
|
|
|
|
database.execSQL(
|
|
|
|
|
"ALTER TABLE subst_table ADD COLUMN teacher TEXT NOT NULL DEFAULT ''")
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-26 18:40:02 +02:00
|
|
|
|
|
|
|
|
fun getInstance(context: Context): SubstDatabase? {
|
|
|
|
|
if (instance == null) {
|
|
|
|
|
synchronized (SubstDatabase::class) {
|
|
|
|
|
instance = Room.databaseBuilder(context.applicationContext,
|
2019-09-08 21:07:20 +02:00
|
|
|
SubstDatabase::class.java,
|
|
|
|
|
"subst_database")
|
|
|
|
|
.addMigrations(addTeacherColumn)
|
|
|
|
|
.fallbackToDestructiveMigration()
|
|
|
|
|
.build()
|
2019-05-26 18:40:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return instance
|
|
|
|
|
}
|
2019-07-06 14:18:59 +02:00
|
|
|
|
2019-08-21 10:26:54 +02:00
|
|
|
private var foodInstance: SubstDatabase? = null
|
|
|
|
|
|
2019-07-06 14:18:59 +02:00
|
|
|
fun getFoodInstance(context: Context): SubstDatabase? {
|
2019-08-21 10:26:54 +02:00
|
|
|
if (foodInstance == null) {
|
2019-07-06 14:18:59 +02:00
|
|
|
synchronized (SubstDatabase::class) {
|
2019-08-21 10:26:54 +02:00
|
|
|
foodInstance = Room.databaseBuilder(context.applicationContext,
|
2019-09-08 21:07:20 +02:00
|
|
|
SubstDatabase::class.java,
|
|
|
|
|
"food_database")
|
|
|
|
|
.fallbackToDestructiveMigration()
|
|
|
|
|
.build()
|
2019-07-06 14:18:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-08-21 10:26:54 +02:00
|
|
|
return foodInstance
|
2019-07-06 14:18:59 +02:00
|
|
|
}
|
2019-05-26 18:40:02 +02:00
|
|
|
}
|
|
|
|
|
}
|