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
|
2019-09-23 17:23:59 +02:00
|
|
|
import com.denizd.substitutionplan.models.Substitution
|
2019-05-26 18:40:02 +02:00
|
|
|
|
2019-09-23 22:25:05 +02:00
|
|
|
@Database(entities = [Substitution::class, Food::class], version = 8, 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) {
|
2019-09-11 20:59:55 +02:00
|
|
|
database.execSQL("ALTER TABLE subst_table ADD COLUMN teacher TEXT NOT NULL DEFAULT ''")
|
2019-09-04 18:46:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-09-13 12:03:14 +02:00
|
|
|
private val addTypeColumn = object : Migration(6, 7) {
|
|
|
|
|
override fun migrate(database: SupportSQLiteDatabase) {
|
|
|
|
|
database.execSQL("ALTER TABLE subst_table ADD COLUMN type TEXT NOT NULL DEFAULT ''")
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-23 22:25:05 +02:00
|
|
|
private val addDatePriorityColumn = object : Migration(7, 8) {
|
|
|
|
|
override fun migrate(database: SupportSQLiteDatabase) {
|
|
|
|
|
database.execSQL("ALTER TABLE subst_table ADD COLUMN date_priority INTEGER NOT NULL DEFAULT 0")
|
|
|
|
|
}
|
|
|
|
|
}
|
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")
|
2019-09-23 22:25:05 +02:00
|
|
|
.addMigrations(addTeacherColumn, addTypeColumn, addDatePriorityColumn)
|
2019-09-08 21:07:20 +02:00
|
|
|
.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")
|
2019-09-13 12:03:14 +02:00
|
|
|
.fallbackToDestructiveMigrationFrom(5, 6)
|
2019-09-08 21:07:20 +02:00
|
|
|
.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
|
|
|
}
|
|
|
|
|
}
|