package com.miya.fastcashier.dao

import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.Room
import com.miya.fastcashier.utils.ContextUtils
import androidx.sqlite.db.SupportSQLiteDatabase

import androidx.room.migration.Migration

@Database(entities = [ViewPayOrderData::class], exportSchema = false, version = 2)
abstract class PayDatabase : RoomDatabase() {
    abstract fun payDataDao(): PayDataDao
}

class DatabaseKeeper {

    companion object {
        private val DATABASE_NAME = "pay_data"

        val payDatabase: PayDatabase by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            Room.databaseBuilder(ContextUtils.getContext(), PayDatabase::class.java, DATABASE_NAME)
                .addMigrations(MIGRATION_1_2)
                .build()
        }

        val MIGRATION_1_2: Migration = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("alter table pay_data add userName Text")
            }
        }
    }
}
