CREATE TABLE IF NOT EXISTS courses ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, semester TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS tutors ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT UNIQUE NOT NULL, password_hash TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS tutor_courses ( tutor_id INTEGER NOT NULL REFERENCES tutors(id), course_id INTEGER NOT NULL REFERENCES courses(id), PRIMARY KEY (tutor_id, course_id) ); CREATE TABLE IF NOT EXISTS students ( id INTEGER PRIMARY KEY, course_id INTEGER NOT NULL REFERENCES courses(id), name TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS rooms ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, layout_json TEXT NOT NULL ); CREATE TABLE IF NOT EXISTS sessions ( id INTEGER PRIMARY KEY, course_id INTEGER NOT NULL REFERENCES courses(id), week_nr INTEGER NOT NULL, date TEXT NOT NULL CHECK (date GLOB '????-??-??'), UNIQUE(course_id, week_nr) ); CREATE TABLE IF NOT EXISTS slots ( id INTEGER PRIMARY KEY, session_id INTEGER NOT NULL REFERENCES sessions(id), room_id INTEGER REFERENCES rooms(id), tutor_id INTEGER NOT NULL REFERENCES tutors(id), start_time TEXT NOT NULL CHECK (start_time GLOB '??:??'), end_time TEXT NOT NULL CHECK (end_time GLOB '??:??'), status TEXT NOT NULL DEFAULT 'closed' CHECK (status IN ('closed','open','locked')), code TEXT UNIQUE ); CREATE TABLE IF NOT EXISTS attendances ( id INTEGER PRIMARY KEY, slot_id INTEGER NOT NULL REFERENCES slots(id), student_id INTEGER NOT NULL REFERENCES students(id), seat_id TEXT, checked_in_at TEXT NOT NULL CHECK (checked_in_at GLOB '????-??-??T??:??:??*'), UNIQUE(slot_id, student_id), UNIQUE(slot_id, seat_id) ); CREATE TABLE IF NOT EXISTS notes ( id INTEGER PRIMARY KEY, slot_id INTEGER NOT NULL REFERENCES slots(id), student_id INTEGER NOT NULL REFERENCES students(id), tutor_id INTEGER NOT NULL REFERENCES tutors(id), content TEXT NOT NULL DEFAULT '', updated_at TEXT NOT NULL CHECK (updated_at GLOB '????-??-??T??:??:??*'), UNIQUE(slot_id, student_id, tutor_id) ); -- Indexes on high-frequency FK columns (SQLite does not auto-index FKs) CREATE INDEX IF NOT EXISTS idx_students_course ON students(course_id); CREATE INDEX IF NOT EXISTS idx_sessions_course ON sessions(course_id); CREATE INDEX IF NOT EXISTS idx_slots_session ON slots(session_id); CREATE INDEX IF NOT EXISTS idx_slots_tutor ON slots(tutor_id); CREATE INDEX IF NOT EXISTS idx_attendances_slot ON attendances(slot_id); CREATE INDEX IF NOT EXISTS idx_attendances_student ON attendances(student_id); CREATE INDEX IF NOT EXISTS idx_notes_slot ON notes(slot_id); CREATE INDEX IF NOT EXISTS idx_notes_student ON notes(student_id);