I have a room database on my App. It’s 3 files: Model, Database and DAO.
I’m add a new val to Model and I update my database version on Database file. And I get a crash.
@Database(entities = [Reminder::class], version = 1)
abstract class ReminderDatabase : RoomDatabase() {
abstract fun userReminderDao(): ReminderDAO
companion object {
private var INSTANCE: ReminderDatabase? = null
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"CREATE TABLE Reminder_2 (" +
"id INTEGER NOT NULL, " +
"title TEXT, " +
"note TEXT, " +
"date TEXT, " +
"PRIMARY KEY(id))"
)
database.execSQL("INSERT INTO Reminder_2 (id, title, note, date) SELECT id, title, note, date FROM Reminder")
database.execSQL("DROP TABLE Reminder")
database.execSQL("ALTER TABLE Reminder_2 RENAME TO Reminder")
}
}
fun getInstance(context: Context): ReminderDatabase {
if (INSTANCE == null) {
synchronized(ReminderDatabase::class) {
INSTANCE = Room.databaseBuilder(
context.applicationContext,
ReminderDatabase::class.java,
"Reminder"
)
.addMigrations(MIGRATION_1_2)
.build()
}
}
return INSTANCE!!
}
}
}
I try the Migrations but I can’t run it. When I open the app, in 2 seconds app get a crash. Please help me.
New contributor
Yahya Melih Koşar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.