112 lines
5.0 KiB
PL/PgSQL
112 lines
5.0 KiB
PL/PgSQL
BEGIN;
|
|
|
|
CREATE TABLE exam_prep.course_category_migration_backup AS
|
|
SELECT id, category FROM exam_prep.catalog_courses;
|
|
UPDATE exam_prep.catalog_courses SET category = 'EDUCATION';
|
|
|
|
-- Keep the legacy module storage as an implementation detail. Each chapter
|
|
-- (formerly a unit) has one lesson container; public APIs never expose it.
|
|
CREATE TABLE exam_prep.chapter_lesson_containers (
|
|
chapter_id BIGINT PRIMARY KEY REFERENCES exam_prep.units(id) ON DELETE CASCADE,
|
|
module_id BIGINT NOT NULL UNIQUE REFERENCES exam_prep.unit_modules(id) ON DELETE CASCADE
|
|
);
|
|
|
|
-- Retain the original placement for a data-preserving rollback.
|
|
CREATE TABLE exam_prep.chapter_lesson_migration_backup AS
|
|
SELECT id, unit_module_id, sort_order, publish_status, access_tier
|
|
FROM exam_prep.unit_module_lessons;
|
|
|
|
WITH containers AS (
|
|
INSERT INTO exam_prep.unit_modules (unit_id, name, sort_order, publish_status, access_tier)
|
|
SELECT u.id, '__chapter_lessons__',
|
|
COALESCE((SELECT MAX(m.sort_order) FROM exam_prep.unit_modules m WHERE m.unit_id = u.id), 0) + 1,
|
|
'PUBLISHED', 'FREE'
|
|
FROM exam_prep.units u
|
|
RETURNING id, unit_id
|
|
)
|
|
INSERT INTO exam_prep.chapter_lesson_containers (chapter_id, module_id)
|
|
SELECT unit_id, id FROM containers;
|
|
|
|
-- Preserve module restrictions on each lesson when removing that public level.
|
|
WITH placement AS (
|
|
SELECT l.id, c.module_id,
|
|
ROW_NUMBER() OVER (PARTITION BY m.unit_id ORDER BY m.sort_order, m.id, l.sort_order, l.id)::integer AS position,
|
|
CASE WHEN m.publish_status = 'PUBLISHED' THEN l.publish_status ELSE 'DRAFT' END AS publish_status,
|
|
CASE WHEN m.access_tier = 'PREMIUM' OR l.access_tier = 'PREMIUM' THEN 'PREMIUM' ELSE 'FREE' END AS access_tier
|
|
FROM exam_prep.unit_module_lessons l
|
|
JOIN exam_prep.unit_modules m ON m.id = l.unit_module_id
|
|
JOIN exam_prep.chapter_lesson_containers c ON c.chapter_id = m.unit_id
|
|
)
|
|
UPDATE exam_prep.unit_module_lessons l
|
|
SET unit_module_id = p.module_id, sort_order = p.position,
|
|
publish_status = p.publish_status, access_tier = p.access_tier
|
|
FROM placement p WHERE p.id = l.id;
|
|
|
|
-- Quiz shells use existing question authoring, publication and scoped progress.
|
|
-- Their parent is always exactly one chapter (legacy parent_kind UNIT).
|
|
CREATE FUNCTION exam_prep.check_chapter_quiz_parent() RETURNS trigger AS $$
|
|
DECLARE p_id bigint; p_ids bigint[]; qs_type text; parent_count bigint; chapter_count bigint;
|
|
BEGIN
|
|
IF TG_TABLE_NAME = 'practice_parents' THEN
|
|
p_ids := ARRAY[CASE WHEN TG_OP = 'DELETE' THEN OLD.practice_id ELSE NEW.practice_id END];
|
|
IF TG_OP = 'UPDATE' THEN
|
|
p_ids := array_append(p_ids, OLD.practice_id);
|
|
END IF;
|
|
ELSE
|
|
p_ids := ARRAY[NEW.id];
|
|
END IF;
|
|
FOREACH p_id IN ARRAY p_ids LOOP
|
|
SELECT qs.set_type INTO qs_type
|
|
FROM exam_prep.lesson_practices p JOIN question_sets qs ON qs.id = p.question_set_id
|
|
WHERE p.id = p_id;
|
|
IF qs_type = 'QUIZ' THEN
|
|
SELECT COUNT(*), COUNT(*) FILTER (WHERE pp.parent_kind = 'UNIT' AND u.id IS NOT NULL)
|
|
INTO parent_count, chapter_count
|
|
FROM exam_prep.practice_parents pp LEFT JOIN exam_prep.units u ON u.id = pp.parent_id
|
|
WHERE pp.practice_id = p_id;
|
|
IF parent_count <> 1 OR chapter_count <> 1 THEN
|
|
RAISE EXCEPTION 'a quiz must belong to exactly one chapter' USING ERRCODE = '23514';
|
|
END IF;
|
|
END IF;
|
|
END LOOP;
|
|
RETURN NULL;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
CREATE CONSTRAINT TRIGGER chapter_quiz_shell_parent
|
|
AFTER INSERT OR UPDATE ON exam_prep.lesson_practices
|
|
DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION exam_prep.check_chapter_quiz_parent();
|
|
CREATE CONSTRAINT TRIGGER chapter_quiz_parent
|
|
AFTER INSERT OR UPDATE OR DELETE ON exam_prep.practice_parents
|
|
DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION exam_prep.check_chapter_quiz_parent();
|
|
|
|
-- Parent links are polymorphic and have no foreign key to units. Remove quiz
|
|
-- shells while their chapter still exists, including cascading course deletes.
|
|
CREATE FUNCTION exam_prep.delete_chapter_quizzes() RETURNS trigger AS $$
|
|
BEGIN
|
|
DELETE FROM exam_prep.lesson_practices p
|
|
USING exam_prep.practice_parents pp, question_sets qs
|
|
WHERE pp.practice_id = p.id AND qs.id = p.question_set_id
|
|
AND qs.set_type = 'QUIZ' AND pp.parent_kind = 'UNIT'
|
|
AND pp.parent_id = OLD.id;
|
|
RETURN OLD;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
CREATE TRIGGER delete_chapter_quizzes BEFORE DELETE ON exam_prep.units
|
|
FOR EACH ROW EXECUTE FUNCTION exam_prep.delete_chapter_quizzes();
|
|
|
|
CREATE FUNCTION exam_prep.protect_quiz_question_set_type() RETURNS trigger AS $$
|
|
BEGIN
|
|
IF NEW.set_type IS DISTINCT FROM OLD.set_type AND EXISTS (
|
|
SELECT 1 FROM exam_prep.lesson_practices WHERE question_set_id = NEW.id
|
|
) THEN
|
|
RAISE EXCEPTION 'unlink learning content before changing its question set type' USING ERRCODE = '23514';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
CREATE TRIGGER protect_learning_question_set_type BEFORE UPDATE OF set_type ON question_sets
|
|
FOR EACH ROW EXECUTE FUNCTION exam_prep.protect_quiz_question_set_type();
|
|
|
|
COMMIT;
|