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;