fix: correct Room migration - use DEFAULT '' to match entity schema
Migration 1→2 had DEFAULT NULL but @ColumnInfo(defaultValue="") expects DEFAULT ''. Room schema validation failed on startup. Since SQLite can't alter column defaults, add migration 2→3 that recreates the alarms table with the correct DEFAULT '' for the date column.
This commit is contained in:
parent
c78cb8a77d
commit
f498ecdef4
2 changed files with 28 additions and 3 deletions
|
|
@ -5,16 +5,41 @@ import androidx.room.RoomDatabase
|
|||
import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
@Database(entities = [AlarmEntity::class], version = 2, exportSchema = true)
|
||||
@Database(entities = [AlarmEntity::class], version = 3, exportSchema = true)
|
||||
abstract class AlarmDatabase : RoomDatabase() {
|
||||
abstract fun alarmDao(): AlarmDao
|
||||
|
||||
companion object {
|
||||
const val NAME = "helios_alarms.db"
|
||||
|
||||
// Fresh install path: add date column with correct default
|
||||
val MIGRATION_1_2 = object : Migration(1, 2) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("ALTER TABLE alarms ADD COLUMN date TEXT DEFAULT NULL")
|
||||
db.execSQL("ALTER TABLE alarms ADD COLUMN date TEXT DEFAULT ''")
|
||||
}
|
||||
}
|
||||
|
||||
// Fix path: first install had DEFAULT NULL instead of DEFAULT ''
|
||||
// SQLite can't alter column defaults, so recreate the table
|
||||
val MIGRATION_2_3 = object : Migration(2, 3) {
|
||||
override fun migrate(db: SupportSQLiteDatabase) {
|
||||
db.execSQL("""
|
||||
CREATE TABLE alarms_new (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
hour INTEGER NOT NULL,
|
||||
minute INTEGER NOT NULL,
|
||||
label TEXT NOT NULL,
|
||||
triggerTimeMillis INTEGER NOT NULL,
|
||||
date TEXT DEFAULT ''
|
||||
)
|
||||
""".trimIndent())
|
||||
db.execSQL("""
|
||||
INSERT INTO alarms_new (id, hour, minute, label, triggerTimeMillis, date)
|
||||
SELECT id, hour, minute, label, triggerTimeMillis, COALESCE(date, '')
|
||||
FROM alarms
|
||||
""".trimIndent())
|
||||
db.execSQL("DROP TABLE alarms")
|
||||
db.execSQL("ALTER TABLE alarms_new RENAME TO alarms")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ object AppModule {
|
|||
AlarmDatabase::class.java,
|
||||
AlarmDatabase.NAME
|
||||
)
|
||||
.addMigrations(AlarmDatabase.MIGRATION_1_2)
|
||||
.addMigrations(AlarmDatabase.MIGRATION_1_2, AlarmDatabase.MIGRATION_2_3)
|
||||
.build()
|
||||
|
||||
@Provides
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue