Data layer: Room entities, DAOs, database with exported schema
Milestone 2. Medication / DoseTime / DoseLog per the brief's model. DoseLog.doseTimeId is SET_NULL (schedule edits must not erase adherence history) while medId CASCADEs; DoseLog.amount is snapshotted at logging time so later dose changes don't rewrite the record. Inventory decrement is a single SQL UPDATE clamped at 0 to stay race-free under escalating notifications. Enums stored as TEXT; schema exported to app/schemas as the migration baseline. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
7256c49112
commit
b884885e40
10 changed files with 614 additions and 2 deletions
38
app/src/main/java/no/naiv/meddetsamme/data/MedDatabase.kt
Normal file
38
app/src/main/java/no/naiv/meddetsamme/data/MedDatabase.kt
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package no.naiv.meddetsamme.data
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
|
||||
/**
|
||||
* exportSchema=true with schemas/ committed: this DB holds a medical record,
|
||||
* so every future schema change ships as a tested Migration against the
|
||||
* exported baseline — never destructive fallback.
|
||||
*
|
||||
* Enums are stored as TEXT (Room's built-in enum converter); all other columns
|
||||
* are primitives, so no TypeConverters are needed yet.
|
||||
*/
|
||||
@Database(
|
||||
entities = [Medication::class, DoseTime::class, DoseLog::class],
|
||||
version = 1,
|
||||
exportSchema = true,
|
||||
)
|
||||
abstract class MedDatabase : RoomDatabase() {
|
||||
abstract fun medicationDao(): MedicationDao
|
||||
abstract fun doseTimeDao(): DoseTimeDao
|
||||
abstract fun doseLogDao(): DoseLogDao
|
||||
|
||||
companion object {
|
||||
@Volatile private var instance: MedDatabase? = null
|
||||
|
||||
fun get(context: Context): MedDatabase =
|
||||
instance ?: synchronized(this) {
|
||||
instance ?: Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
MedDatabase::class.java,
|
||||
"med-det-samme.db",
|
||||
).build().also { instance = it }
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue