Skip to content

Instantly share code, notes, and snippets.

@Pekwerike
Created August 4, 2020 12:59
Show Gist options
  • Save Pekwerike/a5e288c56d125c371b9b6e5e434934f4 to your computer and use it in GitHub Desktop.
Save Pekwerike/a5e288c56d125c371b9b6e5e434934f4 to your computer and use it in GitHub Desktop.
val MIGRATION_3_4 = object : Migration(3, 4){
override fun migrate(database: SupportSQLiteDatabase) {
//create new table
database.execSQL("CREATE TABLE `new_course_table`(`course_index_key` INTEGER NOT NULL," +
" `course_code` TEXT NOT NULL," +
" `course_title` TEXT NOT NULL, " +
"`course_unit` INTEGER NOT NULL," +
" `course_status` TEXT," +
" `course_date_added` INTEGER," +
" PRIMARY KEY(`course_index_key`) )")
//insert data from old table into new table
database.execSQL("INSERT INTO new_course_table(course_code," +
" course_title," +
" course_unit," +
" course_status," +
" course_date_added)" +
" SELECT course_code, course_title, course_unit, course_status, course_date_added FROM course_table")
//drop old table
database.execSQL("DROP TABLE course_table")
//rename new table to the old table name
database.execSQL("ALTER TABLE new_course_table RENAME TO course_table")
}
}
@Pekwerike
Copy link
Author

Handling RoomDb Migration: Create a new primary key column in an existing entity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment