49 lines
3.3 KiB
PL/PgSQL
49 lines
3.3 KiB
PL/PgSQL
BEGIN;
|
|
|
|
-- Course purchases replace category subscriptions for learning content.
|
|
ALTER TABLE exam_prep.catalog_courses
|
|
ADD COLUMN price NUMERIC(12,2) NOT NULL DEFAULT 0 CHECK (price >= 0),
|
|
ADD COLUMN currency VARCHAR(3) NOT NULL DEFAULT 'ETB' CHECK (currency ~ '^[A-Z]{3}$');
|
|
|
|
ALTER TABLE exam_prep.units DROP CONSTRAINT chk_exam_prep_units_access_tier;
|
|
ALTER TABLE exam_prep.units ADD CONSTRAINT chk_exam_prep_units_access_tier CHECK (access_tier IN ('FREE','PREMIUM','INHERIT'));
|
|
ALTER TABLE exam_prep.units ALTER COLUMN access_tier SET DEFAULT 'INHERIT';
|
|
ALTER TABLE exam_prep.unit_modules DROP CONSTRAINT chk_exam_prep_unit_modules_access_tier;
|
|
ALTER TABLE exam_prep.unit_modules ADD CONSTRAINT chk_exam_prep_unit_modules_access_tier CHECK (access_tier IN ('FREE','PREMIUM','INHERIT'));
|
|
ALTER TABLE exam_prep.unit_modules ALTER COLUMN access_tier SET DEFAULT 'INHERIT';
|
|
ALTER TABLE exam_prep.unit_module_lessons DROP CONSTRAINT chk_exam_prep_unit_module_lessons_access_tier;
|
|
ALTER TABLE exam_prep.unit_module_lessons ADD CONSTRAINT chk_exam_prep_unit_module_lessons_access_tier CHECK (access_tier IN ('FREE','PREMIUM','INHERIT'));
|
|
ALTER TABLE exam_prep.unit_module_lessons ALTER COLUMN access_tier SET DEFAULT 'INHERIT';
|
|
|
|
-- Preserve the old effective restrictions; future children inherit unless overridden.
|
|
UPDATE exam_prep.unit_module_lessons l SET access_tier='PREMIUM'
|
|
FROM exam_prep.unit_modules m, exam_prep.units u, exam_prep.catalog_courses c
|
|
WHERE l.unit_module_id=m.id AND m.unit_id=u.id AND u.catalog_course_id=c.id
|
|
AND (m.access_tier='PREMIUM' OR u.access_tier='PREMIUM' OR c.access_tier='PREMIUM');
|
|
UPDATE exam_prep.unit_modules m SET access_tier='PREMIUM'
|
|
FROM exam_prep.units u, exam_prep.catalog_courses c
|
|
WHERE m.unit_id=u.id AND u.catalog_course_id=c.id AND (u.access_tier='PREMIUM' OR c.access_tier='PREMIUM');
|
|
UPDATE exam_prep.units u SET access_tier='PREMIUM' FROM exam_prep.catalog_courses c
|
|
WHERE u.catalog_course_id=c.id AND c.access_tier='PREMIUM';
|
|
-- Remove redundant explicit tiers bottom-up so later parent edits cascade by default.
|
|
UPDATE exam_prep.unit_module_lessons l SET access_tier='INHERIT' FROM exam_prep.unit_modules m WHERE l.unit_module_id=m.id AND l.access_tier=m.access_tier;
|
|
UPDATE exam_prep.unit_modules m SET access_tier='INHERIT' FROM exam_prep.units u WHERE m.unit_id=u.id AND m.access_tier=u.access_tier;
|
|
UPDATE exam_prep.units u SET access_tier='INHERIT' FROM exam_prep.catalog_courses c WHERE u.catalog_course_id=c.id AND u.access_tier=c.access_tier;
|
|
-- Internal containers must not override the chapter's tier.
|
|
UPDATE exam_prep.unit_modules m SET access_tier='INHERIT'
|
|
FROM exam_prep.chapter_lesson_containers cc WHERE cc.module_id=m.id;
|
|
|
|
ALTER TABLE payments ADD COLUMN course_id BIGINT REFERENCES exam_prep.catalog_courses(id) ON DELETE SET NULL;
|
|
ALTER TABLE payments ADD CONSTRAINT payments_course_or_plan CHECK (course_id IS NULL OR (plan_id IS NULL AND subscription_id IS NULL));
|
|
CREATE INDEX payments_course_user ON payments(user_id,course_id) WHERE course_id IS NOT NULL;
|
|
CREATE TABLE course_purchases (
|
|
id BIGSERIAL PRIMARY KEY,
|
|
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
course_id BIGINT NOT NULL REFERENCES exam_prep.catalog_courses(id) ON DELETE CASCADE,
|
|
payment_id BIGINT NOT NULL UNIQUE REFERENCES payments(id),
|
|
purchased_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
UNIQUE(user_id,course_id)
|
|
);
|
|
|
|
COMMIT;
|