feat(migrations): reconcile user_cards UNIQUE constraint per Option A (B6) #149
1 changed files with 16 additions and 8 deletions
|
|
@ -64,17 +64,25 @@ export const up = (pgm) => {
|
||||||
// Re-assert the canonical 3-col UNIQUE from `initial-schema.js`.
|
// Re-assert the canonical 3-col UNIQUE from `initial-schema.js`.
|
||||||
// No-op against initial-schema-applied envs (the constraint already
|
// No-op against initial-schema-applied envs (the constraint already
|
||||||
// exists under its auto-generated name); constructive against any
|
// exists under its auto-generated name); constructive against any
|
||||||
// env where the constraint was somehow dropped. Wrapped in
|
// env where the constraint was somehow dropped. Postgres does not
|
||||||
// `DO $$ ... EXCEPTION WHEN duplicate_object` because Postgres
|
// support `ADD CONSTRAINT ... IF NOT EXISTS`, so we pre-check
|
||||||
// does not support `ADD CONSTRAINT ... IF NOT EXISTS`.
|
// `pg_constraint`. (An earlier draft used `EXCEPTION WHEN
|
||||||
|
// duplicate_object`, but `ADD CONSTRAINT UNIQUE` raises
|
||||||
|
// SQLSTATE 42P07 `duplicate_table` from the auto-created
|
||||||
|
// supporting index, not 42710 `duplicate_object` — the pre-check
|
||||||
|
// sidesteps both.)
|
||||||
pgm.sql(`
|
pgm.sql(`
|
||||||
DO $$
|
DO $$
|
||||||
BEGIN
|
BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint
|
||||||
|
WHERE conname = 'user_cards_user_id_card_id_is_foil_key'
|
||||||
|
AND conrelid = 'user_cards'::regclass
|
||||||
|
) THEN
|
||||||
ALTER TABLE user_cards
|
ALTER TABLE user_cards
|
||||||
ADD CONSTRAINT user_cards_user_id_card_id_is_foil_key
|
ADD CONSTRAINT user_cards_user_id_card_id_is_foil_key
|
||||||
UNIQUE (user_id, card_id, is_foil);
|
UNIQUE (user_id, card_id, is_foil);
|
||||||
EXCEPTION
|
END IF;
|
||||||
WHEN duplicate_object THEN NULL;
|
|
||||||
END $$;
|
END $$;
|
||||||
`);
|
`);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue