Skip to content

Commit

Permalink
Fix performer migration (#3285)
Browse files Browse the repository at this point in the history
* Recreate performers table instead of drop column
* Use performer_aliases to store original value
  • Loading branch information
WithoutPants authored Dec 19, 2022
1 parent 150c496 commit cc4b0f7
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 9 deletions.
113 changes: 108 additions & 5 deletions pkg/sqlite/migrations/42_performer_disambig_aliases.up.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
PRAGMA foreign_keys=OFF;

CREATE TABLE `performer_aliases` (
`performer_id` integer NOT NULL,
`alias` varchar(255) NOT NULL,
Expand All @@ -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;
-- CREATE UNIQUE INDEX `performers_name_unique` on `performers` (`name`) WHERE `disambiguation` IS NULL;

PRAGMA foreign_keys=ON;
17 changes: 13 additions & 4 deletions pkg/sqlite/migrations/42_postmigrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
})
Expand Down

0 comments on commit cc4b0f7

Please sign in to comment.