From d8630c15d64cf90cc0459ab58619ee59b6dff504 Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Thu, 20 Aug 2026 14:58:47 +0300 Subject: [PATCH] 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 --- ...user_practice_progress_parent_scope.down.sql | 3 +++ ...9_user_practice_progress_parent_scope.up.sql | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/db/migrations/000109_user_practice_progress_parent_scope.down.sql b/db/migrations/000109_user_practice_progress_parent_scope.down.sql index 54994109..1a724cfe 100644 --- a/db/migrations/000109_user_practice_progress_parent_scope.down.sql +++ b/db/migrations/000109_user_practice_progress_parent_scope.down.sql @@ -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); diff --git a/db/migrations/000109_user_practice_progress_parent_scope.up.sql b/db/migrations/000109_user_practice_progress_parent_scope.up.sql index c30c0170..52ca75b4 100644 --- a/db/migrations/000109_user_practice_progress_parent_scope.up.sql +++ b/db/migrations/000109_user_practice_progress_parent_scope.up.sql @@ -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;