studium/db/migrations/20260928150000_course_page.sql
Antoine Pelletier 08b69851cd wip
2026-09-29 08:57:04 +02:00

70 lines
2.6 KiB
SQL

-- migrate:up
-- Tags are shared by the time slots and the tasks
ALTER TABLE slot_tags RENAME TO tags;
ALTER TABLE tags RENAME CONSTRAINT slot_tags_pkey TO tags_pkey;
ALTER SEQUENCE slot_tags_id_seq RENAME TO tags_id_seq;
INSERT INTO tags ("name", color) VALUES
('{"fr": "Devoir", "en": "Homework"}', '#9333ea'),
('{"fr": "Examen", "en": "Exam"}', '#dc2626'),
('{"fr": "Projet", "en": "Project"}', '#0891b2');
-- Grades of a course, on any scale: `score` out of `max_score`. The weights are relative
-- to each other, the average of the course is weighted by them.
CREATE TABLE grades (
id SERIAL PRIMARY KEY,
course_id INTEGER NOT NULL REFERENCES courses (id) ON DELETE CASCADE,
"name" TEXT NOT NULL,
score DOUBLE PRECISION NOT NULL CHECK (score >= 0),
max_score DOUBLE PRECISION NOT NULL CHECK (max_score > 0),
weight DOUBLE PRECISION NOT NULL CHECK (weight > 0),
CHECK (score <= max_score)
);
CREATE INDEX grades_course_id_idx ON grades (course_id);
-- Homework, exams, or anything to do for a course. A task may have subtasks (one level).
CREATE TABLE tasks (
id SERIAL PRIMARY KEY,
course_id INTEGER NOT NULL REFERENCES courses (id) ON DELETE CASCADE,
parent_id INTEGER REFERENCES tasks (id) ON DELETE CASCADE,
"name" TEXT NOT NULL,
description TEXT,
links TEXT[] NOT NULL DEFAULT '{}',
deadline TIMESTAMPTZ,
done BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE INDEX tasks_course_id_idx ON tasks (course_id);
CREATE TABLE task_tags (
task_id INTEGER NOT NULL REFERENCES tasks (id) ON DELETE CASCADE,
tag_id INTEGER NOT NULL REFERENCES tags (id) ON DELETE CASCADE,
PRIMARY KEY (task_id, tag_id)
);
-- Lecture notes written in markdown. The content (.md) and the rendered pdf are files
-- in the storage, not in the database.
CREATE TABLE lecture_notes (
id SERIAL PRIMARY KEY,
course_id INTEGER NOT NULL REFERENCES courses (id) ON DELETE CASCADE,
title TEXT NOT NULL,
-- False when the last rendering to pdf failed
has_pdf BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX lecture_notes_course_id_idx ON lecture_notes (course_id);
-- migrate:down
DROP TABLE IF EXISTS lecture_notes;
DROP TABLE IF EXISTS task_tags;
DROP TABLE IF EXISTS tasks;
DROP TABLE IF EXISTS grades;
DELETE FROM tags WHERE id NOT IN (SELECT DISTINCT tag_id FROM time_slots) AND id > 3;
ALTER SEQUENCE tags_id_seq RENAME TO slot_tags_id_seq;
ALTER TABLE tags RENAME CONSTRAINT tags_pkey TO slot_tags_pkey;
ALTER TABLE tags RENAME TO slot_tags;