From cc4b0f7b117918098312fedf88fee1ad2200183b Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Mon, 19 Dec 2022 11:24:42 +1100 Subject: [PATCH] Fix performer migration (#3285) * Recreate performers table instead of drop column * Use performer_aliases to store original value --- .../42_performer_disambig_aliases.up.sql | 113 +++++++++++++++++- pkg/sqlite/migrations/42_postmigrate.go | 17 ++- 2 files changed, 121 insertions(+), 9 deletions(-) diff --git a/pkg/sqlite/migrations/42_performer_disambig_aliases.up.sql b/pkg/sqlite/migrations/42_performer_disambig_aliases.up.sql index 9fec6661df6..34197d8ae07 100644 --- a/pkg/sqlite/migrations/42_performer_disambig_aliases.up.sql +++ b/pkg/sqlite/migrations/42_performer_disambig_aliases.up.sql @@ -1,3 +1,5 @@ +PRAGMA foreign_keys=OFF; + CREATE TABLE `performer_aliases` ( `performer_id` integer NOT NULL, `alias` varchar(255) NOT NULL, @@ -8,11 +10,112 @@ CREATE TABLE `performer_aliases` ( CREATE INDEX `performer_aliases_alias` on `performer_aliases` (`alias`); DROP INDEX `performers_checksum_unique`; -ALTER TABLE `performers` DROP COLUMN `checksum`; -ALTER TABLE `performers` ADD COLUMN `disambiguation` varchar(255); --- these will be executed in the post-migration +-- drop aliases and checksum +-- add disambiguation + +CREATE TABLE `performers_new` ( + `id` integer not null primary key autoincrement, + `name` varchar(255), + `disambiguation` varchar(255), + `gender` varchar(20), + `url` varchar(255), + `twitter` varchar(255), + `instagram` varchar(255), + `birthdate` date, + `ethnicity` varchar(255), + `country` varchar(255), + `eye_color` varchar(255), + `height` int, + `measurements` varchar(255), + `fake_tits` varchar(255), + `career_length` varchar(255), + `tattoos` varchar(255), + `piercings` varchar(255), + `favorite` boolean not null default '0', + `created_at` datetime not null, + `updated_at` datetime not null, + `details` text, + `death_date` date, + `hair_color` varchar(255), + `weight` integer, + `rating` tinyint, + `ignore_auto_tag` boolean not null default '0' +); + +INSERT INTO `performers_new` + ( + `id`, + `name`, + `gender`, + `url`, + `twitter`, + `instagram`, + `birthdate`, + `ethnicity`, + `country`, + `eye_color`, + `height`, + `measurements`, + `fake_tits`, + `career_length`, + `tattoos`, + `piercings`, + `favorite`, + `created_at`, + `updated_at`, + `details`, + `death_date`, + `hair_color`, + `weight`, + `rating`, + `ignore_auto_tag` + ) + SELECT + `id`, + `name`, + `gender`, + `url`, + `twitter`, + `instagram`, + `birthdate`, + `ethnicity`, + `country`, + `eye_color`, + `height`, + `measurements`, + `fake_tits`, + `career_length`, + `tattoos`, + `piercings`, + `favorite`, + `created_at`, + `updated_at`, + `details`, + `death_date`, + `hair_color`, + `weight`, + `rating`, + `ignore_auto_tag` + FROM `performers`; --- ALTER TABLE `performers` DROP COLUMN `aliases` +INSERT INTO `performer_aliases` + ( + `performer_id`, + `alias` + ) + SELECT + `id`, + `aliases` + FROM `performers` + WHERE `performers`.`aliases` IS NOT NULL AND `performers`.`aliases` != ''; + +DROP TABLE `performers`; +ALTER TABLE `performers_new` rename to `performers`; + + +-- these will be executed in the post-migration -- CREATE UNIQUE INDEX `performers_name_disambiguation_unique` on `performers` (`name`, `disambiguation`) WHERE `disambiguation` IS NOT NULL; --- CREATE UNIQUE INDEX `performers_name_unique` on `performers` (`name`) WHERE `disambiguation` IS NULL; \ No newline at end of file +-- CREATE UNIQUE INDEX `performers_name_unique` on `performers` (`name`) WHERE `disambiguation` IS NULL; + +PRAGMA foreign_keys=ON; diff --git a/pkg/sqlite/migrations/42_postmigrate.go b/pkg/sqlite/migrations/42_postmigrate.go index d9147e792c0..cf9b38cdfde 100644 --- a/pkg/sqlite/migrations/42_postmigrate.go +++ b/pkg/sqlite/migrations/42_postmigrate.go @@ -56,13 +56,13 @@ func (m *schema42Migrator) migrate(ctx context.Context) error { gotSome := false if err := m.withTxn(ctx, func(tx *sqlx.Tx) error { - query := "SELECT `id`, `aliases` FROM `performers` WHERE `aliases` IS NOT NULL AND `aliases` != ''" + query := "SELECT `performer_id`, `alias` FROM `performer_aliases`" if lastID != 0 { - query += fmt.Sprintf(" AND `id` > %d ", lastID) + query += fmt.Sprintf(" WHERE `performer_id` > %d ", lastID) } - query += fmt.Sprintf(" ORDER BY `id` LIMIT %d", limit) + query += fmt.Sprintf(" ORDER BY `performer_id` LIMIT %d", limit) rows, err := m.db.Query(query) if err != nil { @@ -113,6 +113,16 @@ func (m *schema42Migrator) migratePerformerAliases(id int, aliases string) error return strings.ContainsRune(",/", r) }) + if len(aliasList) < 2 { + // existing value is fine + return nil + } + + // delete the existing row + if _, err := m.db.Exec("DELETE FROM `performer_aliases` WHERE `performer_id` = ?", id); err != nil { + return err + } + // trim whitespace from each alias for i, alias := range aliasList { aliasList[i] = strings.TrimSpace(alias) @@ -232,7 +242,6 @@ SELECT disambiguation FROM performers WHERE name = ? ORDER BY disambiguation DES func (m *schema42Migrator) executeSchemaChanges() error { return m.execAll([]string{ - "ALTER TABLE `performers` DROP COLUMN `aliases`", "CREATE UNIQUE INDEX `performers_name_disambiguation_unique` on `performers` (`name`, `disambiguation`) WHERE `disambiguation` IS NOT NULL", "CREATE UNIQUE INDEX `performers_name_unique` on `performers` (`name`) WHERE `disambiguation` IS NULL", })