Fix migration 000109 to drop legacy unique key before parent-scope backfill.

The backfill inserts multiple rows per user and question set when a practice has several parent links, so the old (user_id, question_set_id) constraint must be removed first.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-08-20 14:58:47 +03:00
parent fb528e9e77
commit d8630c15d6
2 changed files with 15 additions and 5 deletions

View File

@ -30,6 +30,9 @@ ALTER TABLE user_practice_progress
DROP COLUMN IF EXISTS parent_kind,
DROP COLUMN IF EXISTS parent_id;
ALTER TABLE user_practice_progress
DROP CONSTRAINT IF EXISTS user_practice_progress_user_id_question_set_id_key;
ALTER TABLE user_practice_progress
ADD CONSTRAINT user_practice_progress_user_id_question_set_id_key
UNIQUE (user_id, question_set_id);

View File

@ -1,9 +1,13 @@
BEGIN;
ALTER TABLE user_practice_progress
ADD COLUMN content_domain VARCHAR(20),
ADD COLUMN parent_kind VARCHAR(30),
ADD COLUMN parent_id BIGINT;
ADD COLUMN IF NOT EXISTS content_domain VARCHAR(20),
ADD COLUMN IF NOT EXISTS parent_kind VARCHAR(30),
ADD COLUMN IF NOT EXISTS parent_id BIGINT;
-- Allow multiple rows per (user_id, question_set_id) while backfilling parent scope.
ALTER TABLE user_practice_progress
DROP CONSTRAINT IF EXISTS user_practice_progress_user_id_question_set_id_key;
-- Expand legacy global completions to each LMS parent link.
INSERT INTO user_practice_progress (
@ -63,18 +67,21 @@ ALTER TABLE user_practice_progress
ALTER COLUMN parent_kind SET NOT NULL,
ALTER COLUMN parent_id SET NOT NULL;
ALTER TABLE user_practice_progress
DROP CONSTRAINT IF EXISTS user_practice_progress_content_domain_check;
ALTER TABLE user_practice_progress
ADD CONSTRAINT user_practice_progress_content_domain_check
CHECK (content_domain IN ('LMS', 'EXAM_PREP'));
ALTER TABLE user_practice_progress
DROP CONSTRAINT user_practice_progress_user_id_question_set_id_key;
DROP CONSTRAINT IF EXISTS user_practice_progress_user_question_set_parent_key;
ALTER TABLE user_practice_progress
ADD CONSTRAINT user_practice_progress_user_question_set_parent_key
UNIQUE (user_id, question_set_id, content_domain, parent_kind, parent_id);
CREATE INDEX idx_user_practice_progress_parent_scope
CREATE INDEX IF NOT EXISTS idx_user_practice_progress_parent_scope
ON user_practice_progress (user_id, content_domain, parent_kind, parent_id);
COMMIT;