From ae9a99805966d0b5ba4f54455eda8f60866dcfa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81sar=20Del=20Solar?= Date: Tue, 14 Jan 2025 08:58:52 -0500 Subject: [PATCH] fix conflict --- .../migrate_wordvault_lexicon/migrate.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/scripts/migrations/migrate_wordvault_lexicon/migrate.go b/scripts/migrations/migrate_wordvault_lexicon/migrate.go index b0d4d70..6658a1b 100644 --- a/scripts/migrations/migrate_wordvault_lexicon/migrate.go +++ b/scripts/migrations/migrate_wordvault_lexicon/migrate.go @@ -54,7 +54,23 @@ func migrate(cfg *config.Config, pool *pgxpool.Pool, fromLex, toLex string) erro } defer tx.Rollback(ctx) - changeLexQuery := `UPDATE wordvault_cards SET lexicon_name = $1 WHERE lexicon_name = $2` + changeLexQuery := ` + -- 1) DELETE all rows that *would* conflict. + DELETE FROM wordvault_cards c + WHERE c.lexicon_name = $2 + AND EXISTS ( + SELECT 1 + FROM wordvault_cards c2 + WHERE c2.user_id = c.user_id + AND c2.lexicon_name = $1 + AND c2.alphagram = c.alphagram + ); + + -- 2) UPDATE the remaining rows + UPDATE wordvault_cards + SET lexicon_name = $1 + WHERE lexicon_name = $2; + ` t, err := tx.Exec(ctx, changeLexQuery, toLex, fromLex) if err != nil {