Fold the course-template migrations into the base schema
Browse files at this commitd20043a5e1c61b9fb0925c330a17e60c07d0567eParent: f151345- chore
- fold the course-template migrations into the base schema
2 changed files +148 -27
- migrations/school/20260912000002_courses.sql +11 -12
- migrations/school/20260916000001_k12_remodel.sql +137 -15
migrations/school/20260912000002_courses.sqlmodified+11 -12
| @@ -150,7 +150,6 @@ CREATE INDEX session_attendance_course ON session_attendance (course); | ||
| 150 | 150 | CREATE TABLE class_group ( |
| 151 | 151 | id uuid PRIMARY KEY, |
| 152 | 152 | name TEXT NOT NULL, |
| 153 | - grade TEXT NULL, | |
| 154 | 153 | term uuid NULL REFERENCES term(id) ON DELETE NO ACTION, |
| 155 | 154 | creator uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, |
| 156 | 155 | teacher uuid NULL REFERENCES app_user(id) ON DELETE NO ACTION, |
| @@ -158,18 +157,18 @@ CREATE TABLE class_group ( | ||
| 158 | 157 | class_course_count BIGINT NOT NULL DEFAULT 0 |
| 159 | 158 | ); |
| 160 | 159 | |
| 161 | -CREATE INDEX class_group_grade ON class_group (grade); | |
| 162 | - | |
| 163 | --- A grade's course template. The grade label is the identity the whole API | |
| 164 | --- speaks and it stays UNIQUE, but the row carries a surrogate uuid PK: it is | |
| 165 | --- what class_course.source references, and a delete-and-recreate of the same | |
| 166 | --- grade mints a new id, so links tagged by the old one were swept by that | |
| 167 | --- delete instead of being adopted by the new blueprint (a TEXT grade tag | |
| 168 | --- made the two indistinguishable). | |
| 160 | +-- A grade-level course template. The grade level is the identity the whole | |
| 161 | +-- API speaks and it stays UNIQUE, but the row carries a surrogate uuid PK: | |
| 162 | +-- it is what class_course.source references, and a delete-and-recreate of | |
| 163 | +-- the same grade mints a new id, so links tagged by the old one were swept | |
| 164 | +-- by that delete instead of being adopted by the new blueprint. The UNIQUE | |
| 165 | +-- name is load-bearing: db::class_blueprint matches it on SQLSTATE 23505. | |
| 169 | 166 | CREATE TABLE class_blueprint ( |
| 170 | - id uuid PRIMARY KEY, | |
| 171 | - grade TEXT NOT NULL CONSTRAINT class_blueprint_grade_key UNIQUE, | |
| 172 | - creator uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION | |
| 167 | + id uuid PRIMARY KEY, | |
| 168 | + creator uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, | |
| 169 | + -- 0 = anaokulu, 1..12 = 1. sinif..12. sinif. | |
| 170 | + grade_level SMALLINT NOT NULL CONSTRAINT class_blueprint_grade_key UNIQUE | |
| 171 | + CHECK (grade_level BETWEEN 0 AND 12) | |
| 173 | 172 | ); |
| 174 | 173 | |
| 175 | 174 | -- A template's course list. The old `courses uuid[]` on the blueprint row is |
migrations/school/20260916000001_k12_remodel.sqlmodified+137 -15
| @@ -76,6 +76,11 @@ ALTER TABLE class_group ADD COLUMN year uuid REFERENCES academic_year(id) ON DEL | ||
| 76 | 76 | UPDATE class_group c SET year = t.year FROM term t WHERE t.id = c.term; |
| 77 | 77 | ALTER TABLE class_group DROP COLUMN term; |
| 78 | 78 | |
| 79 | +-- The class's place on the grade ladder: 0 = anaokulu, 1..12 = 1. sinif..12. sinif. | |
| 80 | +ALTER TABLE class_group ADD COLUMN grade_level SMALLINT NOT NULL | |
| 81 | + CHECK (grade_level BETWEEN 0 AND 12); | |
| 82 | +CREATE INDEX class_group_grade_level ON class_group (grade_level); | |
| 83 | + | |
| 79 | 84 | UPDATE academic_year SET class_count = (SELECT count(*) FROM class_group WHERE year = academic_year.id); |
| 80 | 85 | |
| 81 | 86 | -- --------------------------------------------------------------------------- |
| @@ -134,25 +139,66 @@ UPDATE class_group SET class_member_count = 0, class_course_count = 0; | ||
| 134 | 139 | UPDATE subject SET exam_question_count = 0, homework_count = 0; |
| 135 | 140 | UPDATE kind_ref SET count = 0; |
| 136 | 141 | |
| 137 | --- The instance. It now carries the teachers (a junction), the weekly hours | |
| 138 | --- and whether it counts toward the karne; `source` still marks blueprint vs | |
| 139 | --- hand-placed. `enrollment_count` is the instance roster size — a counter, | |
| 140 | --- not a capacity gate (the seat-claim 409 is deleted with D4/D5). | |
| 142 | +-- The grade-level course template. One `course_offering` row per | |
| 143 | +-- (course × grade_level) holds the content every section at that grade | |
| 144 | +-- teaches from; every `class_course` instance points at exactly one | |
| 145 | +-- offering and may override any content field. Unset = inherit from the | |
| 146 | +-- offering; the offering's unset = inherit from the catalog row / the | |
| 147 | +-- constants (1 weekly hour, counted toward the karne). Override-or-inherit, | |
| 148 | +-- never merge. Timestamps follow the repo idiom (epoch millis, app-stamped | |
| 149 | +-- by `Timestamp::now`). | |
| 150 | +CREATE TABLE course_offering ( | |
| 151 | + id uuid PRIMARY KEY, | |
| 152 | + course uuid NOT NULL REFERENCES course(id) ON DELETE CASCADE, | |
| 153 | + grade_level SMALLINT NOT NULL CHECK (grade_level BETWEEN 0 AND 12), | |
| 154 | + title TEXT, -- NULL -> course.title | |
| 155 | + description TEXT, -- NULL -> course.description | |
| 156 | + default_ders_saati SMALLINT CHECK (default_ders_saati BETWEEN 1 AND 40), -- NULL -> 1 | |
| 157 | + default_counts_toward_karne BOOLEAN, -- NULL -> TRUE | |
| 158 | + created_by uuid NOT NULL REFERENCES app_user(id), | |
| 159 | + created_at BIGINT NOT NULL, | |
| 160 | + updated_at BIGINT NOT NULL, | |
| 161 | + CONSTRAINT course_offering_course_grade_level UNIQUE (course, grade_level) | |
| 162 | +); | |
| 163 | + | |
| 164 | +CREATE INDEX course_offering_course ON course_offering (course); | |
| 165 | + | |
| 166 | +-- The instance. It now carries the teachers (a junction), the offering it | |
| 167 | +-- teaches from, and the per-section overrides of the template fields | |
| 168 | +-- (NULL = inherit from the offering chain; a set value is the section's | |
| 169 | +-- own). The set-valued policies (subjects, exam weights, weekly plan) | |
| 170 | +-- cannot be NULL, so their override is a flag: TRUE = inherit the | |
| 171 | +-- offering's set, FALSE = this section's own table is authoritative, | |
| 172 | +-- including when empty. `source` still marks blueprint vs hand-placed. | |
| 173 | +-- `enrollment_count` is the instance roster size — a counter, not a | |
| 174 | +-- capacity gate (the seat-claim 409 is deleted with D4/D5). | |
| 141 | 175 | CREATE TABLE class_course ( |
| 142 | - id uuid PRIMARY KEY, | |
| 143 | - class uuid NOT NULL REFERENCES class_group(id) ON DELETE NO ACTION, | |
| 144 | - course uuid NOT NULL REFERENCES course(id) ON DELETE NO ACTION, | |
| 145 | - attached_by uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, | |
| 146 | - source uuid NULL REFERENCES class_blueprint(id) ON DELETE NO ACTION, | |
| 147 | - ders_saati SMALLINT NOT NULL DEFAULT 1, | |
| 148 | - counts_toward_karne BOOLEAN NOT NULL DEFAULT TRUE, | |
| 149 | - enrollment_count BIGINT NOT NULL DEFAULT 0, | |
| 150 | - attached_at BIGINT NOT NULL, | |
| 176 | + id uuid PRIMARY KEY, | |
| 177 | + class uuid NOT NULL REFERENCES class_group(id) ON DELETE NO ACTION, | |
| 178 | + course uuid NOT NULL REFERENCES course(id) ON DELETE NO ACTION, | |
| 179 | + attached_by uuid NOT NULL REFERENCES app_user(id) ON DELETE NO ACTION, | |
| 180 | + source uuid NULL REFERENCES class_blueprint(id) ON DELETE NO ACTION, | |
| 181 | + ders_saati SMALLINT, | |
| 182 | + counts_toward_karne BOOLEAN, | |
| 183 | + enrollment_count BIGINT NOT NULL DEFAULT 0, | |
| 184 | + attached_at BIGINT NOT NULL, | |
| 185 | + -- Every instance teaches FROM an offering — the one the (course, | |
| 186 | + -- grade_level) pair of the attaching class resolves to, auto-created | |
| 187 | + -- empty by the attach pump when missing. `RESTRICT` (not CASCADE): an | |
| 188 | + -- offering a section still teaches cannot go — the delete route answers | |
| 189 | + -- 409 `offering_in_use` first. | |
| 190 | + offering uuid NOT NULL REFERENCES course_offering(id) ON DELETE RESTRICT, | |
| 191 | + title TEXT, | |
| 192 | + description TEXT, | |
| 193 | + subjects_inherited BOOLEAN NOT NULL DEFAULT TRUE, | |
| 194 | + exam_weights_inherited BOOLEAN NOT NULL DEFAULT TRUE, | |
| 195 | + weekly_plan_inherited BOOLEAN NOT NULL DEFAULT TRUE, | |
| 151 | 196 | CONSTRAINT class_course_class_course UNIQUE (class, course) |
| 152 | 197 | ); |
| 153 | 198 | |
| 154 | -CREATE INDEX class_course_course ON class_course (course); | |
| 155 | -CREATE INDEX class_course_class ON class_course (class); | |
| 199 | +CREATE INDEX class_course_course ON class_course (course); | |
| 200 | +CREATE INDEX class_course_class ON class_course (class); | |
| 201 | +CREATE INDEX class_course_offering ON class_course (offering); | |
| 156 | 202 | |
| 157 | 203 | -- D6: teacher assignment moves off the catalog course onto the instance. |
| 158 | 204 | CREATE TABLE class_course_teacher ( |
| @@ -163,6 +209,82 @@ CREATE TABLE class_course_teacher ( | ||
| 163 | 209 | |
| 164 | 210 | CREATE INDEX class_course_teacher_teacher ON class_course_teacher (teacher); |
| 165 | 211 | |
| 212 | +-- --------------------------------------------------------------------------- | |
| 213 | +-- 5c. The template's set-valued policies. | |
| 214 | +-- | |
| 215 | +-- Which of a course's subjects an offering teaches, and which one a | |
| 216 | +-- class section teaches: pure membership junctions over the subjects | |
| 217 | +-- the catalog course already owns, so a selection can never name a | |
| 218 | +-- topic the course does not have (the service checks the same | |
| 219 | +-- precondition up front to answer 400 instead of letting the foreign | |
| 220 | +-- key answer 500). Every class-side write flips the section's | |
| 221 | +-- `*_inherited` flag in the same statement it writes a row; the reset | |
| 222 | +-- door deletes the rows and flips it back. | |
| 223 | +-- | |
| 224 | +-- All of these are pure rows: no id, no stamps — the composite key is | |
| 225 | +-- the whole row, and the cascades mean a deleted course/offering/ | |
| 226 | +-- section takes its selections with it. | |
| 227 | +-- --------------------------------------------------------------------------- | |
| 228 | +CREATE TABLE offering_subject ( | |
| 229 | + offering uuid NOT NULL REFERENCES course_offering(id) ON DELETE CASCADE, | |
| 230 | + subject uuid NOT NULL REFERENCES subject(id) ON DELETE CASCADE, | |
| 231 | + CONSTRAINT offering_subject_offering_subject PRIMARY KEY (offering, subject) | |
| 232 | +); | |
| 233 | + | |
| 234 | +CREATE TABLE class_course_subject ( | |
| 235 | + class_course uuid NOT NULL REFERENCES class_course(id) ON DELETE CASCADE, | |
| 236 | + subject uuid NOT NULL REFERENCES subject(id) ON DELETE CASCADE, | |
| 237 | + CONSTRAINT class_course_subject_class_course_subject PRIMARY KEY (class_course, subject) | |
| 238 | +); | |
| 239 | + | |
| 240 | +-- Per-offering and per-class exam-kind weights. The school's | |
| 241 | +-- `settings.exam_kinds` stays the *default* weight of every kind; these two | |
| 242 | +-- tables override it per grade-level template and per class section. The | |
| 243 | +-- weight column mirrors the settings rule (1..=100); no timestamps — a | |
| 244 | +-- weight override is a set row keyed by its owner and kind, like the | |
| 245 | +-- subject set. | |
| 246 | +CREATE TABLE offering_exam_weight ( | |
| 247 | + offering uuid NOT NULL REFERENCES course_offering(id) ON DELETE CASCADE, | |
| 248 | + kind TEXT NOT NULL, | |
| 249 | + weight SMALLINT NOT NULL CHECK (weight BETWEEN 1 AND 100), | |
| 250 | + PRIMARY KEY (offering, kind) | |
| 251 | +); | |
| 252 | + | |
| 253 | +CREATE TABLE class_course_exam_weight ( | |
| 254 | + class_course uuid NOT NULL REFERENCES class_course(id) ON DELETE CASCADE, | |
| 255 | + kind TEXT NOT NULL, | |
| 256 | + weight SMALLINT NOT NULL CHECK (weight BETWEEN 1 AND 100), | |
| 257 | + PRIMARY KEY (class_course, kind) | |
| 258 | +); | |
| 259 | + | |
| 260 | +-- The weekly plan: an offering's (and a section's) timetable template. | |
| 261 | +-- A slot is where one of the week's lesson hours lands. `weekday` is the | |
| 262 | +-- ISO day (1 = Monday .. 7 = Sunday); `starts_at`/`ends_at` are times of | |
| 263 | +-- day (Postgres `TIME`, carried over the API as minutes past midnight). | |
| 264 | +-- Two slots of one owner may touch (one may start the minute another | |
| 265 | +-- ends) but never overlap; the exact (weekday, starts_at) duplicate is the | |
| 266 | +-- UNIQUE key. `CASCADE`: the slots are template data — a detach must never | |
| 267 | +-- be blocked by timetable rows. | |
| 268 | +CREATE TABLE offering_slot ( | |
| 269 | + id uuid PRIMARY KEY, | |
| 270 | + offering uuid NOT NULL REFERENCES course_offering(id) ON DELETE CASCADE, | |
| 271 | + weekday SMALLINT NOT NULL CHECK (weekday BETWEEN 1 AND 7), -- 1 = Monday | |
| 272 | + starts_at TIME NOT NULL, | |
| 273 | + ends_at TIME NOT NULL, | |
| 274 | + CHECK (ends_at > starts_at), | |
| 275 | + CONSTRAINT offering_slot_offering_weekday_starts_at UNIQUE (offering, weekday, starts_at) | |
| 276 | +); | |
| 277 | + | |
| 278 | +CREATE TABLE class_course_slot ( | |
| 279 | + id uuid PRIMARY KEY, | |
| 280 | + class_course uuid NOT NULL REFERENCES class_course(id) ON DELETE CASCADE, | |
| 281 | + weekday SMALLINT NOT NULL CHECK (weekday BETWEEN 1 AND 7), | |
| 282 | + starts_at TIME NOT NULL, | |
| 283 | + ends_at TIME NOT NULL, | |
| 284 | + CHECK (ends_at > starts_at), | |
| 285 | + CONSTRAINT class_course_slot_class_course_weekday_starts_at UNIQUE (class_course, weekday, starts_at) | |
| 286 | +); | |
| 287 | + | |
| 166 | 288 | -- D11: a history table with a surrogate id. `left_at` NULL is the live stint; |
| 167 | 289 | -- the partial unique index enforces one live stint per (class, user) pair |
| 168 | 290 | -- while preserving rejoin history. A soft-left row holds no seat, so the |