diff --git a/src/constant.rs b/src/constant.rs index 8f9cb7c..dc1eb82 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -86,6 +86,12 @@ pub const MAX_EXAM_DESCRIPTION_LEN: usize = 2_000; pub const MAX_COURSE_TITLE_LEN: usize = 200; pub const MAX_COURSE_DESCRIPTION_LEN: usize = 2_000; +/// A class (şube) is named like a course, but its optional `grade` is a free-text +/// label the school picks ("9", "10-A", "anaokulu") — a line of display text, so +/// it is bounded like the other short labels, not like a description. +pub const MAX_CLASS_NAME_LEN: usize = 200; +pub const MAX_CLASS_GRADE_LEN: usize = 20; + pub const MAX_SUBJECT_NAME_LEN: usize = 200; pub const MAX_SUBJECT_DESCRIPTION_LEN: usize = 2_000; @@ -698,6 +704,9 @@ pub const BANK_QUESTION_TABLE: &str = "bank_question"; pub const BANK_QUESTION_IMAGE_TABLE: &str = "bank_question_image"; pub const COURSE_TABLE: &str = "course"; pub const ENROLLMENT_TABLE: &str = "enrollment"; +pub const CLASS_GROUP_TABLE: &str = "class_group"; +pub const CLASS_MEMBER_TABLE: &str = "class_member"; +pub const CLASS_COURSE_TABLE: &str = "class_course"; pub const PARENT_LINK_TABLE: &str = "parent_link"; pub const COURSE_SESSION_TABLE: &str = "course_session"; pub const SESSION_ATTENDANCE_TABLE: &str = "session_attendance"; @@ -750,6 +759,14 @@ pub const REGISTRATION_COUNT_FIELD: &str = "registration_count"; /// is claimed before a course's link is written, so the two decisions contend /// on the term row instead of on a cross-table `SELECT` no transaction orders. pub const COURSE_COUNT_FIELD: &str = "course_count"; +/// The two refcounts on a class row: how many students it holds and how many +/// courses it is attached to. A class may only be dropped at zero on both. +pub const CLASS_MEMBER_COUNT_FIELD: &str = "class_member_count"; +pub const CLASS_COURSE_COUNT_FIELD: &str = "class_course_count"; +/// Classes linking a term, the second half of the term delete guard. Deliberately +/// *not* `COURSE_COUNT_FIELD`: boot recounts that one from the course rows alone, +/// so a class claiming into it would be wiped on the next migration. +pub const TERM_CLASS_COUNT_FIELD: &str = "class_count"; /// A refcount too, and the whole of the fee-plan edit *and* delete guard: how /// many students are on this plan. A plan may only be edited or deleted at /// zero, and the count is claimed in the same transaction as the assignment diff --git a/src/migration_sql.rs b/src/migration_sql.rs index 0ce2f3b..f1f6b1d 100644 --- a/src/migration_sql.rs +++ b/src/migration_sql.rs @@ -239,6 +239,9 @@ pub const MIGRATION: &str = " -- write-skew between concurrent request tasks (see `crate::domain::cap`). -- Absent reads as zero, so no Rust struct needs it. DEFINE FIELD IF NOT EXISTS course_count ON term TYPE option; + -- Classes linking this term, the other half of the same guard. A column of + -- its own because `course_count` is seeded from the course rows alone. + DEFINE FIELD IF NOT EXISTS class_count ON term TYPE option; DEFINE TABLE IF NOT EXISTS course SCHEMAFULL; DEFINE FIELD IF NOT EXISTS creator ON course TYPE record; @@ -262,10 +265,41 @@ pub const MIGRATION: &str = " DEFINE FIELD IF NOT EXISTS homework_count ON subject TYPE option; DEFINE INDEX IF NOT EXISTS subject_course ON subject FIELDS course; + -- A class (şube) is a named set of students that bulk-manages real + -- `enrollment` rows: `class_member` is one student in it, `class_course` one + -- course it is attached to. Both counters gate the class delete. + DEFINE TABLE IF NOT EXISTS class_group SCHEMAFULL; + DEFINE FIELD IF NOT EXISTS name ON class_group TYPE string; + DEFINE FIELD IF NOT EXISTS grade ON class_group TYPE option; + DEFINE FIELD IF NOT EXISTS term ON class_group TYPE option>; + DEFINE FIELD IF NOT EXISTS creator ON class_group TYPE record; + DEFINE FIELD IF NOT EXISTS class_member_count ON class_group TYPE option; + DEFINE FIELD IF NOT EXISTS class_course_count ON class_group TYPE option; + + DEFINE TABLE IF NOT EXISTS class_member SCHEMAFULL; + DEFINE FIELD IF NOT EXISTS class ON class_member TYPE record; + DEFINE FIELD IF NOT EXISTS user ON class_member TYPE record; + DEFINE FIELD IF NOT EXISTS added_by ON class_member TYPE record; + DEFINE INDEX IF NOT EXISTS class_member_class_user ON class_member FIELDS class, user UNIQUE; + DEFINE INDEX IF NOT EXISTS class_member_class ON class_member FIELDS class; + DEFINE INDEX IF NOT EXISTS class_member_user ON class_member FIELDS user; + + DEFINE TABLE IF NOT EXISTS class_course SCHEMAFULL; + DEFINE FIELD IF NOT EXISTS class ON class_course TYPE record; + DEFINE FIELD IF NOT EXISTS course ON class_course TYPE record; + DEFINE FIELD IF NOT EXISTS attached_by ON class_course TYPE record; + DEFINE INDEX IF NOT EXISTS class_course_class_course ON class_course FIELDS class, course UNIQUE; + DEFINE INDEX IF NOT EXISTS class_course_class ON class_course FIELDS class; + DEFINE INDEX IF NOT EXISTS class_course_course ON class_course FIELDS course; + DEFINE TABLE IF NOT EXISTS enrollment SCHEMAFULL; DEFINE FIELD IF NOT EXISTS course ON enrollment TYPE record; DEFINE FIELD IF NOT EXISTS user ON enrollment TYPE record; DEFINE FIELD IF NOT EXISTS enrolled_by ON enrollment TYPE record; + -- The class that pumped this row, absent when a human placed the student + -- directly. Absence is the meaning, so there is nothing to backfill: a class + -- sweep may only take back the rows it wrote. + DEFINE FIELD IF NOT EXISTS source ON enrollment TYPE option>; DEFINE INDEX IF NOT EXISTS enrollment_course_user ON enrollment FIELDS course, user UNIQUE; DEFINE INDEX IF NOT EXISTS enrollment_user ON enrollment FIELDS user; diff --git a/src/web/limits.rs b/src/web/limits.rs index 80ee3dd..7f4abd4 100644 --- a/src/web/limits.rs +++ b/src/web/limits.rs @@ -106,7 +106,8 @@ struct EventLimits { max_description_len: usize, } -/// Courses, their curriculum subjects, and lesson sessions. +/// Courses, their curriculum subjects, lesson sessions, and the classes that +/// bulk-enroll into them. #[derive(Serialize, ToSchema)] struct CourseLimits { max_title_len: usize, @@ -118,6 +119,9 @@ struct CourseLimits { max_subject_description_len: usize, max_session_topic_len: usize, max_term_name_len: usize, + max_class_name_len: usize, + /// A class's optional free-text grade label ("9", "10-A"). + max_class_grade_len: usize, } /// Exams, their questions, answers, and marks. @@ -420,6 +424,8 @@ impl LimitsResponse { max_subject_description_len: MAX_SUBJECT_DESCRIPTION_LEN, max_session_topic_len: MAX_SESSION_TOPIC_LEN, max_term_name_len: MAX_TERM_NAME_LEN, + max_class_name_len: MAX_CLASS_NAME_LEN, + max_class_grade_len: MAX_CLASS_GRADE_LEN, }, exam: ExamLimits { max_title_len: MAX_EXAM_TITLE_LEN,