Skip to content

Instantly share code, notes, and snippets.

@julioz
Last active June 30, 2020 16:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save julioz/fe8689de278e80d4c9d95ec09fd1beb0 to your computer and use it in GitHub Desktop.
Migration: copying data, rename table and relax FK constraints
database.execSQL("""
CREATE TABLE IF NOT EXISTS TrackCreator_temp (
`track_id` TEXT NOT NULL,
`user_id` TEXT NOT NULL,
PRIMARY KEY(`track_id`, `user_id`),
FOREIGN KEY(`track_id`) REFERENCES `Track`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION ,
FOREIGN KEY(`user_id`) REFERENCES `User`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
""".trimIndent())
// Move all data to temporary table
database.execSQL("INSERT INTO TrackCreator_temp SELECT * FROM TrackCreator")
// Drop mis-scheme table
database.execSQL("DROP TABLE TrackCreator")
// Rename temporary table to definitive name
database.execSQL("ALTER TABLE TrackCreator_temp RENAME TO TrackCreator")
// Recreate indexes if needed (omitted for brevity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment