Skip to content

Instantly share code, notes, and snippets.

@mathemandy
Last active January 26, 2022 13:35
Show Gist options
  • Save mathemandy/8cca63ea3e7b24c405505be73cff857d to your computer and use it in GitHub Desktop.
Save mathemandy/8cca63ea3e7b24c405505be73cff857d to your computer and use it in GitHub Desktop.
val MIGRATION_8_9 = object : Migration(8, 9) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("CREATE TABLE `savedAlbums_new` (`entityOwnerId` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `id` TEXT NOT NULL, `artwork` TEXT, `artist` TEXT, `description` TEXT, `album_genre` TEXT, `name` TEXT, `presave` INTEGER NOT NULL, `artist_name` TEXT, `label` TEXT, `created` INTEGER NOT NULL, `modified` INTEGER NOT NULL, `releasedate` INTEGER NOT NULL, `entityOwner` TEXT, `uniqueId` TEXT, `indexInQueue` INTEGER NOT NULL default -1, `sharelink` TEXT)")
database.execSQL("CREATE UNIQUE INDEX `index_savedAlbums_uniqueId` ON `savedAlbums_new` (`uniqueId`)")
database.execSQL("INSERT INTO `savedAlbums_new` (`id` , `artwork`, `artist` , `description` , `album_genre`, `name` , `presave` , `artist_name`, `label` , `created` , `modified` , `releasedate`, `entityOwner`) select `id` , `artwork`, `artist` , `description` , `album_genre`, `name` , `presave` , `artist_name`, `label` , `created` , `modified` , `releasedate`, `entityOwner` from savedAlbums ")
database.execSQL(" DROP TABLE savedAlbums ")
database.execSQL("ALTER TABLE savedAlbums_new RENAME TO savedAlbums")
}
....
....
Room.databaseBuilder(androidApplication(), xxxDatabase::class.java, "xxxDabase")
.addMigrations(MIGRATION_8_9)
.build()
@mathemandy
Copy link
Author

Steps:

  1. Create a new table with the new structure
  2. Copy the data from the old table into the new table
  3. Drop the old table
  4. Rename the new table to match the old table

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