diff --git a/Makefile b/Makefile index 43403f4..96a6164 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ seed-demo: @echo "Applying migrations and seeding demo data..." @DB_PATH=$${DATABASE_URL:-sqlite:data/attendance.db}; \ DB_FILE=$${DB_PATH#sqlite:}; \ + rm -f $$DB_FILE; \ for f in backend/migrations/*.sql; do \ echo "Applying $$f..."; \ sqlite3 $$DB_FILE < $$f; \ diff --git a/backend/migrations/001_initial.sql b/backend/migrations/001_initial.sql index 7d9ecfd..d099b28 100644 --- a/backend/migrations/001_initial.sql +++ b/backend/migrations/001_initial.sql @@ -1,35 +1,35 @@ -CREATE TABLE IF NOT EXISTS courses ( +CREATE TABLE courses ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, semester TEXT NOT NULL ); -CREATE TABLE IF NOT EXISTS tutors ( +CREATE TABLE 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 ( +CREATE TABLE 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 ( +CREATE TABLE students ( id INTEGER PRIMARY KEY, course_id INTEGER NOT NULL REFERENCES courses(id), name TEXT NOT NULL ); -CREATE TABLE IF NOT EXISTS rooms ( +CREATE TABLE rooms ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, layout_json TEXT NOT NULL ); -CREATE TABLE IF NOT EXISTS sessions ( +CREATE TABLE sessions ( id INTEGER PRIMARY KEY, course_id INTEGER NOT NULL REFERENCES courses(id), week_nr INTEGER NOT NULL, @@ -37,7 +37,7 @@ CREATE TABLE IF NOT EXISTS sessions ( UNIQUE(course_id, week_nr) ); -CREATE TABLE IF NOT EXISTS slots ( +CREATE TABLE slots ( id INTEGER PRIMARY KEY, session_id INTEGER NOT NULL REFERENCES sessions(id), room_id INTEGER REFERENCES rooms(id), @@ -48,7 +48,7 @@ CREATE TABLE IF NOT EXISTS slots ( code TEXT UNIQUE ); -CREATE TABLE IF NOT EXISTS attendances ( +CREATE TABLE attendances ( id INTEGER PRIMARY KEY, slot_id INTEGER NOT NULL REFERENCES slots(id), student_id INTEGER NOT NULL REFERENCES students(id), @@ -58,7 +58,7 @@ CREATE TABLE IF NOT EXISTS attendances ( UNIQUE(slot_id, seat_id) ); -CREATE TABLE IF NOT EXISTS notes ( +CREATE TABLE notes ( id INTEGER PRIMARY KEY, slot_id INTEGER NOT NULL REFERENCES slots(id), student_id INTEGER NOT NULL REFERENCES students(id), @@ -69,11 +69,11 @@ CREATE TABLE IF NOT EXISTS notes ( ); -- 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); +CREATE INDEX idx_students_course ON students(course_id); +CREATE INDEX idx_sessions_course ON sessions(course_id); +CREATE INDEX idx_slots_session ON slots(session_id); +CREATE INDEX idx_slots_tutor ON slots(tutor_id); +CREATE INDEX idx_attendances_slot ON attendances(slot_id); +CREATE INDEX idx_attendances_student ON attendances(student_id); +CREATE INDEX idx_notes_slot ON notes(slot_id); +CREATE INDEX idx_notes_student ON notes(student_id);