Unify LMS hierarchy and assessment support
This commit is contained in:
parent
d54a2b11d4
commit
09dc5917f9
|
|
@ -225,7 +225,9 @@ Entry point: `cmd/main.go` (wires config, stores, services, Fiber, cron).
|
|||
### Migrations
|
||||
|
||||
- Path: `db/migrations/`
|
||||
- Create: `make migrations/new name=add_something`
|
||||
- Baseline: `000001_Rezina.up.sql` and `000001_Rezina.down.sql` contain the consolidated migration history.
|
||||
- This baseline is for a fresh or rebuilt database. Existing databases with a previous `schema_migrations` version need a database reset or a dedicated upgrade migration before using it.
|
||||
- Create future incremental migrations: `make migrations/new name=add_something`
|
||||
- Apply (Compose migrate service): `make migrations/up`
|
||||
|
||||
### sqlc
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
|
@ -1,8 +0,0 @@
|
|||
DROP TABLE IF EXISTS course_categories;
|
||||
DROP TABLE IF EXISTS courses;
|
||||
DROP TABLE IF EXISTS programs;
|
||||
DROP TABLE IF EXISTS levels;
|
||||
DROP TABLE IF EXISTS modules;
|
||||
DROP TABLE IF EXISTS module_videos;
|
||||
DROP TABLE IF EXISTS practices;
|
||||
DROP TABLE IF EXISTS practice_questions;
|
||||
|
|
@ -1,103 +0,0 @@
|
|||
CREATE TABLE IF NOT EXISTS course_categories (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name VARCHAR(150) NOT NULL, -- "Learning English", "Other Courses"
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS courses (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
category_id BIGINT NOT NULL REFERENCES course_categories(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS programs (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
course_id BIGINT NOT NULL REFERENCES courses(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
thumbnail TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS levels (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
program_id BIGINT NOT NULL REFERENCES programs(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
level_index INT NOT NULL, -- 1,2,3...
|
||||
|
||||
number_of_modules INT NOT NULL DEFAULT 0,
|
||||
number_of_practices INT NOT NULL DEFAULT 0,
|
||||
number_of_videos INT NOT NULL DEFAULT 0,
|
||||
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS modules (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
level_id BIGINT NOT NULL REFERENCES levels(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
content TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS module_videos (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
module_id BIGINT NOT NULL REFERENCES modules(id) ON DELETE CASCADE,
|
||||
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
video_url TEXT NOT NULL,
|
||||
duration INT NOT NULL, -- seconds
|
||||
resolution VARCHAR(20), -- "720p", "1080p"
|
||||
|
||||
is_published BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
publish_date TIMESTAMPTZ,
|
||||
visibility VARCHAR(50), -- public, private, unlisted
|
||||
|
||||
instructor_id VARCHAR(100),
|
||||
thumbnail TEXT,
|
||||
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS practices (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
owner_type VARCHAR(50) NOT NULL, -- LEVEL | MODULE
|
||||
owner_id BIGINT NOT NULL,
|
||||
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
banner_image TEXT,
|
||||
persona VARCHAR(100),
|
||||
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
|
||||
CHECK (owner_type IN ('LEVEL', 'MODULE'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS practice_questions (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
practice_id BIGINT NOT NULL REFERENCES practices(id) ON DELETE CASCADE,
|
||||
|
||||
question TEXT NOT NULL,
|
||||
question_voice_prompt TEXT,
|
||||
sample_answer_voice_prompt TEXT,
|
||||
sample_answer TEXT,
|
||||
tips TEXT,
|
||||
|
||||
type VARCHAR(50) NOT NULL -- MCQ, TRUE_FALSE, SHORT
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_courses_category_id ON courses(category_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_programs_course_id ON programs(course_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_levels_program_id ON levels(program_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_modules_level_id ON modules(level_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_videos_module_id ON module_videos(module_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_practices_owner ON practices(owner_type, owner_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_practice_questions_practice_id ON practice_questions(practice_id);
|
||||
|
|
@ -1,72 +0,0 @@
|
|||
-- Rollback: Restore old course hierarchy
|
||||
-- Note: This will lose any new data created with the simplified structure
|
||||
|
||||
-- Step 1: Recreate old tables
|
||||
CREATE TABLE IF NOT EXISTS programs (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
course_id BIGINT NOT NULL REFERENCES courses(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
thumbnail TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS levels (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
program_id BIGINT NOT NULL REFERENCES programs(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
level_index INT NOT NULL,
|
||||
number_of_modules INT NOT NULL DEFAULT 0,
|
||||
number_of_practices INT NOT NULL DEFAULT 0,
|
||||
number_of_videos INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS modules (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
level_id BIGINT NOT NULL REFERENCES levels(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
content TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS module_videos (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
module_id BIGINT NOT NULL REFERENCES modules(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
video_url TEXT NOT NULL,
|
||||
duration INT NOT NULL,
|
||||
resolution VARCHAR(20),
|
||||
is_published BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
publish_date TIMESTAMPTZ,
|
||||
visibility VARCHAR(50),
|
||||
instructor_id VARCHAR(100),
|
||||
thumbnail TEXT,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE
|
||||
);
|
||||
|
||||
-- Step 2: Restore practices polymorphic columns
|
||||
ALTER TABLE practices ADD COLUMN IF NOT EXISTS owner_type VARCHAR(50);
|
||||
ALTER TABLE practices ADD COLUMN IF NOT EXISTS owner_id BIGINT;
|
||||
|
||||
-- Step 3: Recreate old indexes
|
||||
CREATE INDEX IF NOT EXISTS idx_programs_course_id ON programs(course_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_levels_program_id ON levels(program_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_modules_level_id ON modules(level_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_videos_module_id ON module_videos(module_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_practices_owner ON practices(owner_type, owner_id);
|
||||
|
||||
-- Step 4: Drop new tables
|
||||
ALTER TABLE practices DROP CONSTRAINT IF EXISTS practices_sub_course_id_fkey;
|
||||
DROP INDEX IF EXISTS idx_practices_sub_course_id;
|
||||
ALTER TABLE practices DROP COLUMN IF EXISTS sub_course_id;
|
||||
|
||||
DROP TABLE IF EXISTS sub_course_videos CASCADE;
|
||||
DROP TABLE IF EXISTS sub_courses CASCADE;
|
||||
|
||||
-- Step 5: Add back constraint on practices
|
||||
ALTER TABLE practices ADD CONSTRAINT practices_owner_type_check CHECK (owner_type IN ('LEVEL', 'MODULE'));
|
||||
|
|
@ -1,145 +0,0 @@
|
|||
-- Migration: Simplify course hierarchy
|
||||
-- OLD: Course Category → Course → Program → Level → Module → (Video, Practice)
|
||||
-- NEW: Course Category → Course → Sub-course (with level) → (Video, Practice)
|
||||
|
||||
-- Step 1: Create new tables
|
||||
CREATE TABLE IF NOT EXISTS sub_courses (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
course_id BIGINT NOT NULL REFERENCES courses(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
thumbnail TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
level VARCHAR(50) NOT NULL, -- BEGINNER, INTERMEDIATE, ADVANCED
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
|
||||
CHECK (level IN ('BEGINNER', 'INTERMEDIATE', 'ADVANCED'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_courses_course_id ON sub_courses(course_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sub_course_videos (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
sub_course_id BIGINT NOT NULL REFERENCES sub_courses(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
video_url TEXT NOT NULL,
|
||||
duration INT NOT NULL, -- seconds
|
||||
resolution VARCHAR(20), -- "720p", "1080p"
|
||||
is_published BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
publish_date TIMESTAMPTZ,
|
||||
visibility VARCHAR(50), -- public, private, unlisted
|
||||
instructor_id VARCHAR(100),
|
||||
thumbnail TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_course_videos_sub_course_id ON sub_course_videos(sub_course_id);
|
||||
|
||||
-- Step 2: Add sub_course_id to practices (nullable during migration)
|
||||
ALTER TABLE practices ADD COLUMN IF NOT EXISTS sub_course_id BIGINT NULL;
|
||||
CREATE INDEX IF NOT EXISTS idx_practices_sub_course_id ON practices(sub_course_id);
|
||||
|
||||
-- Step 3: Migrate data from old structure to new structure
|
||||
|
||||
-- Insert sub-courses from (program, level) combinations
|
||||
INSERT INTO sub_courses (course_id, title, description, thumbnail, display_order, level, is_active)
|
||||
SELECT
|
||||
p.course_id,
|
||||
(p.title || ' - ' || l.title) as title,
|
||||
COALESCE(l.description, p.description) as description,
|
||||
p.thumbnail,
|
||||
(p.display_order * 100 + l.level_index) as display_order,
|
||||
CASE l.level_index
|
||||
WHEN 1 THEN 'BEGINNER'
|
||||
WHEN 2 THEN 'INTERMEDIATE'
|
||||
WHEN 3 THEN 'ADVANCED'
|
||||
ELSE 'BEGINNER'
|
||||
END as level,
|
||||
(l.is_active AND p.is_active) as is_active
|
||||
FROM levels l
|
||||
JOIN programs p ON p.id = l.program_id;
|
||||
|
||||
-- Create temporary mapping table for migration
|
||||
CREATE TEMP TABLE level_to_sub_course AS
|
||||
SELECT
|
||||
l.id as level_id,
|
||||
sc.id as sub_course_id
|
||||
FROM levels l
|
||||
JOIN programs p ON p.id = l.program_id
|
||||
JOIN sub_courses sc
|
||||
ON sc.course_id = p.course_id
|
||||
AND sc.title = (p.title || ' - ' || l.title);
|
||||
|
||||
-- Create temporary mapping for modules to sub-courses
|
||||
CREATE TEMP TABLE module_to_sub_course AS
|
||||
SELECT
|
||||
m.id as module_id,
|
||||
lsc.sub_course_id
|
||||
FROM modules m
|
||||
JOIN level_to_sub_course lsc ON lsc.level_id = m.level_id;
|
||||
|
||||
-- Migrate videos from module_videos to sub_course_videos
|
||||
INSERT INTO sub_course_videos (
|
||||
sub_course_id, title, description, video_url, duration, resolution,
|
||||
is_published, publish_date, visibility, instructor_id, thumbnail, display_order, is_active
|
||||
)
|
||||
SELECT
|
||||
msc.sub_course_id,
|
||||
mv.title,
|
||||
mv.description,
|
||||
mv.video_url,
|
||||
mv.duration,
|
||||
mv.resolution,
|
||||
mv.is_published,
|
||||
mv.publish_date,
|
||||
mv.visibility,
|
||||
mv.instructor_id,
|
||||
mv.thumbnail,
|
||||
(m.display_order * 100 + mv.id) as display_order,
|
||||
mv.is_active
|
||||
FROM module_videos mv
|
||||
JOIN modules m ON m.id = mv.module_id
|
||||
JOIN module_to_sub_course msc ON msc.module_id = m.id;
|
||||
|
||||
-- Migrate practices owned by LEVEL
|
||||
UPDATE practices pr
|
||||
SET sub_course_id = lsc.sub_course_id
|
||||
FROM level_to_sub_course lsc
|
||||
WHERE pr.owner_type = 'LEVEL'
|
||||
AND pr.owner_id = lsc.level_id;
|
||||
|
||||
-- Migrate practices owned by MODULE
|
||||
UPDATE practices pr
|
||||
SET sub_course_id = msc.sub_course_id
|
||||
FROM module_to_sub_course msc
|
||||
WHERE pr.owner_type = 'MODULE'
|
||||
AND pr.owner_id = msc.module_id;
|
||||
|
||||
-- Step 4: Enforce integrity on practices
|
||||
ALTER TABLE practices
|
||||
ADD CONSTRAINT practices_sub_course_id_fkey
|
||||
FOREIGN KEY (sub_course_id) REFERENCES sub_courses(id) ON DELETE CASCADE;
|
||||
|
||||
-- Make sub_course_id NOT NULL (only if there's data to migrate)
|
||||
-- If practices table has rows without sub_course_id after migration, this will fail
|
||||
-- ALTER TABLE practices ALTER COLUMN sub_course_id SET NOT NULL;
|
||||
|
||||
-- Step 5: Drop old columns from practices
|
||||
ALTER TABLE practices DROP CONSTRAINT IF EXISTS practices_owner_type_check;
|
||||
ALTER TABLE practices DROP COLUMN IF EXISTS owner_type;
|
||||
ALTER TABLE practices DROP COLUMN IF EXISTS owner_id;
|
||||
|
||||
-- Step 6: Drop old indexes
|
||||
DROP INDEX IF EXISTS idx_videos_module_id;
|
||||
DROP INDEX IF EXISTS idx_modules_level_id;
|
||||
DROP INDEX IF EXISTS idx_levels_program_id;
|
||||
DROP INDEX IF EXISTS idx_programs_course_id;
|
||||
DROP INDEX IF EXISTS idx_practices_owner;
|
||||
|
||||
-- Step 7: Drop old tables (CASCADE to remove FK references)
|
||||
DROP TABLE IF EXISTS module_videos CASCADE;
|
||||
DROP TABLE IF EXISTS modules CASCADE;
|
||||
DROP TABLE IF EXISTS levels CASCADE;
|
||||
DROP TABLE IF EXISTS programs CASCADE;
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
-- Remove thumbnail column from courses table
|
||||
ALTER TABLE courses DROP COLUMN IF EXISTS thumbnail;
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
-- Add thumbnail column to courses table
|
||||
ALTER TABLE courses ADD COLUMN IF NOT EXISTS thumbnail TEXT;
|
||||
|
|
@ -1,33 +0,0 @@
|
|||
-- Revert status field changes
|
||||
|
||||
-- Drop indexes
|
||||
DROP INDEX IF EXISTS idx_sub_course_videos_status;
|
||||
DROP INDEX IF EXISTS idx_practices_status;
|
||||
|
||||
-- Add back is_active to sub_course_videos
|
||||
ALTER TABLE sub_course_videos
|
||||
ADD COLUMN IF NOT EXISTS is_active BOOLEAN NOT NULL DEFAULT TRUE;
|
||||
|
||||
-- Migrate data back
|
||||
UPDATE sub_course_videos
|
||||
SET is_active = CASE
|
||||
WHEN status IN ('PUBLISHED', 'DRAFT') THEN true
|
||||
ELSE false
|
||||
END;
|
||||
|
||||
-- Drop status from sub_course_videos
|
||||
ALTER TABLE sub_course_videos DROP COLUMN IF EXISTS status;
|
||||
|
||||
-- Add back is_active to practices
|
||||
ALTER TABLE practices
|
||||
ADD COLUMN IF NOT EXISTS is_active BOOLEAN NOT NULL DEFAULT TRUE;
|
||||
|
||||
-- Migrate data back
|
||||
UPDATE practices
|
||||
SET is_active = CASE
|
||||
WHEN status IN ('PUBLISHED', 'DRAFT') THEN true
|
||||
ELSE false
|
||||
END;
|
||||
|
||||
-- Drop status from practices
|
||||
ALTER TABLE practices DROP COLUMN IF EXISTS status;
|
||||
|
|
@ -1,38 +0,0 @@
|
|||
-- Add status field to sub_course_videos and practices
|
||||
-- Status values: DRAFT, PUBLISHED, INACTIVE, ARCHIVED
|
||||
-- ARCHIVED is used for soft deletes
|
||||
|
||||
-- Add status column to sub_course_videos
|
||||
ALTER TABLE sub_course_videos
|
||||
ADD COLUMN IF NOT EXISTS status VARCHAR(20) NOT NULL DEFAULT 'DRAFT'
|
||||
CHECK (status IN ('DRAFT', 'PUBLISHED', 'INACTIVE', 'ARCHIVED'));
|
||||
|
||||
-- Migrate existing data based on is_active and is_published
|
||||
UPDATE sub_course_videos
|
||||
SET status = CASE
|
||||
WHEN is_published = true AND is_active = true THEN 'PUBLISHED'
|
||||
WHEN is_active = false THEN 'INACTIVE'
|
||||
ELSE 'DRAFT'
|
||||
END;
|
||||
|
||||
-- Drop is_active column from sub_course_videos (keep is_published for publish_date tracking)
|
||||
ALTER TABLE sub_course_videos DROP COLUMN IF EXISTS is_active;
|
||||
|
||||
-- Add status column to practices
|
||||
ALTER TABLE practices
|
||||
ADD COLUMN IF NOT EXISTS status VARCHAR(20) NOT NULL DEFAULT 'DRAFT'
|
||||
CHECK (status IN ('DRAFT', 'PUBLISHED', 'INACTIVE', 'ARCHIVED'));
|
||||
|
||||
-- Migrate existing data based on is_active
|
||||
UPDATE practices
|
||||
SET status = CASE
|
||||
WHEN is_active = true THEN 'PUBLISHED'
|
||||
ELSE 'INACTIVE'
|
||||
END;
|
||||
|
||||
-- Drop is_active column from practices
|
||||
ALTER TABLE practices DROP COLUMN IF EXISTS is_active;
|
||||
|
||||
-- Create indexes for status queries
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_course_videos_status ON sub_course_videos(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_practices_status ON practices(status);
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
-- Revert unified questions migration
|
||||
-- This will recreate the old tables but data migration back is not supported
|
||||
|
||||
-- Recreate assessment tables
|
||||
CREATE TABLE IF NOT EXISTS assessment_questions (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
question_type VARCHAR(30) NOT NULL CHECK (question_type IN ('MULTIPLE_CHOICE', 'SHORT_ANSWER', 'TRUE_FALSE')),
|
||||
difficulty_level TEXT,
|
||||
points INT NOT NULL DEFAULT 1,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS assessment_question_options (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
question_id BIGINT NOT NULL REFERENCES assessment_questions(id) ON DELETE CASCADE,
|
||||
option_text TEXT NOT NULL,
|
||||
option_order INT NOT NULL DEFAULT 0,
|
||||
is_correct BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS assessment_short_answers (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
question_id BIGINT NOT NULL REFERENCES assessment_questions(id) ON DELETE CASCADE,
|
||||
correct_answer TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS assessment_attempts (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL,
|
||||
total_questions INT NOT NULL,
|
||||
total_points INT NOT NULL,
|
||||
score INT,
|
||||
percentage NUMERIC(5,2),
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'IN_PROGRESS',
|
||||
started_at TIMESTAMPTZ,
|
||||
submitted_at TIMESTAMPTZ,
|
||||
evaluated_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS assessment_attempt_questions (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
attempt_id BIGINT NOT NULL REFERENCES assessment_attempts(id) ON DELETE CASCADE,
|
||||
question_id BIGINT NOT NULL REFERENCES assessment_questions(id),
|
||||
question_type VARCHAR(30) NOT NULL,
|
||||
points INT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS assessment_attempt_answers (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
attempt_id BIGINT NOT NULL REFERENCES assessment_attempts(id) ON DELETE CASCADE,
|
||||
question_id BIGINT NOT NULL REFERENCES assessment_questions(id),
|
||||
selected_option_id BIGINT,
|
||||
submitted_text TEXT,
|
||||
is_correct BOOLEAN,
|
||||
awarded_points INT NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Recreate practices tables
|
||||
CREATE TABLE IF NOT EXISTS practices (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
sub_course_id BIGINT,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
banner_image TEXT,
|
||||
persona VARCHAR(100),
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'DRAFT'
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS practice_questions (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
practice_id BIGINT NOT NULL REFERENCES practices(id) ON DELETE CASCADE,
|
||||
question TEXT NOT NULL,
|
||||
question_voice_prompt TEXT,
|
||||
sample_answer_voice_prompt TEXT,
|
||||
sample_answer TEXT,
|
||||
tips TEXT,
|
||||
type VARCHAR(50) NOT NULL
|
||||
);
|
||||
|
||||
-- Drop new unified tables
|
||||
DROP TABLE IF EXISTS question_set_items CASCADE;
|
||||
DROP TABLE IF EXISTS question_sets CASCADE;
|
||||
DROP TABLE IF EXISTS question_short_answers CASCADE;
|
||||
DROP TABLE IF EXISTS question_options CASCADE;
|
||||
DROP TABLE IF EXISTS questions CASCADE;
|
||||
|
|
@ -1,195 +0,0 @@
|
|||
-- Unified Question System Migration
|
||||
-- Replaces: practice_questions, assessment_questions, assessment_question_options, assessment_short_answers
|
||||
|
||||
-- 1. Create unified questions table
|
||||
CREATE TABLE IF NOT EXISTS questions (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
question_text TEXT NOT NULL,
|
||||
question_type VARCHAR(20) NOT NULL CHECK (question_type IN ('MCQ', 'TRUE_FALSE', 'SHORT_ANSWER')),
|
||||
difficulty_level VARCHAR(20) CHECK (difficulty_level IN ('EASY', 'MEDIUM', 'HARD')),
|
||||
points INT NOT NULL DEFAULT 1,
|
||||
explanation TEXT,
|
||||
tips TEXT,
|
||||
voice_prompt TEXT,
|
||||
sample_answer_voice_prompt TEXT,
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'DRAFT' CHECK (status IN ('DRAFT', 'PUBLISHED', 'INACTIVE', 'ARCHIVED')),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX idx_questions_type ON questions(question_type);
|
||||
CREATE INDEX idx_questions_status ON questions(status);
|
||||
CREATE INDEX idx_questions_difficulty ON questions(difficulty_level);
|
||||
|
||||
-- 2. Create question options table (for MCQ and TRUE_FALSE)
|
||||
CREATE TABLE IF NOT EXISTS question_options (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
question_id BIGINT NOT NULL REFERENCES questions(id) ON DELETE CASCADE,
|
||||
option_text TEXT NOT NULL,
|
||||
option_order INT NOT NULL DEFAULT 0,
|
||||
is_correct BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX idx_question_options_question_id ON question_options(question_id);
|
||||
|
||||
-- 3. Create question short answers table (for SHORT_ANSWER type)
|
||||
CREATE TABLE IF NOT EXISTS question_short_answers (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
question_id BIGINT NOT NULL REFERENCES questions(id) ON DELETE CASCADE,
|
||||
acceptable_answer TEXT NOT NULL,
|
||||
match_type VARCHAR(20) NOT NULL DEFAULT 'EXACT' CHECK (match_type IN ('EXACT', 'CONTAINS', 'CASE_INSENSITIVE')),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX idx_question_short_answers_question_id ON question_short_answers(question_id);
|
||||
|
||||
-- 4. Create question sets table (replaces practices for grouping questions)
|
||||
CREATE TABLE IF NOT EXISTS question_sets (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
set_type VARCHAR(30) NOT NULL CHECK (set_type IN ('PRACTICE', 'INITIAL_ASSESSMENT', 'QUIZ', 'EXAM', 'SURVEY')),
|
||||
owner_type VARCHAR(30), -- SUB_COURSE, COURSE, CATEGORY, STANDALONE
|
||||
owner_id BIGINT, -- References the owning entity
|
||||
banner_image TEXT,
|
||||
persona VARCHAR(100),
|
||||
time_limit_minutes INT, -- Optional time limit
|
||||
passing_score INT, -- Optional passing percentage
|
||||
shuffle_questions BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'DRAFT' CHECK (status IN ('DRAFT', 'PUBLISHED', 'INACTIVE', 'ARCHIVED')),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX idx_question_sets_type ON question_sets(set_type);
|
||||
CREATE INDEX idx_question_sets_owner ON question_sets(owner_type, owner_id);
|
||||
CREATE INDEX idx_question_sets_status ON question_sets(status);
|
||||
|
||||
-- 5. Create question set items table (links questions to sets)
|
||||
CREATE TABLE IF NOT EXISTS question_set_items (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
set_id BIGINT NOT NULL REFERENCES question_sets(id) ON DELETE CASCADE,
|
||||
question_id BIGINT NOT NULL REFERENCES questions(id) ON DELETE CASCADE,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(set_id, question_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_question_set_items_set_id ON question_set_items(set_id);
|
||||
CREATE INDEX idx_question_set_items_question_id ON question_set_items(question_id);
|
||||
|
||||
-- 6. Migrate data from assessment_questions to new questions table
|
||||
INSERT INTO questions (id, question_text, question_type, difficulty_level, points, status, created_at)
|
||||
SELECT
|
||||
id,
|
||||
title,
|
||||
CASE question_type
|
||||
WHEN 'MULTIPLE_CHOICE' THEN 'MCQ'
|
||||
WHEN 'SHORT_ANSWER' THEN 'SHORT_ANSWER'
|
||||
WHEN 'TRUE_FALSE' THEN 'TRUE_FALSE'
|
||||
ELSE 'MCQ'
|
||||
END,
|
||||
CASE difficulty_level
|
||||
WHEN 'EASY' THEN 'EASY'
|
||||
WHEN 'MEDIUM' THEN 'MEDIUM'
|
||||
WHEN 'HARD' THEN 'HARD'
|
||||
ELSE 'MEDIUM'
|
||||
END,
|
||||
points,
|
||||
CASE WHEN is_active THEN 'PUBLISHED' ELSE 'INACTIVE' END,
|
||||
created_at
|
||||
FROM assessment_questions;
|
||||
|
||||
-- 7. Migrate assessment_question_options to question_options
|
||||
INSERT INTO question_options (question_id, option_text, option_order, is_correct, created_at)
|
||||
SELECT question_id, option_text, option_order, is_correct, created_at
|
||||
FROM assessment_question_options;
|
||||
|
||||
-- 8. Migrate assessment_short_answers to question_short_answers
|
||||
INSERT INTO question_short_answers (question_id, acceptable_answer, match_type, created_at)
|
||||
SELECT question_id, correct_answer, 'EXACT', created_at
|
||||
FROM assessment_short_answers;
|
||||
|
||||
-- 9. Create initial assessment question set from existing data
|
||||
INSERT INTO question_sets (title, description, set_type, owner_type, status, created_at)
|
||||
VALUES ('Initial Assessment', 'Default initial assessment for new users', 'INITIAL_ASSESSMENT', 'STANDALONE', 'PUBLISHED', CURRENT_TIMESTAMP);
|
||||
|
||||
-- Link existing assessment questions to the initial assessment set
|
||||
INSERT INTO question_set_items (set_id, question_id, display_order)
|
||||
SELECT
|
||||
(SELECT id FROM question_sets WHERE set_type = 'INITIAL_ASSESSMENT' LIMIT 1),
|
||||
id,
|
||||
id -- Use ID as initial display order
|
||||
FROM questions
|
||||
WHERE id IN (SELECT id FROM assessment_questions);
|
||||
|
||||
-- 10. Migrate practice_questions to new structure
|
||||
-- First, get max ID from questions to avoid conflicts
|
||||
DO $$
|
||||
DECLARE
|
||||
max_q_id BIGINT;
|
||||
practice_rec RECORD;
|
||||
new_set_id BIGINT;
|
||||
new_question_id BIGINT;
|
||||
BEGIN
|
||||
SELECT COALESCE(MAX(id), 0) INTO max_q_id FROM questions;
|
||||
|
||||
-- For each practice in the old system, create a question_set
|
||||
FOR practice_rec IN
|
||||
SELECT DISTINCT p.id, p.sub_course_id, p.title, p.description, p.banner_image, p.persona, p.status
|
||||
FROM practices p
|
||||
LOOP
|
||||
-- Create question set for this practice
|
||||
INSERT INTO question_sets (title, description, set_type, owner_type, owner_id, banner_image, persona, status, created_at)
|
||||
VALUES (
|
||||
practice_rec.title,
|
||||
practice_rec.description,
|
||||
'PRACTICE',
|
||||
'SUB_COURSE',
|
||||
practice_rec.sub_course_id,
|
||||
practice_rec.banner_image,
|
||||
practice_rec.persona,
|
||||
practice_rec.status,
|
||||
CURRENT_TIMESTAMP
|
||||
)
|
||||
RETURNING id INTO new_set_id;
|
||||
|
||||
-- Migrate questions from this practice
|
||||
FOR new_question_id IN
|
||||
INSERT INTO questions (question_text, question_type, tips, sample_answer_voice_prompt, voice_prompt, status, created_at)
|
||||
SELECT
|
||||
pq.question,
|
||||
pq.type,
|
||||
pq.tips,
|
||||
pq.sample_answer_voice_prompt,
|
||||
pq.question_voice_prompt,
|
||||
'PUBLISHED',
|
||||
CURRENT_TIMESTAMP
|
||||
FROM practice_questions pq
|
||||
WHERE pq.practice_id = practice_rec.id
|
||||
RETURNING id
|
||||
LOOP
|
||||
-- Link question to set
|
||||
INSERT INTO question_set_items (set_id, question_id, display_order)
|
||||
VALUES (new_set_id, new_question_id, new_question_id);
|
||||
END LOOP;
|
||||
END LOOP;
|
||||
END $$;
|
||||
|
||||
-- 11. Reset sequences
|
||||
SELECT setval(pg_get_serial_sequence('questions', 'id'), COALESCE((SELECT MAX(id) FROM questions), 1), true);
|
||||
SELECT setval(pg_get_serial_sequence('question_options', 'id'), COALESCE((SELECT MAX(id) FROM question_options), 1), true);
|
||||
SELECT setval(pg_get_serial_sequence('question_short_answers', 'id'), COALESCE((SELECT MAX(id) FROM question_short_answers), 1), true);
|
||||
SELECT setval(pg_get_serial_sequence('question_sets', 'id'), COALESCE((SELECT MAX(id) FROM question_sets), 1), true);
|
||||
SELECT setval(pg_get_serial_sequence('question_set_items', 'id'), COALESCE((SELECT MAX(id) FROM question_set_items), 1), true);
|
||||
|
||||
-- 12. Drop old tables
|
||||
DROP TABLE IF EXISTS practice_questions CASCADE;
|
||||
DROP TABLE IF EXISTS practices CASCADE;
|
||||
DROP TABLE IF EXISTS assessment_attempt_answers CASCADE;
|
||||
DROP TABLE IF EXISTS assessment_attempt_questions CASCADE;
|
||||
DROP TABLE IF EXISTS assessment_attempts CASCADE;
|
||||
DROP TABLE IF EXISTS assessment_short_answers CASCADE;
|
||||
DROP TABLE IF EXISTS assessment_question_options CASCADE;
|
||||
DROP TABLE IF EXISTS assessment_questions CASCADE;
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
-- Remove video link from question_sets
|
||||
ALTER TABLE question_sets DROP COLUMN IF EXISTS sub_course_video_id;
|
||||
|
||||
-- Drop junction table
|
||||
DROP TABLE IF EXISTS question_set_personas;
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
-- Junction table to link users (as personas) to a question_set (practice)
|
||||
CREATE TABLE question_set_personas (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
question_set_id BIGINT NOT NULL REFERENCES question_sets(id) ON DELETE CASCADE,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
display_order INT DEFAULT 0,
|
||||
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(question_set_id, user_id)
|
||||
);
|
||||
|
||||
-- Add video context to question_sets for practice-video linking
|
||||
ALTER TABLE question_sets
|
||||
ADD COLUMN sub_course_video_id BIGINT REFERENCES sub_course_videos(id) ON DELETE SET NULL;
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX idx_question_set_personas_question_set_id ON question_set_personas(question_set_id);
|
||||
CREATE INDEX idx_question_set_personas_user_id ON question_set_personas(user_id);
|
||||
CREATE INDEX idx_question_sets_sub_course_video_id ON question_sets(sub_course_video_id);
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
DROP TABLE IF EXISTS user_subscriptions;
|
||||
DROP TABLE IF EXISTS subscription_plans;
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
-- Subscription Plans table
|
||||
CREATE TABLE subscription_plans (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
description TEXT,
|
||||
duration_value INT NOT NULL,
|
||||
duration_unit VARCHAR(10) NOT NULL CHECK (duration_unit IN ('DAY', 'WEEK', 'MONTH', 'YEAR')),
|
||||
price DECIMAL(10, 2) NOT NULL,
|
||||
currency VARCHAR(3) NOT NULL DEFAULT 'ETB',
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX idx_subscription_plans_active ON subscription_plans(is_active);
|
||||
|
||||
-- User Subscriptions table
|
||||
CREATE TABLE user_subscriptions (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
plan_id BIGINT NOT NULL REFERENCES subscription_plans(id) ON DELETE RESTRICT,
|
||||
starts_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'ACTIVE' CHECK (status IN ('PENDING', 'ACTIVE', 'EXPIRED', 'CANCELLED')),
|
||||
payment_reference VARCHAR(255),
|
||||
payment_method VARCHAR(50),
|
||||
auto_renew BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
cancelled_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX idx_user_subscriptions_user_id ON user_subscriptions(user_id);
|
||||
CREATE INDEX idx_user_subscriptions_status ON user_subscriptions(status);
|
||||
CREATE INDEX idx_user_subscriptions_expires_at ON user_subscriptions(expires_at);
|
||||
CREATE INDEX idx_user_subscriptions_user_status ON user_subscriptions(user_id, status);
|
||||
|
|
@ -1 +0,0 @@
|
|||
DROP TABLE IF EXISTS payments;
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
-- Payments table for tracking all payment transactions
|
||||
CREATE TABLE payments (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
plan_id BIGINT REFERENCES subscription_plans(id) ON DELETE SET NULL,
|
||||
subscription_id BIGINT REFERENCES user_subscriptions(id) ON DELETE SET NULL,
|
||||
|
||||
-- ArifPay specific fields
|
||||
session_id VARCHAR(100),
|
||||
transaction_id VARCHAR(100),
|
||||
nonce VARCHAR(255) NOT NULL UNIQUE,
|
||||
|
||||
-- Payment details
|
||||
amount DECIMAL(10, 2) NOT NULL,
|
||||
currency VARCHAR(3) NOT NULL DEFAULT 'ETB',
|
||||
payment_method VARCHAR(50),
|
||||
|
||||
-- Status tracking
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'PENDING' CHECK (status IN ('PENDING', 'PROCESSING', 'SUCCESS', 'FAILED', 'CANCELLED', 'EXPIRED')),
|
||||
|
||||
-- URLs for redirect
|
||||
payment_url TEXT,
|
||||
|
||||
-- Timestamps
|
||||
paid_at TIMESTAMPTZ,
|
||||
expires_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX idx_payments_user_id ON payments(user_id);
|
||||
CREATE INDEX idx_payments_session_id ON payments(session_id);
|
||||
CREATE INDEX idx_payments_nonce ON payments(nonce);
|
||||
CREATE INDEX idx_payments_status ON payments(status);
|
||||
CREATE INDEX idx_payments_subscription_id ON payments(subscription_id);
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
-- Remove Vimeo video hosting fields from sub_course_videos table
|
||||
DROP INDEX IF EXISTS idx_sub_course_videos_vimeo_id;
|
||||
|
||||
ALTER TABLE sub_course_videos
|
||||
DROP COLUMN IF EXISTS vimeo_id,
|
||||
DROP COLUMN IF EXISTS vimeo_embed_url,
|
||||
DROP COLUMN IF EXISTS vimeo_player_html,
|
||||
DROP COLUMN IF EXISTS vimeo_status,
|
||||
DROP COLUMN IF EXISTS video_host_provider;
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
-- Add Vimeo video hosting fields to sub_course_videos table
|
||||
ALTER TABLE sub_course_videos
|
||||
ADD COLUMN IF NOT EXISTS vimeo_id TEXT,
|
||||
ADD COLUMN IF NOT EXISTS vimeo_embed_url TEXT,
|
||||
ADD COLUMN IF NOT EXISTS vimeo_player_html TEXT,
|
||||
ADD COLUMN IF NOT EXISTS vimeo_status TEXT DEFAULT 'pending',
|
||||
ADD COLUMN IF NOT EXISTS video_host_provider TEXT DEFAULT 'DIRECT';
|
||||
|
||||
-- Create index on vimeo_id for faster lookups
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_course_videos_vimeo_id ON sub_course_videos(vimeo_id) WHERE vimeo_id IS NOT NULL;
|
||||
|
||||
-- Add comment for documentation
|
||||
COMMENT ON COLUMN sub_course_videos.vimeo_id IS 'Vimeo video ID for videos hosted on Vimeo';
|
||||
COMMENT ON COLUMN sub_course_videos.vimeo_embed_url IS 'Vimeo player embed URL';
|
||||
COMMENT ON COLUMN sub_course_videos.vimeo_player_html IS 'Vimeo iframe embed HTML code';
|
||||
COMMENT ON COLUMN sub_course_videos.vimeo_status IS 'Vimeo video status: pending, uploading, transcoding, available, error';
|
||||
COMMENT ON COLUMN sub_course_videos.video_host_provider IS 'Video hosting provider: DIRECT or VIMEO';
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
DROP INDEX IF EXISTS idx_team_members_status;
|
||||
DROP INDEX IF EXISTS idx_team_members_department;
|
||||
DROP INDEX IF EXISTS idx_team_members_team_role;
|
||||
DROP INDEX IF EXISTS idx_team_members_email;
|
||||
DROP TABLE IF EXISTS team_members;
|
||||
|
|
@ -1,72 +0,0 @@
|
|||
-- Team members table for managing internal LMS company staff
|
||||
CREATE TABLE IF NOT EXISTS team_members (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
|
||||
-- Basic info
|
||||
first_name VARCHAR(255) NOT NULL,
|
||||
last_name VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(255) NOT NULL UNIQUE,
|
||||
phone_number VARCHAR(20),
|
||||
|
||||
-- Authentication
|
||||
password BYTEA NOT NULL,
|
||||
|
||||
-- Role within the team (different from learner roles)
|
||||
team_role VARCHAR(50) NOT NULL CHECK (
|
||||
team_role IN (
|
||||
'SUPER_ADMIN', -- Full system access
|
||||
'ADMIN', -- Administrative tasks
|
||||
'CONTENT_MANAGER', -- Manages courses, content
|
||||
'SUPPORT_AGENT', -- Customer support
|
||||
'INSTRUCTOR', -- Creates/manages courses
|
||||
'FINANCE', -- Payment/subscription management
|
||||
'HR', -- Team member management
|
||||
'ANALYST' -- Reports and analytics
|
||||
)
|
||||
),
|
||||
|
||||
-- Department
|
||||
department VARCHAR(100),
|
||||
|
||||
-- Job title
|
||||
job_title VARCHAR(150),
|
||||
|
||||
-- Employment details
|
||||
employment_type VARCHAR(50) CHECK (
|
||||
employment_type IN ('full_time', 'part_time', 'contract', 'intern')
|
||||
),
|
||||
hire_date DATE,
|
||||
|
||||
-- Profile
|
||||
profile_picture_url TEXT,
|
||||
bio TEXT,
|
||||
|
||||
-- Contact
|
||||
work_phone VARCHAR(20),
|
||||
emergency_contact VARCHAR(255),
|
||||
|
||||
-- Status
|
||||
status VARCHAR(50) NOT NULL DEFAULT 'active' CHECK (
|
||||
status IN ('active', 'inactive', 'suspended', 'terminated')
|
||||
),
|
||||
|
||||
-- Verification
|
||||
email_verified BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
|
||||
-- Permissions (JSON array of permission strings)
|
||||
permissions JSONB DEFAULT '[]'::jsonb,
|
||||
|
||||
-- Tracking
|
||||
last_login TIMESTAMPTZ,
|
||||
created_by BIGINT REFERENCES team_members(id),
|
||||
updated_by BIGINT REFERENCES team_members(id),
|
||||
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
-- Indexes
|
||||
CREATE INDEX IF NOT EXISTS idx_team_members_email ON team_members(email);
|
||||
CREATE INDEX IF NOT EXISTS idx_team_members_team_role ON team_members(team_role);
|
||||
CREATE INDEX IF NOT EXISTS idx_team_members_department ON team_members(department);
|
||||
CREATE INDEX IF NOT EXISTS idx_team_members_status ON team_members(status);
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
-- Drop trigger
|
||||
DROP TRIGGER IF EXISTS trg_update_profile_completion ON users;
|
||||
|
||||
-- Drop function
|
||||
DROP FUNCTION IF EXISTS calculate_profile_completion();
|
||||
|
||||
-- Drop column
|
||||
ALTER TABLE users DROP COLUMN IF EXISTS profile_completion_percentage;
|
||||
|
|
@ -1,71 +0,0 @@
|
|||
-- Add profile_completion_percentage column
|
||||
ALTER TABLE users ADD COLUMN profile_completion_percentage SMALLINT NOT NULL DEFAULT 0;
|
||||
|
||||
-- Create function to calculate profile completion
|
||||
CREATE OR REPLACE FUNCTION calculate_profile_completion()
|
||||
RETURNS TRIGGER AS $$
|
||||
DECLARE
|
||||
filled_count INTEGER := 0;
|
||||
BEGIN
|
||||
-- Check first_name
|
||||
IF NULLIF(TRIM(NEW.first_name), '') IS NOT NULL THEN
|
||||
filled_count := filled_count + 1;
|
||||
END IF;
|
||||
|
||||
-- Check last_name
|
||||
IF NULLIF(TRIM(NEW.last_name), '') IS NOT NULL THEN
|
||||
filled_count := filled_count + 1;
|
||||
END IF;
|
||||
|
||||
-- Check email OR phone_number (counts as 1 if either is filled)
|
||||
IF NULLIF(TRIM(NEW.email), '') IS NOT NULL OR NULLIF(TRIM(NEW.phone_number), '') IS NOT NULL THEN
|
||||
filled_count := filled_count + 1;
|
||||
END IF;
|
||||
|
||||
-- Check preferred_language
|
||||
IF NULLIF(TRIM(NEW.preferred_language), '') IS NOT NULL THEN
|
||||
filled_count := filled_count + 1;
|
||||
END IF;
|
||||
|
||||
--- Check country
|
||||
IF NULLIF(TRIM(NEW.country), '') IS NOT NULL THEN
|
||||
filled_count := filled_count + 1;
|
||||
END IF;
|
||||
|
||||
-- Check age_group
|
||||
IF NULLIF(TRIM(NEW.age_group), '') IS NOT NULL THEN
|
||||
filled_count := filled_count + 1;
|
||||
END IF;
|
||||
|
||||
-- Check learning_goal
|
||||
IF NULLIF(TRIM(NEW.learning_goal), '') IS NOT NULL THEN
|
||||
filled_count := filled_count + 1;
|
||||
END IF;
|
||||
|
||||
-- Check language_goal
|
||||
IF NULLIF(TRIM(NEW.language_goal), '') IS NOT NULL THEN
|
||||
filled_count := filled_count + 1;
|
||||
END IF;
|
||||
|
||||
-- Calculate percentage (8 total required fields)
|
||||
NEW.profile_completion_percentage := (filled_count * 100 / 8)::SMALLINT;
|
||||
|
||||
-- Set profile_completed if 100%
|
||||
IF NEW.profile_completion_percentage = 100 THEN
|
||||
NEW.profile_completed := true;
|
||||
ELSE
|
||||
NEW.profile_completed := false;
|
||||
END IF;
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Create trigger
|
||||
CREATE TRIGGER trg_update_profile_completion
|
||||
BEFORE INSERT OR UPDATE ON users
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION calculate_profile_completion();
|
||||
|
||||
-- Backfill existing rows
|
||||
UPDATE users SET updated_at = updated_at;
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
ALTER TABLE devices DROP CONSTRAINT IF EXISTS devices_user_fk;
|
||||
ALTER TABLE devices DROP CONSTRAINT IF EXISTS devices_user_id_device_token_uniq;
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
-- Add unique constraint for ON CONFLICT to work in CreateDevice query
|
||||
ALTER TABLE devices
|
||||
ADD CONSTRAINT devices_user_id_device_token_uniq UNIQUE (user_id, device_token);
|
||||
|
||||
-- Add foreign key to users table for data integrity and cascade deletion
|
||||
ALTER TABLE devices
|
||||
ADD CONSTRAINT devices_user_fk
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
|
||||
|
|
@ -1 +0,0 @@
|
|||
DROP TABLE IF EXISTS activity_logs;
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
CREATE TABLE IF NOT EXISTS activity_logs (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
actor_id BIGINT NULL,
|
||||
actor_role TEXT NULL,
|
||||
action TEXT NOT NULL,
|
||||
resource_type TEXT NOT NULL,
|
||||
resource_id BIGINT NULL,
|
||||
message TEXT NULL,
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
ip_address TEXT NULL,
|
||||
user_agent TEXT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_activity_logs_created_at ON activity_logs (created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_activity_logs_actor ON activity_logs (actor_id, created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_activity_logs_resource ON activity_logs (resource_type, resource_id, created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_activity_logs_action ON activity_logs (action, created_at DESC);
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
ALTER TABLE notifications
|
||||
DROP CONSTRAINT IF EXISTS notifications_type_check;
|
||||
|
||||
ALTER TABLE notifications
|
||||
ADD CONSTRAINT notifications_type_check CHECK (
|
||||
type IN (
|
||||
'course_enrolled',
|
||||
'lesson_completed',
|
||||
'assessment_assigned',
|
||||
'assessment_submitted',
|
||||
'assessment_graded',
|
||||
'course_completed',
|
||||
'certificate_issued',
|
||||
'announcement',
|
||||
'otp_sent',
|
||||
'signup_welcome',
|
||||
'system_alert'
|
||||
)
|
||||
);
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
-- Drop the existing CHECK constraint on notifications.type and replace with an expanded one
|
||||
ALTER TABLE notifications
|
||||
DROP CONSTRAINT IF EXISTS notifications_type_check;
|
||||
|
||||
ALTER TABLE notifications
|
||||
ADD CONSTRAINT notifications_type_check CHECK (
|
||||
type IN (
|
||||
'course_enrolled',
|
||||
'lesson_completed',
|
||||
'assessment_assigned',
|
||||
'assessment_submitted',
|
||||
'assessment_graded',
|
||||
'course_completed',
|
||||
'certificate_issued',
|
||||
'announcement',
|
||||
'otp_sent',
|
||||
'signup_welcome',
|
||||
'system_alert',
|
||||
'knowledge_level_update',
|
||||
'payment_verified',
|
||||
'subscription_activated',
|
||||
'course_created',
|
||||
'sub_course_created',
|
||||
'video_added',
|
||||
'issue_status_updated',
|
||||
'issue_created',
|
||||
'admin_created',
|
||||
'team_member_created',
|
||||
'user_deleted'
|
||||
)
|
||||
);
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
-- Remove the receiver_type column.
|
||||
ALTER TABLE notifications
|
||||
DROP COLUMN IF EXISTS receiver_type;
|
||||
|
||||
-- Re-add the foreign key constraint on user_id referencing users(id).
|
||||
ALTER TABLE notifications
|
||||
ADD CONSTRAINT notifications_user_id_fkey
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
-- Drop the foreign key constraint on user_id so notifications can reference
|
||||
-- either users or team_members depending on receiver_type.
|
||||
ALTER TABLE notifications
|
||||
DROP CONSTRAINT IF EXISTS notifications_user_id_fkey;
|
||||
|
||||
-- Add a receiver_type column to distinguish between user and team_member recipients.
|
||||
ALTER TABLE notifications
|
||||
ADD COLUMN IF NOT EXISTS receiver_type TEXT NOT NULL DEFAULT 'user'
|
||||
CHECK (receiver_type IN ('user', 'team_member'));
|
||||
|
|
@ -1 +0,0 @@
|
|||
DROP TABLE IF EXISTS ratings;
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
CREATE TABLE IF NOT EXISTS ratings (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
target_type VARCHAR(20) NOT NULL, -- 'app', 'course', 'sub_course'
|
||||
target_id BIGINT NOT NULL DEFAULT 0, -- 0 for app rating, course_id or sub_course_id otherwise
|
||||
stars SMALLINT NOT NULL CHECK (stars >= 1 AND stars <= 5),
|
||||
review TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
UNIQUE (user_id, target_type, target_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_ratings_target ON ratings (target_type, target_id);
|
||||
CREATE INDEX idx_ratings_user ON ratings (user_id);
|
||||
|
|
@ -1 +0,0 @@
|
|||
ALTER TABLE courses DROP COLUMN IF EXISTS intro_video_url;
|
||||
|
|
@ -1 +0,0 @@
|
|||
ALTER TABLE courses ADD COLUMN IF NOT EXISTS intro_video_url TEXT;
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
DROP TABLE IF EXISTS user_sub_course_progress;
|
||||
DROP TABLE IF EXISTS sub_course_prerequisites;
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
-- Sub-course prerequisite links (admin-defined course flow)
|
||||
CREATE TABLE IF NOT EXISTS sub_course_prerequisites (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
sub_course_id BIGINT NOT NULL REFERENCES sub_courses(id) ON DELETE CASCADE,
|
||||
prerequisite_sub_course_id BIGINT NOT NULL REFERENCES sub_courses(id) ON DELETE CASCADE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
UNIQUE(sub_course_id, prerequisite_sub_course_id),
|
||||
CHECK(sub_course_id != prerequisite_sub_course_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_course_prerequisites_sub_course_id
|
||||
ON sub_course_prerequisites(sub_course_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_course_prerequisites_prerequisite_id
|
||||
ON sub_course_prerequisites(prerequisite_sub_course_id);
|
||||
|
||||
-- User progress per sub-course
|
||||
CREATE TABLE IF NOT EXISTS user_sub_course_progress (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
sub_course_id BIGINT NOT NULL REFERENCES sub_courses(id) ON DELETE CASCADE,
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'IN_PROGRESS' CHECK (
|
||||
status IN ('IN_PROGRESS', 'COMPLETED')
|
||||
),
|
||||
progress_percentage SMALLINT NOT NULL DEFAULT 0 CHECK (
|
||||
progress_percentage BETWEEN 0 AND 100
|
||||
),
|
||||
started_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
completed_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ,
|
||||
|
||||
UNIQUE(user_id, sub_course_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_sub_course_progress_user_id
|
||||
ON user_sub_course_progress(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_sub_course_progress_sub_course_id
|
||||
ON user_sub_course_progress(sub_course_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_sub_course_progress_status
|
||||
ON user_sub_course_progress(user_id, status);
|
||||
|
|
@ -1 +0,0 @@
|
|||
DROP TABLE IF EXISTS scheduled_notifications;
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
CREATE TABLE IF NOT EXISTS scheduled_notifications (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
|
||||
channel TEXT NOT NULL CHECK (channel IN ('email', 'sms', 'push')),
|
||||
title TEXT,
|
||||
message TEXT NOT NULL,
|
||||
html TEXT,
|
||||
|
||||
scheduled_at TIMESTAMPTZ NOT NULL,
|
||||
|
||||
status TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (status IN ('pending', 'processing', 'sent', 'failed', 'cancelled')),
|
||||
|
||||
target_user_ids BIGINT[],
|
||||
target_role TEXT,
|
||||
target_raw JSONB,
|
||||
|
||||
attempt_count INT NOT NULL DEFAULT 0,
|
||||
last_error TEXT,
|
||||
processing_started_at TIMESTAMPTZ,
|
||||
sent_at TIMESTAMPTZ,
|
||||
cancelled_at TIMESTAMPTZ,
|
||||
|
||||
created_by BIGINT NOT NULL REFERENCES users(id),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT scheduled_notifications_target_required CHECK (
|
||||
(target_user_ids IS NOT NULL AND array_length(target_user_ids, 1) > 0)
|
||||
OR target_role IS NOT NULL
|
||||
OR target_raw IS NOT NULL
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_scheduled_notifications_due
|
||||
ON scheduled_notifications (scheduled_at)
|
||||
WHERE status = 'pending';
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_scheduled_notifications_status
|
||||
ON scheduled_notifications (status, scheduled_at);
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
DROP TABLE IF EXISTS role_permissions;
|
||||
DROP TABLE IF EXISTS permissions;
|
||||
DROP TABLE IF EXISTS roles;
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
-- RBAC: Roles, Permissions, Role-Permissions
|
||||
|
||||
CREATE TABLE IF NOT EXISTS roles (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
description TEXT NOT NULL DEFAULT '',
|
||||
is_system BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS permissions (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
key TEXT NOT NULL UNIQUE,
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
description TEXT NOT NULL DEFAULT '',
|
||||
group_name TEXT NOT NULL DEFAULT '',
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS role_permissions (
|
||||
role_id BIGINT NOT NULL REFERENCES roles(id) ON DELETE CASCADE,
|
||||
permission_id BIGINT NOT NULL REFERENCES permissions(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (role_id, permission_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_role_permissions_role_id ON role_permissions(role_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_role_permissions_permission_id ON role_permissions(permission_id);
|
||||
|
||||
-- Seed system roles
|
||||
INSERT INTO roles (name, description, is_system) VALUES
|
||||
('SUPER_ADMIN', 'System super administrator with full access', true),
|
||||
('ADMIN', 'System administrator', true),
|
||||
('STUDENT', 'Student user', true),
|
||||
('INSTRUCTOR', 'Instructor user', true),
|
||||
('SUPPORT', 'Support staff', true)
|
||||
ON CONFLICT (name) DO NOTHING;
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
-- Revert AUDIO question type changes
|
||||
|
||||
-- 1. Drop question_audio_answers table
|
||||
DROP TABLE IF EXISTS question_audio_answers CASCADE;
|
||||
|
||||
-- 2. Remove image_url column
|
||||
ALTER TABLE questions DROP COLUMN IF EXISTS image_url;
|
||||
|
||||
-- 3. Revert question_type CHECK constraint
|
||||
ALTER TABLE questions
|
||||
DROP CONSTRAINT IF EXISTS questions_question_type_check;
|
||||
|
||||
ALTER TABLE questions
|
||||
ADD CONSTRAINT questions_question_type_check
|
||||
CHECK (question_type IN ('MCQ', 'TRUE_FALSE', 'SHORT_ANSWER'));
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
-- Add AUDIO question type and image_url support
|
||||
|
||||
-- 1. Extend question_type CHECK constraint to include AUDIO
|
||||
ALTER TABLE questions
|
||||
DROP CONSTRAINT IF EXISTS questions_question_type_check;
|
||||
|
||||
ALTER TABLE questions
|
||||
ADD CONSTRAINT questions_question_type_check
|
||||
CHECK (question_type IN ('MCQ', 'TRUE_FALSE', 'SHORT_ANSWER', 'AUDIO'));
|
||||
|
||||
-- 2. Add image_url column to questions
|
||||
ALTER TABLE questions
|
||||
ADD COLUMN IF NOT EXISTS image_url TEXT;
|
||||
|
||||
-- 3. Create question_audio_answers table for storing correct answer text
|
||||
CREATE TABLE IF NOT EXISTS question_audio_answers (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
question_id BIGINT NOT NULL REFERENCES questions(id) ON DELETE CASCADE,
|
||||
correct_answer_text TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_question_audio_answers_question_id
|
||||
ON question_audio_answers(question_id);
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
ALTER TABLE course_categories DROP COLUMN display_order;
|
||||
ALTER TABLE courses DROP COLUMN display_order;
|
||||
ALTER TABLE question_sets DROP COLUMN display_order;
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
ALTER TABLE course_categories ADD COLUMN display_order INT NOT NULL DEFAULT 0;
|
||||
ALTER TABLE courses ADD COLUMN display_order INT NOT NULL DEFAULT 0;
|
||||
ALTER TABLE question_sets ADD COLUMN display_order INT NOT NULL DEFAULT 0;
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
DROP TRIGGER IF EXISTS trg_sub_courses_create_entry_assessment ON sub_courses;
|
||||
DROP FUNCTION IF EXISTS create_sub_course_entry_assessment();
|
||||
DROP FUNCTION IF EXISTS clone_default_initial_assessment_items(BIGINT);
|
||||
|
||||
DROP INDEX IF EXISTS idx_question_sets_unique_subcourse_initial_assessment;
|
||||
DROP INDEX IF EXISTS idx_sub_courses_level_sub_level;
|
||||
|
||||
ALTER TABLE sub_courses
|
||||
DROP CONSTRAINT IF EXISTS sub_courses_level_sub_level_check;
|
||||
|
||||
ALTER TABLE sub_courses
|
||||
DROP COLUMN IF EXISTS sub_level;
|
||||
|
|
@ -1,148 +0,0 @@
|
|||
-- Add sub-level support to sub_courses and enforce valid level/sub-level combinations.
|
||||
ALTER TABLE sub_courses
|
||||
ADD COLUMN sub_level VARCHAR(2);
|
||||
|
||||
UPDATE sub_courses
|
||||
SET sub_level = CASE level
|
||||
WHEN 'BEGINNER' THEN 'A1'
|
||||
WHEN 'INTERMEDIATE' THEN 'B1'
|
||||
WHEN 'ADVANCED' THEN 'C1'
|
||||
ELSE 'A1'
|
||||
END
|
||||
WHERE sub_level IS NULL;
|
||||
|
||||
ALTER TABLE sub_courses
|
||||
ALTER COLUMN sub_level SET NOT NULL;
|
||||
|
||||
ALTER TABLE sub_courses
|
||||
ADD CONSTRAINT sub_courses_level_sub_level_check CHECK (
|
||||
(level = 'BEGINNER' AND sub_level IN ('A1', 'A2', 'A3')) OR
|
||||
(level = 'INTERMEDIATE' AND sub_level IN ('B1', 'B2', 'B3')) OR
|
||||
(level = 'ADVANCED' AND sub_level IN ('C1', 'C2', 'C3'))
|
||||
);
|
||||
|
||||
CREATE INDEX idx_sub_courses_level_sub_level ON sub_courses(level, sub_level);
|
||||
|
||||
-- Ensure each sub-course has an entry-assessment question set.
|
||||
CREATE UNIQUE INDEX idx_question_sets_unique_subcourse_initial_assessment
|
||||
ON question_sets(owner_type, owner_id, set_type)
|
||||
WHERE owner_type = 'SUB_COURSE' AND set_type = 'INITIAL_ASSESSMENT' AND status != 'ARCHIVED';
|
||||
|
||||
CREATE OR REPLACE FUNCTION clone_default_initial_assessment_items(target_set_id BIGINT)
|
||||
RETURNS VOID AS $$
|
||||
DECLARE
|
||||
template_set_id BIGINT;
|
||||
BEGIN
|
||||
SELECT id
|
||||
INTO template_set_id
|
||||
FROM question_sets
|
||||
WHERE set_type = 'INITIAL_ASSESSMENT'
|
||||
AND owner_type = 'STANDALONE'
|
||||
AND status = 'PUBLISHED'
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 1;
|
||||
|
||||
IF template_set_id IS NULL THEN
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
INSERT INTO question_set_items (set_id, question_id, display_order)
|
||||
SELECT target_set_id, qsi.question_id, qsi.display_order
|
||||
FROM question_set_items qsi
|
||||
WHERE qsi.set_id = template_set_id
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM question_set_items existing
|
||||
WHERE existing.set_id = target_set_id
|
||||
AND existing.question_id = qsi.question_id
|
||||
);
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
INSERT INTO question_sets (
|
||||
title,
|
||||
description,
|
||||
set_type,
|
||||
owner_type,
|
||||
owner_id,
|
||||
shuffle_questions,
|
||||
status
|
||||
)
|
||||
SELECT
|
||||
sc.title || ' Entry Assessment',
|
||||
'Entry assessment used to evaluate learners before joining this sub-course.',
|
||||
'INITIAL_ASSESSMENT',
|
||||
'SUB_COURSE',
|
||||
sc.id,
|
||||
false,
|
||||
'DRAFT'
|
||||
FROM sub_courses sc
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM question_sets qs
|
||||
WHERE qs.owner_type = 'SUB_COURSE'
|
||||
AND qs.owner_id = sc.id
|
||||
AND qs.set_type = 'INITIAL_ASSESSMENT'
|
||||
AND qs.status != 'ARCHIVED'
|
||||
);
|
||||
|
||||
DO $$
|
||||
DECLARE
|
||||
r RECORD;
|
||||
BEGIN
|
||||
FOR r IN
|
||||
SELECT id
|
||||
FROM question_sets
|
||||
WHERE owner_type = 'SUB_COURSE'
|
||||
AND set_type = 'INITIAL_ASSESSMENT'
|
||||
AND status != 'ARCHIVED'
|
||||
LOOP
|
||||
IF NOT EXISTS (SELECT 1 FROM question_set_items WHERE set_id = r.id) THEN
|
||||
PERFORM clone_default_initial_assessment_items(r.id);
|
||||
END IF;
|
||||
END LOOP;
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE OR REPLACE FUNCTION create_sub_course_entry_assessment()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
INSERT INTO question_sets (
|
||||
title,
|
||||
description,
|
||||
set_type,
|
||||
owner_type,
|
||||
owner_id,
|
||||
shuffle_questions,
|
||||
status
|
||||
)
|
||||
VALUES (
|
||||
NEW.title || ' Entry Assessment',
|
||||
'Entry assessment used to evaluate learners before joining this sub-course.',
|
||||
'INITIAL_ASSESSMENT',
|
||||
'SUB_COURSE',
|
||||
NEW.id,
|
||||
false,
|
||||
'DRAFT'
|
||||
)
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
PERFORM clone_default_initial_assessment_items((
|
||||
SELECT id
|
||||
FROM question_sets
|
||||
WHERE owner_type = 'SUB_COURSE'
|
||||
AND owner_id = NEW.id
|
||||
AND set_type = 'INITIAL_ASSESSMENT'
|
||||
AND status != 'ARCHIVED'
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 1
|
||||
));
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER trg_sub_courses_create_entry_assessment
|
||||
AFTER INSERT ON sub_courses
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION create_sub_course_entry_assessment();
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
DROP INDEX IF EXISTS idx_user_video_progress_sub_course;
|
||||
DROP INDEX IF EXISTS idx_user_video_progress_unique;
|
||||
|
||||
DROP TABLE IF EXISTS user_sub_course_video_progress;
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
CREATE TABLE IF NOT EXISTS user_sub_course_video_progress (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
sub_course_id BIGINT NOT NULL REFERENCES sub_courses(id) ON DELETE CASCADE,
|
||||
video_id BIGINT NOT NULL REFERENCES sub_course_videos(id) ON DELETE CASCADE,
|
||||
completed_at TIMESTAMP,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_video_progress_unique
|
||||
ON user_sub_course_video_progress(user_id, video_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_video_progress_sub_course
|
||||
ON user_sub_course_video_progress(user_id, sub_course_id);
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
DROP INDEX IF EXISTS idx_user_practice_progress_sub_course;
|
||||
DROP INDEX IF EXISTS idx_user_practice_progress_unique;
|
||||
|
||||
DROP TABLE IF EXISTS user_practice_progress;
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
CREATE TABLE IF NOT EXISTS user_practice_progress (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
sub_course_id BIGINT NOT NULL REFERENCES sub_courses(id) ON DELETE CASCADE,
|
||||
question_set_id BIGINT NOT NULL REFERENCES question_sets(id) ON DELETE CASCADE,
|
||||
completed_at TIMESTAMP,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_practice_progress_unique
|
||||
ON user_practice_progress(user_id, question_set_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_practice_progress_sub_course
|
||||
ON user_practice_progress(user_id, sub_course_id);
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
DROP INDEX IF EXISTS idx_users_deletion_due;
|
||||
DROP INDEX IF EXISTS idx_users_deletion_scheduled_at;
|
||||
|
||||
ALTER TABLE users
|
||||
DROP COLUMN IF EXISTS deletion_cancelled_at,
|
||||
DROP COLUMN IF EXISTS deletion_scheduled_at,
|
||||
DROP COLUMN IF EXISTS deletion_requested_at;
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
ALTER TABLE users
|
||||
ADD COLUMN IF NOT EXISTS deletion_requested_at TIMESTAMPTZ,
|
||||
ADD COLUMN IF NOT EXISTS deletion_scheduled_at TIMESTAMPTZ,
|
||||
ADD COLUMN IF NOT EXISTS deletion_cancelled_at TIMESTAMPTZ;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_users_deletion_scheduled_at
|
||||
ON users (deletion_scheduled_at)
|
||||
WHERE deletion_scheduled_at IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_users_deletion_due
|
||||
ON users (deletion_scheduled_at, id)
|
||||
WHERE deletion_scheduled_at IS NOT NULL;
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
DROP INDEX IF EXISTS idx_user_audio_responses_user_question;
|
||||
DROP TABLE IF EXISTS user_audio_responses;
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
-- Store learner audio answer submissions for AUDIO-type questions
|
||||
CREATE TABLE IF NOT EXISTS user_audio_responses (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
question_id BIGINT NOT NULL REFERENCES questions(id) ON DELETE CASCADE,
|
||||
question_set_id BIGINT NOT NULL REFERENCES question_sets(id) ON DELETE CASCADE,
|
||||
audio_object_key TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_audio_responses_user_question
|
||||
ON user_audio_responses(user_id, question_id, question_set_id);
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
ALTER TABLE question_sets
|
||||
DROP COLUMN IF EXISTS intro_video_url;
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
ALTER TABLE question_sets
|
||||
ADD COLUMN intro_video_url TEXT;
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
UPDATE question_sets qs
|
||||
SET owner_type = 'SUB_COURSE',
|
||||
owner_id = sm.legacy_sub_course_id
|
||||
FROM sub_modules sm
|
||||
WHERE qs.owner_type = 'SUB_MODULE'
|
||||
AND qs.owner_id = sm.id
|
||||
AND qs.set_type = 'PRACTICE'
|
||||
AND sm.legacy_sub_course_id IS NOT NULL;
|
||||
|
||||
DROP TABLE IF EXISTS sub_module_practices CASCADE;
|
||||
DROP TABLE IF EXISTS sub_module_videos CASCADE;
|
||||
DROP TABLE IF EXISTS sub_modules CASCADE;
|
||||
DROP TABLE IF EXISTS modules CASCADE;
|
||||
DROP TABLE IF EXISTS levels CASCADE;
|
||||
|
||||
ALTER TABLE courses DROP COLUMN IF EXISTS sub_category_id;
|
||||
DROP TABLE IF EXISTS course_sub_categories CASCADE;
|
||||
|
||||
-- Best-effort rollback to old expectation.
|
||||
UPDATE user_practice_progress
|
||||
SET sub_course_id = 1
|
||||
WHERE sub_course_id IS NULL;
|
||||
ALTER TABLE user_practice_progress
|
||||
ALTER COLUMN sub_course_id SET NOT NULL;
|
||||
|
||||
|
|
@ -1,228 +0,0 @@
|
|||
-- Unified hierarchy
|
||||
-- Course Category -> Course Sub-category -> Course -> Level -> Module -> Sub-Module
|
||||
-- -> Sub-Module Videos
|
||||
-- -> Sub-Module Practices (question sets)
|
||||
|
||||
CREATE TABLE IF NOT EXISTS course_sub_categories (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
category_id BIGINT NOT NULL REFERENCES course_categories(id) ON DELETE CASCADE,
|
||||
name VARCHAR(150) NOT NULL,
|
||||
description TEXT,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(category_id, name)
|
||||
);
|
||||
|
||||
ALTER TABLE courses
|
||||
ADD COLUMN IF NOT EXISTS sub_category_id BIGINT REFERENCES course_sub_categories(id) ON DELETE SET NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_courses_sub_category_id ON courses(sub_category_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS levels (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
course_id BIGINT NOT NULL REFERENCES courses(id) ON DELETE CASCADE,
|
||||
cefr_level VARCHAR(2) NOT NULL,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(course_id, cefr_level),
|
||||
CHECK (cefr_level IN ('A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_levels_course_id ON levels(course_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS modules (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
level_id BIGINT NOT NULL REFERENCES levels(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_modules_level_id ON modules(level_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sub_modules (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
module_id BIGINT NOT NULL REFERENCES modules(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
legacy_sub_course_id BIGINT UNIQUE
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_modules_module_id ON sub_modules(module_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sub_module_videos (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
sub_module_id BIGINT NOT NULL REFERENCES sub_modules(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
video_url TEXT NOT NULL,
|
||||
duration INT,
|
||||
resolution VARCHAR(20),
|
||||
is_published BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
publish_date TIMESTAMPTZ,
|
||||
visibility VARCHAR(50),
|
||||
instructor_id VARCHAR(100),
|
||||
thumbnail TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'DRAFT',
|
||||
vimeo_id TEXT,
|
||||
vimeo_embed_url TEXT,
|
||||
vimeo_player_html TEXT,
|
||||
vimeo_status VARCHAR(50),
|
||||
video_host_provider VARCHAR(20),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_module_videos_sub_module_id ON sub_module_videos(sub_module_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sub_module_practices (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
sub_module_id BIGINT NOT NULL REFERENCES sub_modules(id) ON DELETE CASCADE,
|
||||
question_set_id BIGINT NOT NULL REFERENCES question_sets(id) ON DELETE CASCADE,
|
||||
intro_video_url TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(question_set_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_module_practices_sub_module_id ON sub_module_practices(sub_module_id);
|
||||
|
||||
-- Practice progress now supports sub-module owned practices where no legacy sub_course exists.
|
||||
ALTER TABLE user_practice_progress
|
||||
ALTER COLUMN sub_course_id DROP NOT NULL;
|
||||
|
||||
-- Backfill from existing structure
|
||||
INSERT INTO course_sub_categories (category_id, name, description, display_order, is_active)
|
||||
SELECT cc.id, c.title || ' Group', 'Auto-generated from existing course structure', 0, TRUE
|
||||
FROM courses c
|
||||
JOIN course_categories cc ON cc.id = c.category_id
|
||||
LEFT JOIN course_sub_categories csc
|
||||
ON csc.category_id = cc.id AND csc.name = c.title || ' Group'
|
||||
WHERE csc.id IS NULL;
|
||||
|
||||
UPDATE courses c
|
||||
SET sub_category_id = csc.id
|
||||
FROM course_sub_categories csc
|
||||
WHERE csc.category_id = c.category_id
|
||||
AND csc.name = c.title || ' Group'
|
||||
AND c.sub_category_id IS NULL;
|
||||
|
||||
INSERT INTO levels (course_id, cefr_level, display_order, is_active)
|
||||
SELECT
|
||||
sc.course_id,
|
||||
sc.sub_level,
|
||||
MIN(sc.display_order),
|
||||
BOOL_AND(sc.is_active)
|
||||
FROM sub_courses sc
|
||||
WHERE sc.sub_level IN ('A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3')
|
||||
GROUP BY sc.course_id, sc.sub_level
|
||||
ON CONFLICT (course_id, cefr_level) DO NOTHING;
|
||||
|
||||
INSERT INTO modules (level_id, title, description, display_order, is_active)
|
||||
SELECT
|
||||
l.id,
|
||||
l.cefr_level || ' Module 1',
|
||||
'Auto-generated default module for ' || l.cefr_level,
|
||||
1,
|
||||
l.is_active
|
||||
FROM levels l
|
||||
LEFT JOIN modules m ON m.level_id = l.id AND m.display_order = 1
|
||||
WHERE m.id IS NULL;
|
||||
|
||||
INSERT INTO sub_modules (module_id, title, description, display_order, is_active, legacy_sub_course_id)
|
||||
SELECT
|
||||
m.id,
|
||||
sc.title,
|
||||
sc.description,
|
||||
sc.display_order,
|
||||
sc.is_active,
|
||||
sc.id
|
||||
FROM sub_courses sc
|
||||
JOIN levels l
|
||||
ON l.course_id = sc.course_id
|
||||
AND l.cefr_level = sc.sub_level
|
||||
JOIN modules m
|
||||
ON m.level_id = l.id
|
||||
AND m.display_order = 1
|
||||
LEFT JOIN sub_modules sm ON sm.legacy_sub_course_id = sc.id
|
||||
WHERE sm.id IS NULL;
|
||||
|
||||
INSERT INTO sub_module_videos (
|
||||
sub_module_id,
|
||||
title,
|
||||
description,
|
||||
video_url,
|
||||
duration,
|
||||
resolution,
|
||||
is_published,
|
||||
publish_date,
|
||||
visibility,
|
||||
instructor_id,
|
||||
thumbnail,
|
||||
display_order,
|
||||
status,
|
||||
vimeo_id,
|
||||
vimeo_embed_url,
|
||||
vimeo_player_html,
|
||||
vimeo_status,
|
||||
video_host_provider
|
||||
)
|
||||
SELECT
|
||||
sm.id,
|
||||
scv.title,
|
||||
scv.description,
|
||||
scv.video_url,
|
||||
scv.duration,
|
||||
scv.resolution,
|
||||
scv.is_published,
|
||||
scv.publish_date,
|
||||
scv.visibility,
|
||||
scv.instructor_id,
|
||||
scv.thumbnail,
|
||||
scv.display_order,
|
||||
scv.status,
|
||||
scv.vimeo_id,
|
||||
scv.vimeo_embed_url,
|
||||
scv.vimeo_player_html,
|
||||
scv.vimeo_status,
|
||||
scv.video_host_provider
|
||||
FROM sub_course_videos scv
|
||||
JOIN sub_modules sm ON sm.legacy_sub_course_id = scv.sub_course_id
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM sub_module_videos smv
|
||||
WHERE smv.sub_module_id = sm.id
|
||||
AND smv.title = scv.title
|
||||
AND COALESCE(smv.video_url, '') = COALESCE(scv.video_url, '')
|
||||
);
|
||||
|
||||
UPDATE question_sets qs
|
||||
SET owner_type = 'SUB_MODULE',
|
||||
owner_id = sm.id
|
||||
FROM sub_modules sm
|
||||
WHERE qs.owner_type = 'SUB_COURSE'
|
||||
AND qs.owner_id = sm.legacy_sub_course_id
|
||||
AND qs.set_type = 'PRACTICE';
|
||||
|
||||
INSERT INTO sub_module_practices (sub_module_id, question_set_id, intro_video_url, display_order, is_active)
|
||||
SELECT
|
||||
sm.id,
|
||||
qs.id,
|
||||
qs.intro_video_url,
|
||||
COALESCE(qs.display_order, 0),
|
||||
(qs.status != 'ARCHIVED')
|
||||
FROM question_sets qs
|
||||
JOIN sub_modules sm
|
||||
ON qs.owner_type = 'SUB_MODULE'
|
||||
AND qs.owner_id = sm.id
|
||||
WHERE qs.set_type = 'PRACTICE'
|
||||
ON CONFLICT (question_set_id) DO NOTHING;
|
||||
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
DROP INDEX IF EXISTS idx_sub_module_lessons_sub_module_id;
|
||||
|
||||
DROP TABLE IF EXISTS sub_module_lessons;
|
||||
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
-- Keep practices as a separate feature and introduce lessons as a new table.
|
||||
CREATE TABLE IF NOT EXISTS sub_module_lessons (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
sub_module_id BIGINT NOT NULL REFERENCES sub_modules(id) ON DELETE CASCADE,
|
||||
question_set_id BIGINT NOT NULL REFERENCES question_sets(id) ON DELETE CASCADE,
|
||||
intro_video_url TEXT,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(question_set_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_module_lessons_sub_module_id
|
||||
ON sub_module_lessons(sub_module_id);
|
||||
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
DROP INDEX IF EXISTS idx_sub_module_practices_sub_module_id;
|
||||
|
||||
DROP TABLE IF EXISTS sub_module_practices;
|
||||
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
CREATE TABLE IF NOT EXISTS sub_module_practices (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
sub_module_id BIGINT NOT NULL REFERENCES sub_modules(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
thumbnail TEXT,
|
||||
intro_video_url TEXT,
|
||||
question_set_id BIGINT NOT NULL REFERENCES question_sets(id) ON DELETE CASCADE,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(question_set_id)
|
||||
);
|
||||
|
||||
-- If the table already existed from older unified hierarchy migrations,
|
||||
-- backfill missing columns so practices keep their own richer schema.
|
||||
ALTER TABLE sub_module_practices
|
||||
ADD COLUMN IF NOT EXISTS title VARCHAR(255);
|
||||
|
||||
ALTER TABLE sub_module_practices
|
||||
ADD COLUMN IF NOT EXISTS description TEXT;
|
||||
|
||||
ALTER TABLE sub_module_practices
|
||||
ADD COLUMN IF NOT EXISTS thumbnail TEXT;
|
||||
|
||||
UPDATE sub_module_practices
|
||||
SET title = COALESCE(NULLIF(title, ''), 'Practice')
|
||||
WHERE title IS NULL OR title = '';
|
||||
|
||||
ALTER TABLE sub_module_practices
|
||||
ALTER COLUMN title SET NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_module_practices_sub_module_id
|
||||
ON sub_module_practices(sub_module_id);
|
||||
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
-- Restores legacy lesson columns. Rows will have NULL question_set_id until repopulated.
|
||||
|
||||
ALTER TABLE sub_module_lessons
|
||||
ADD COLUMN IF NOT EXISTS question_set_id BIGINT REFERENCES question_sets(id) ON DELETE CASCADE,
|
||||
ADD COLUMN IF NOT EXISTS intro_video_url TEXT;
|
||||
|
||||
UPDATE sub_module_lessons
|
||||
SET intro_video_url = teaching_video_url
|
||||
WHERE teaching_video_url IS NOT NULL;
|
||||
|
||||
ALTER TABLE sub_module_lessons
|
||||
DROP COLUMN IF EXISTS title,
|
||||
DROP COLUMN IF EXISTS description,
|
||||
DROP COLUMN IF EXISTS thumbnail,
|
||||
DROP COLUMN IF EXISTS teaching_text,
|
||||
DROP COLUMN IF EXISTS teaching_image_url,
|
||||
DROP COLUMN IF EXISTS teaching_audio_url,
|
||||
DROP COLUMN IF EXISTS teaching_video_url;
|
||||
|
|
@ -1,37 +0,0 @@
|
|||
-- Lessons are teaching content only (text, images, audio, video, thumbnail).
|
||||
-- Question sets remain linked to practices, not lessons.
|
||||
|
||||
ALTER TABLE sub_module_lessons
|
||||
ADD COLUMN IF NOT EXISTS title VARCHAR(255),
|
||||
ADD COLUMN IF NOT EXISTS description TEXT,
|
||||
ADD COLUMN IF NOT EXISTS thumbnail TEXT,
|
||||
ADD COLUMN IF NOT EXISTS teaching_text TEXT,
|
||||
ADD COLUMN IF NOT EXISTS teaching_image_url TEXT,
|
||||
ADD COLUMN IF NOT EXISTS teaching_audio_url TEXT,
|
||||
ADD COLUMN IF NOT EXISTS teaching_video_url TEXT;
|
||||
|
||||
UPDATE sub_module_lessons sml
|
||||
SET
|
||||
title = qs.title,
|
||||
description = qs.description
|
||||
FROM question_sets qs
|
||||
WHERE sml.question_set_id IS NOT NULL
|
||||
AND qs.id = sml.question_set_id;
|
||||
|
||||
UPDATE sub_module_lessons
|
||||
SET title = 'Lesson'
|
||||
WHERE title IS NULL OR trim(title) = '';
|
||||
|
||||
UPDATE sub_module_lessons
|
||||
SET teaching_video_url = intro_video_url
|
||||
WHERE intro_video_url IS NOT NULL;
|
||||
|
||||
ALTER TABLE sub_module_lessons DROP CONSTRAINT IF EXISTS sub_module_lessons_question_set_id_fkey;
|
||||
ALTER TABLE sub_module_lessons DROP CONSTRAINT IF EXISTS sub_module_lessons_question_set_id_key;
|
||||
|
||||
ALTER TABLE sub_module_lessons DROP COLUMN IF EXISTS question_set_id;
|
||||
ALTER TABLE sub_module_lessons DROP COLUMN IF EXISTS intro_video_url;
|
||||
|
||||
ALTER TABLE sub_module_lessons
|
||||
ALTER COLUMN title SET NOT NULL,
|
||||
ALTER COLUMN title SET DEFAULT 'Lesson';
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
ALTER TABLE levels
|
||||
DROP COLUMN IF EXISTS title,
|
||||
DROP COLUMN IF EXISTS description,
|
||||
DROP COLUMN IF EXISTS thumbnail;
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
ALTER TABLE levels
|
||||
ADD COLUMN IF NOT EXISTS title VARCHAR(255),
|
||||
ADD COLUMN IF NOT EXISTS description TEXT,
|
||||
ADD COLUMN IF NOT EXISTS thumbnail TEXT;
|
||||
|
||||
UPDATE levels
|
||||
SET title = cefr_level
|
||||
WHERE title IS NULL OR trim(title) = '';
|
||||
|
||||
ALTER TABLE levels
|
||||
ALTER COLUMN title SET NOT NULL;
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
DROP INDEX IF EXISTS idx_sub_module_capstones_sub_module_id;
|
||||
DROP TABLE IF EXISTS sub_module_capstones;
|
||||
|
||||
ALTER TABLE question_sets DROP CONSTRAINT IF EXISTS question_sets_set_type_check;
|
||||
ALTER TABLE question_sets ADD CONSTRAINT question_sets_set_type_check
|
||||
CHECK (set_type IN (
|
||||
'PRACTICE',
|
||||
'INITIAL_ASSESSMENT',
|
||||
'QUIZ',
|
||||
'EXAM',
|
||||
'SURVEY'
|
||||
));
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
-- Capstone assessments: sub-module scoped, backed by question_sets (type CAPSTONE).
|
||||
|
||||
ALTER TABLE question_sets DROP CONSTRAINT IF EXISTS question_sets_set_type_check;
|
||||
ALTER TABLE question_sets ADD CONSTRAINT question_sets_set_type_check
|
||||
CHECK (set_type IN (
|
||||
'PRACTICE',
|
||||
'INITIAL_ASSESSMENT',
|
||||
'QUIZ',
|
||||
'EXAM',
|
||||
'SURVEY',
|
||||
'CAPSTONE'
|
||||
));
|
||||
|
||||
CREATE TABLE IF NOT EXISTS sub_module_capstones (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
sub_module_id BIGINT NOT NULL REFERENCES sub_modules(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
tips TEXT,
|
||||
thumbnail TEXT,
|
||||
question_set_id BIGINT NOT NULL REFERENCES question_sets(id) ON DELETE CASCADE,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE (question_set_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sub_module_capstones_sub_module_id
|
||||
ON sub_module_capstones (sub_module_id);
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
DROP INDEX IF EXISTS idx_module_capstones_module_id;
|
||||
DROP TABLE IF EXISTS module_capstones;
|
||||
|
||||
ALTER TABLE modules DROP COLUMN IF EXISTS icon_url;
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
ALTER TABLE modules
|
||||
ADD COLUMN IF NOT EXISTS icon_url TEXT;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS module_capstones (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
module_id BIGINT NOT NULL REFERENCES modules(id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
tips TEXT,
|
||||
thumbnail TEXT,
|
||||
question_set_id BIGINT NOT NULL REFERENCES question_sets(id) ON DELETE CASCADE,
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE (question_set_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_module_capstones_module_id
|
||||
ON module_capstones (module_id);
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
ALTER TABLE sub_modules
|
||||
DROP COLUMN IF EXISTS tips,
|
||||
DROP COLUMN IF EXISTS thumbnail;
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
ALTER TABLE sub_modules
|
||||
ADD COLUMN IF NOT EXISTS thumbnail TEXT,
|
||||
ADD COLUMN IF NOT EXISTS tips TEXT;
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
-- Restores fixed CEFR list; fails if any row has cefr_level outside the old set or longer than 2 characters.
|
||||
ALTER TABLE levels
|
||||
ALTER COLUMN cefr_level TYPE VARCHAR(2);
|
||||
|
||||
ALTER TABLE levels
|
||||
ADD CONSTRAINT levels_cefr_level_check
|
||||
CHECK (cefr_level IN ('A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3'));
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
-- Allow arbitrary level codes/labels per course (not only fixed CEFR bands).
|
||||
DO $$
|
||||
DECLARE
|
||||
con_name text;
|
||||
BEGIN
|
||||
SELECT c.conname INTO con_name
|
||||
FROM pg_constraint c
|
||||
JOIN pg_class t ON c.conrelid = t.oid
|
||||
JOIN pg_namespace n ON t.relnamespace = n.oid
|
||||
WHERE n.nspname = current_schema()
|
||||
AND t.relname = 'levels'
|
||||
AND c.contype = 'c'
|
||||
AND pg_get_constraintdef(c.oid) LIKE '%cefr_level%IN (%A1%';
|
||||
IF con_name IS NOT NULL THEN
|
||||
EXECUTE format('ALTER TABLE levels DROP CONSTRAINT %I', con_name);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
ALTER TABLE levels
|
||||
ALTER COLUMN cefr_level TYPE VARCHAR(64);
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
DROP INDEX IF EXISTS idx_team_refresh_tokens_team_member_id;
|
||||
DROP TABLE IF EXISTS team_refresh_tokens;
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
CREATE TABLE IF NOT EXISTS team_refresh_tokens (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
team_member_id BIGINT NOT NULL REFERENCES team_members(id) ON DELETE CASCADE,
|
||||
token TEXT NOT NULL UNIQUE,
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
revoked BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_team_refresh_tokens_team_member_id
|
||||
ON team_refresh_tokens (team_member_id);
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
ALTER TABLE sub_module_lessons DROP COLUMN IF EXISTS inactive_since;
|
||||
ALTER TABLE sub_module_practices DROP COLUMN IF EXISTS inactive_since;
|
||||
ALTER TABLE sub_module_capstones DROP COLUMN IF EXISTS inactive_since;
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
-- Track when submodule lessons, practices, and capstones became inactive for retention-based hard delete.
|
||||
|
||||
ALTER TABLE sub_module_lessons
|
||||
ADD COLUMN IF NOT EXISTS inactive_since TIMESTAMPTZ;
|
||||
|
||||
ALTER TABLE sub_module_practices
|
||||
ADD COLUMN IF NOT EXISTS inactive_since TIMESTAMPTZ;
|
||||
|
||||
ALTER TABLE sub_module_capstones
|
||||
ADD COLUMN IF NOT EXISTS inactive_since TIMESTAMPTZ;
|
||||
|
||||
-- Existing inactive rows: start retention window from migration time (conservative).
|
||||
UPDATE sub_module_lessons
|
||||
SET inactive_since = NOW()
|
||||
WHERE is_active = FALSE
|
||||
AND inactive_since IS NULL;
|
||||
|
||||
UPDATE sub_module_practices
|
||||
SET inactive_since = NOW()
|
||||
WHERE is_active = FALSE
|
||||
AND inactive_since IS NULL;
|
||||
|
||||
UPDATE sub_module_capstones
|
||||
SET inactive_since = NOW()
|
||||
WHERE is_active = FALSE
|
||||
AND inactive_since IS NULL;
|
||||
|
|
@ -1 +0,0 @@
|
|||
-- Restoring the removed course hierarchy is not supported; apply new migrations for the next model.
|
||||
|
|
@ -1,46 +0,0 @@
|
|||
-- Tear down the legacy course / learning-tree schema so a new hierarchy can be introduced.
|
||||
BEGIN;
|
||||
|
||||
-- Entry-assessment automation on sub_courses (from 000024)
|
||||
DROP TRIGGER IF EXISTS trg_sub_courses_create_entry_assessment ON sub_courses;
|
||||
DROP FUNCTION IF EXISTS create_sub_course_entry_assessment();
|
||||
DROP FUNCTION IF EXISTS clone_default_initial_assessment_items(BIGINT);
|
||||
DROP INDEX IF EXISTS idx_question_sets_unique_subcourse_initial_assessment;
|
||||
|
||||
ALTER TABLE question_sets DROP COLUMN IF EXISTS sub_course_video_id;
|
||||
|
||||
-- Dependent objects first
|
||||
DROP TABLE IF EXISTS user_sub_course_video_progress CASCADE;
|
||||
DROP TABLE IF EXISTS user_practice_progress CASCADE;
|
||||
DROP TABLE IF EXISTS sub_course_prerequisites CASCADE;
|
||||
DROP TABLE IF EXISTS user_sub_course_progress CASCADE;
|
||||
|
||||
DROP TABLE IF EXISTS sub_module_practices CASCADE;
|
||||
DROP TABLE IF EXISTS sub_module_capstones CASCADE;
|
||||
DROP TABLE IF EXISTS sub_module_lessons CASCADE;
|
||||
DROP TABLE IF EXISTS sub_module_videos CASCADE;
|
||||
DROP TABLE IF EXISTS sub_modules CASCADE;
|
||||
DROP TABLE IF EXISTS module_capstones CASCADE;
|
||||
DROP TABLE IF EXISTS modules CASCADE;
|
||||
DROP TABLE IF EXISTS levels CASCADE;
|
||||
|
||||
DROP TABLE IF EXISTS sub_course_videos CASCADE;
|
||||
DROP TABLE IF EXISTS sub_courses CASCADE;
|
||||
DROP TABLE IF EXISTS course_sub_categories CASCADE;
|
||||
DROP TABLE IF EXISTS courses CASCADE;
|
||||
DROP TABLE IF EXISTS course_categories CASCADE;
|
||||
|
||||
-- Keep learner practice completion for the questions system (no sub_course column)
|
||||
CREATE TABLE user_practice_progress (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
question_set_id BIGINT NOT NULL REFERENCES question_sets(id) ON DELETE CASCADE,
|
||||
completed_at TIMESTAMPTZ,
|
||||
updated_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE (user_id, question_set_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_user_practice_progress_user_id ON user_practice_progress(user_id);
|
||||
|
||||
COMMIT;
|
||||
|
|
@ -1 +0,0 @@
|
|||
DROP TABLE IF EXISTS programs;
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
-- Top-level LMS program (e.g. Beginner / Intermediate / Advanced — labels come from admin config later).
|
||||
CREATE TABLE programs (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
thumbnail TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX idx_programs_created_at ON programs (created_at DESC);
|
||||
|
|
@ -1,4 +0,0 @@
|
|||
DELETE FROM programs
|
||||
WHERE (name = 'Beginner' AND description = 'Default program for the beginner level.')
|
||||
OR (name = 'Intermediate' AND description = 'Default program for the intermediate level.')
|
||||
OR (name = 'Advanced' AND description = 'Default program for the advanced level.');
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
-- Default top-level programs (hierarchy: Program → Course → …).
|
||||
INSERT INTO programs (name, description, thumbnail)
|
||||
VALUES
|
||||
('Beginner', 'Default program for the beginner level.', NULL),
|
||||
('Intermediate', 'Default program for the intermediate level.', NULL),
|
||||
('Advanced', 'Default program for the advanced level.', NULL);
|
||||
|
|
@ -1 +0,0 @@
|
|||
DROP TABLE IF EXISTS courses;
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
-- Courses belong to a Program (CEFR-style labels like A1..C2 will be configured separately).
|
||||
CREATE TABLE courses (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
program_id BIGINT NOT NULL REFERENCES programs (id) ON DELETE CASCADE,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
thumbnail TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX idx_courses_program_id ON courses (program_id);
|
||||
CREATE INDEX idx_courses_program_created ON courses (program_id, created_at DESC);
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
DROP TABLE IF EXISTS modules;
|
||||
ALTER TABLE courses DROP CONSTRAINT IF EXISTS courses_program_id_id_key;
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
-- Modules belong to a Course; program_id is denormalized and enforced with the course by a composite FK.
|
||||
ALTER TABLE courses
|
||||
ADD CONSTRAINT courses_program_id_id_key UNIQUE (program_id, id);
|
||||
|
||||
CREATE TABLE modules (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
program_id BIGINT NOT NULL,
|
||||
course_id BIGINT NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT,
|
||||
icon TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ,
|
||||
CONSTRAINT modules_course_scope_fkey
|
||||
FOREIGN KEY (program_id, course_id)
|
||||
REFERENCES courses (program_id, id)
|
||||
ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX idx_modules_course_id ON modules (course_id);
|
||||
CREATE INDEX idx_modules_program_id ON modules (program_id);
|
||||
CREATE INDEX idx_modules_program_course_created ON modules (program_id, course_id, created_at DESC);
|
||||
|
|
@ -1 +0,0 @@
|
|||
DROP TABLE IF EXISTS lessons;
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
-- Lessons belong to a Module.
|
||||
CREATE TABLE lessons (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
module_id BIGINT NOT NULL REFERENCES modules (id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
video_url TEXT,
|
||||
thumbnail TEXT,
|
||||
description TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
CREATE INDEX idx_lessons_module_id ON lessons (module_id);
|
||||
CREATE INDEX idx_lessons_module_created ON lessons (module_id, created_at DESC);
|
||||
|
|
@ -1 +0,0 @@
|
|||
DROP TABLE IF EXISTS lms_practices;
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
-- Practices attach to exactly one of: course, module, or lesson.
|
||||
CREATE TABLE lms_practices (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
course_id BIGINT REFERENCES courses (id) ON DELETE CASCADE,
|
||||
module_id BIGINT REFERENCES modules (id) ON DELETE CASCADE,
|
||||
lesson_id BIGINT REFERENCES lessons (id) ON DELETE CASCADE,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
story_description TEXT,
|
||||
story_image TEXT,
|
||||
persona_id BIGINT REFERENCES users (id) ON DELETE SET NULL,
|
||||
question_set_id BIGINT NOT NULL REFERENCES question_sets (id) ON DELETE RESTRICT,
|
||||
quick_tips TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMPTZ,
|
||||
CONSTRAINT lms_practices_one_parent CHECK (
|
||||
(course_id IS NOT NULL)::int
|
||||
+ (module_id IS NOT NULL)::int
|
||||
+ (lesson_id IS NOT NULL)::int
|
||||
= 1
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_lms_practices_course_id ON lms_practices (course_id);
|
||||
CREATE INDEX idx_lms_practices_module_id ON lms_practices (module_id);
|
||||
CREATE INDEX idx_lms_practices_lesson_id ON lms_practices (lesson_id);
|
||||
CREATE INDEX idx_lms_practices_question_set_id ON lms_practices (question_set_id);
|
||||
CREATE INDEX idx_lms_practices_course_created ON lms_practices (course_id, created_at DESC);
|
||||
CREATE INDEX idx_lms_practices_module_created ON lms_practices (module_id, created_at DESC);
|
||||
CREATE INDEX idx_lms_practices_lesson_created ON lms_practices (lesson_id, created_at DESC);
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
DELETE FROM courses
|
||||
WHERE description = 'Default CEFR level course (system seed).'
|
||||
AND name IN ('A1', 'A2', 'B1', 'B2', 'C1', 'C2');
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
-- Default CEFR-style courses per seeded program: Beginner→A1,A2; Intermediate→B1,B2; Advanced→C1,C2.
|
||||
-- Custom courses can still be created via the API with any name.
|
||||
INSERT INTO courses (program_id, name, description, thumbnail)
|
||||
SELECT
|
||||
p.id,
|
||||
v.name,
|
||||
'Default CEFR level course (system seed).',
|
||||
NULL
|
||||
FROM programs AS p
|
||||
INNER JOIN (
|
||||
VALUES
|
||||
('Beginner', 'A1'),
|
||||
('Beginner', 'A2'),
|
||||
('Intermediate', 'B1'),
|
||||
('Intermediate', 'B2'),
|
||||
('Advanced', 'C1'),
|
||||
('Advanced', 'C2')
|
||||
) AS v (program_name, name)
|
||||
ON p.name = v.program_name;
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
DROP TABLE IF EXISTS lms_user_program_progress;
|
||||
DROP TABLE IF EXISTS lms_user_course_progress;
|
||||
DROP TABLE IF EXISTS lms_user_module_progress;
|
||||
DROP TABLE IF EXISTS lms_user_lesson_progress;
|
||||
|
||||
DROP INDEX IF EXISTS uq_lessons_module_sort;
|
||||
DROP INDEX IF EXISTS uq_modules_course_sort;
|
||||
DROP INDEX IF EXISTS uq_courses_program_sort;
|
||||
DROP INDEX IF EXISTS uq_programs_sort_order;
|
||||
|
||||
ALTER TABLE lessons
|
||||
DROP COLUMN IF EXISTS sort_order;
|
||||
ALTER TABLE modules
|
||||
DROP COLUMN IF EXISTS sort_order;
|
||||
ALTER TABLE courses
|
||||
DROP COLUMN IF EXISTS sort_order;
|
||||
ALTER TABLE programs
|
||||
DROP COLUMN IF EXISTS sort_order;
|
||||
|
|
@ -1,150 +0,0 @@
|
|||
-- Sequential order for programs, courses, modules, and lessons (1 = first in each scope).
|
||||
-- Progress tables mark completion; API enforces prerequisites for learners (STUDENT role).
|
||||
|
||||
ALTER TABLE programs
|
||||
ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0;
|
||||
ALTER TABLE courses
|
||||
ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0;
|
||||
ALTER TABLE modules
|
||||
ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0;
|
||||
ALTER TABLE lessons
|
||||
ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
-- Program order (one global sequence): Beginner -> Intermediate -> Advanced; others by id
|
||||
UPDATE programs
|
||||
SET sort_order = v.so
|
||||
FROM (
|
||||
VALUES
|
||||
('Beginner', 1),
|
||||
('Intermediate', 2),
|
||||
('Advanced', 3)
|
||||
) AS v (name, so)
|
||||
WHERE programs.name = v.name;
|
||||
|
||||
UPDATE programs
|
||||
SET sort_order = 1000 + r.rn
|
||||
FROM (
|
||||
SELECT
|
||||
id,
|
||||
row_number() OVER (
|
||||
ORDER BY id
|
||||
) AS rn
|
||||
FROM programs
|
||||
WHERE
|
||||
sort_order = 0
|
||||
) AS r
|
||||
WHERE
|
||||
programs.id = r.id;
|
||||
|
||||
-- CEFR courses: A1..C2; remaining courses in each program: stable order
|
||||
UPDATE courses
|
||||
SET sort_order = CASE name
|
||||
WHEN 'A1' THEN
|
||||
1
|
||||
WHEN 'A2' THEN
|
||||
2
|
||||
WHEN 'B1' THEN
|
||||
3
|
||||
WHEN 'B2' THEN
|
||||
4
|
||||
WHEN 'C1' THEN
|
||||
5
|
||||
WHEN 'C2' THEN
|
||||
6
|
||||
ELSE
|
||||
0
|
||||
END
|
||||
WHERE
|
||||
name IN ('A1', 'A2', 'B1', 'B2', 'C1', 'C2');
|
||||
|
||||
UPDATE courses c
|
||||
SET sort_order = 2000 + s.rn
|
||||
FROM (
|
||||
SELECT
|
||||
id,
|
||||
row_number() OVER (
|
||||
PARTITION BY program_id
|
||||
ORDER BY
|
||||
id
|
||||
) AS rn
|
||||
FROM courses
|
||||
WHERE
|
||||
sort_order = 0
|
||||
) AS s
|
||||
WHERE
|
||||
c.id = s.id;
|
||||
|
||||
UPDATE modules m
|
||||
SET sort_order = r.rn
|
||||
FROM (
|
||||
SELECT
|
||||
id,
|
||||
row_number() OVER (
|
||||
PARTITION BY course_id
|
||||
ORDER BY
|
||||
id
|
||||
) AS rn
|
||||
FROM modules
|
||||
) AS r
|
||||
WHERE
|
||||
m.id = r.id;
|
||||
|
||||
UPDATE lessons l
|
||||
SET sort_order = r.rn
|
||||
FROM (
|
||||
SELECT
|
||||
id,
|
||||
row_number() OVER (
|
||||
PARTITION BY module_id
|
||||
ORDER BY
|
||||
id
|
||||
) AS rn
|
||||
FROM lessons
|
||||
) AS r
|
||||
WHERE
|
||||
l.id = r.id;
|
||||
|
||||
CREATE UNIQUE INDEX uq_programs_sort_order ON programs (sort_order);
|
||||
CREATE UNIQUE INDEX uq_courses_program_sort ON courses (program_id, sort_order);
|
||||
CREATE UNIQUE INDEX uq_modules_course_sort ON modules (course_id, sort_order);
|
||||
CREATE UNIQUE INDEX uq_lessons_module_sort ON lessons (module_id, sort_order);
|
||||
|
||||
CREATE TABLE lms_user_lesson_progress (
|
||||
user_id BIGINT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
lesson_id BIGINT NOT NULL REFERENCES lessons (id) ON DELETE CASCADE,
|
||||
completed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (user_id, lesson_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_lms_user_lesson_progress_user ON lms_user_lesson_progress (user_id);
|
||||
CREATE INDEX idx_lms_user_lesson_progress_lesson ON lms_user_lesson_progress (lesson_id);
|
||||
|
||||
CREATE TABLE lms_user_module_progress (
|
||||
user_id BIGINT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
module_id BIGINT NOT NULL REFERENCES modules (id) ON DELETE CASCADE,
|
||||
completed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (user_id, module_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_lms_user_module_progress_user ON lms_user_module_progress (user_id);
|
||||
CREATE INDEX idx_lms_user_module_progress_module ON lms_user_module_progress (module_id);
|
||||
|
||||
CREATE TABLE lms_user_course_progress (
|
||||
user_id BIGINT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
course_id BIGINT NOT NULL REFERENCES courses (id) ON DELETE CASCADE,
|
||||
completed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (user_id, course_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_lms_user_course_progress_user ON lms_user_course_progress (user_id);
|
||||
CREATE INDEX idx_lms_user_course_progress_course ON lms_user_course_progress (course_id);
|
||||
|
||||
CREATE TABLE lms_user_program_progress (
|
||||
user_id BIGINT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
program_id BIGINT NOT NULL REFERENCES programs (id) ON DELETE CASCADE,
|
||||
completed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (user_id, program_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_lms_user_program_progress_user ON lms_user_program_progress (user_id);
|
||||
CREATE INDEX idx_lms_user_program_progress_program ON lms_user_program_progress (program_id);
|
||||
|
|
@ -1 +0,0 @@
|
|||
-- Data cleanup is not reversed; restoring the old cross-product seed would be ambiguous.
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user