π₯ Android Room μμ version Migration μ΄ νμν λμ μ¬μ©λ²μ μμ보μ.
μ΄λ²μ google play μ μ λ‘λν μ΄ν리μΌμ΄μ μ μ λ°μ΄νΈνκ² λμλλ°, μ½λ μμ μ€, DataBase μ κ΅¬μ‘°κ° λ³κ²½λλ μΌμ΄ λ°μνμλ€.
κ°λ° μ€μ΄μλ€λ©΄, μλ κΈμμ μμ±νλ κ²μ²λΌ μΊμ νμΌμ μμ νλκ° μ± μ체λ₯Ό μμ νκ³ , google λλΌμ΄λΈ μΊμμ κ°μ΄ μ μ₯λμ§ μλλ‘ μ€μ νμμ κ²μ΄λ€.
https://velog.io/@pass/Android-Room-cannot-verify-the-data-integrity
νμ§λ§, Google Play μμ μ΄λ―Έ λ€μ΄λ‘λ λ°μ μ¬μ©μκ° μ
λ°μ΄νΈ νμ λ°λ‘ μ¬μ©μ΄ κ°λ₯ν΄μΌ νλ―λ‘ Database μ version μ μ¬λ¦¬κΈ°λ‘ νμλ€.
μλλ μμμ½λμ΄λ©°, version μ 2μμ 3μΌλ‘ migration ν μμ μ΄λ€.
onOff μμ± νμ
λ§ λ³κ²½λμμΌλ©°, μλ‘μ΄ ν
μ΄λΈμ μμ±νμ¬ λ체νλ λ°©λ²μΌλ‘ μννμλ€.
@Database(entities = [Alarm::class], version = 3)
abstract class AlarmDataBase : RoomDatabase() {
abstract fun alarmDao(): AlarmDao
companion object {
@Volatile
private var INSTANCE: AlarmDataBase? = null
fun getInstance(context: Context): AlarmDataBase =
INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
}
private fun buildDatabase(context: Context) =
Room.databaseBuilder(
context.applicationContext,
AlarmDataBase::class.java, Constants.ALARM_DB
)
.addMigrations(MIGRATION_2_3)
.build()
private val MIGRATION_2_3: Migration = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
// Create the new table
database.execSQL("""
CREATE TABLE Alarm_new (
id INTEGER PRIMARY KEY AUTOINCREMENT,
alarmHour TEXT NOT NULL,
alarmMinute TEXT NOT NULL,
onOff INTEGER NOT NULL DEFAULT 0,
playlistId INTEGER NOT NULL,
playlistName TEXT NOT NULL
)
""")
// Copy the data
database.execSQL("""
INSERT INTO Alarm_new (id, alarmHour, alarmMinute, onOff, playlistId, playlistName)
SELECT id, alarmHour, alarmMinute,CASE WHEN onOff > 0 THEN 1 ELSE 0 END AS onOff ,playlistId ,playlistName FROM Alarm
""")
// Remove the old table
database.execSQL("DROP TABLE Alarm")
// Change the table name to the correct one
database.execSQL("ALTER TABLE Alarm_new RENAME TO Alarm")
}
}
}
}