RezinaLMS-BackEnd/db/migrations/000102_initial_assessment_per_level.up.sql
2026-08-03 11:04:19 -07:00

77 lines
2.6 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Progressive placement: one INITIAL_ASSESSMENT question set per CEFR level (description = A1–C2).
-- Threshold min_percent becomes the pass mark for that level's set.
-- Normalize legacy single "PLACEMENT" / empty sets onto A1, keep at most one published A1.
WITH ranked AS (
SELECT
id,
ROW_NUMBER() OVER (
ORDER BY
CASE WHEN UPPER(TRIM(status)) = 'PUBLISHED' THEN 0 ELSE 1 END,
created_at DESC
) AS rn
FROM question_sets
WHERE set_type = 'INITIAL_ASSESSMENT'
)
UPDATE question_sets qs
SET
description = 'A1',
status = CASE
WHEN ranked.rn = 1 THEN qs.status
WHEN UPPER(TRIM(qs.status)) = 'PUBLISHED' THEN 'DRAFT'
ELSE qs.status
END,
updated_at = NOW()
FROM ranked
WHERE qs.id = ranked.id
AND (
qs.description IS NULL
OR TRIM(qs.description) = ''
OR UPPER(TRIM(qs.description)) = 'PLACEMENT'
OR UPPER(TRIM(qs.description)) NOT IN ('A1', 'A2', 'B1', 'B2', 'C1', 'C2')
);
WITH pubs AS (
SELECT
id,
UPPER(TRIM(description)) AS level,
ROW_NUMBER() OVER (
PARTITION BY UPPER(TRIM(description))
ORDER BY created_at DESC
) AS rn
FROM question_sets
WHERE set_type = 'INITIAL_ASSESSMENT'
AND UPPER(TRIM(status)) = 'PUBLISHED'
AND UPPER(TRIM(description)) IN ('A1', 'A2', 'B1', 'B2', 'C1', 'C2')
)
UPDATE question_sets qs
SET status = 'DRAFT', updated_at = NOW()
FROM pubs
WHERE qs.id = pubs.id
AND pubs.rn > 1;
CREATE UNIQUE INDEX IF NOT EXISTS idx_initial_assessment_one_published_per_level
ON question_sets ((UPPER(TRIM(description))))
WHERE set_type = 'INITIAL_ASSESSMENT'
AND UPPER(TRIM(status)) = 'PUBLISHED'
AND UPPER(TRIM(description)) IN ('A1', 'A2', 'B1', 'B2', 'C1', 'C2');
-- Pass marks for each level set (replaces single-test score banding defaults).
UPDATE initial_assessment_level_thresholds
SET min_percent = 70, updated_at = NOW();
CREATE TABLE IF NOT EXISTS user_initial_assessment_progress (
user_id BIGINT PRIMARY KEY REFERENCES users (id) ON DELETE CASCADE,
next_level VARCHAR(8) NOT NULL DEFAULT 'A1',
highest_passed_level VARCHAR(8),
completed BOOLEAN NOT NULL DEFAULT FALSE,
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT user_initial_assessment_progress_next_level_check
CHECK (UPPER(TRIM(next_level)) IN ('A1', 'A2', 'B1', 'B2', 'C1', 'C2')),
CONSTRAINT user_initial_assessment_progress_highest_passed_check
CHECK (
highest_passed_level IS NULL
OR UPPER(TRIM(highest_passed_level)) IN ('A1', 'A2', 'B1', 'B2', 'C1', 'C2')
)
);