diff --git a/src/database.rs b/src/database.rs new file mode 100644 index 0000000..fe15716 --- /dev/null +++ b/src/database.rs @@ -0,0 +1,91 @@ +//! Embedded SurrealDB (surrealkv, file-backed) connection + schema. + +use surrealdb::Surreal; +use surrealdb::engine::local::{Db, Mem, SurrealKv}; + +use crate::config::Config; +use crate::error::AppError; + +pub type Database = Surreal; + +pub const USER_TABLE: &str = "user"; +pub const SESSION_TABLE: &str = "session"; +pub const NOTE_TABLE: &str = "note"; +pub const EVENT_TABLE: &str = "event"; +pub const ATTENDANCE_TABLE: &str = "attendance"; +pub const EXAM_TABLE: &str = "exam"; +pub const EXAM_RESULT_TABLE: &str = "exam_result"; + +/// SCHEMAFULL schema: every column is typed, references use `record<..>`. +/// Idempotent — safe to run on every boot. +const MIGRATION: &str = " + DEFINE TABLE IF NOT EXISTS user SCHEMAFULL; + DEFINE FIELD IF NOT EXISTS username ON user TYPE string; + DEFINE FIELD IF NOT EXISTS password_hash ON user TYPE string; + DEFINE FIELD IF NOT EXISTS role ON user TYPE string DEFAULT 'student'; + DEFINE INDEX IF NOT EXISTS user_username ON user FIELDS username UNIQUE; + UPDATE user SET role = 'student' WHERE role = NONE; + + DEFINE TABLE IF NOT EXISTS session SCHEMAFULL; + DEFINE FIELD IF NOT EXISTS user ON session TYPE record; + DEFINE FIELD IF NOT EXISTS token ON session TYPE string; + DEFINE FIELD IF NOT EXISTS expires_at ON session TYPE int; + DEFINE INDEX IF NOT EXISTS session_token ON session FIELDS token UNIQUE; + DEFINE INDEX IF NOT EXISTS session_expires ON session FIELDS expires_at; + + DEFINE TABLE IF NOT EXISTS note SCHEMAFULL; + DEFINE FIELD IF NOT EXISTS user ON note TYPE record; + DEFINE FIELD IF NOT EXISTS title ON note TYPE string; + DEFINE FIELD IF NOT EXISTS content ON note TYPE string; + DEFINE INDEX IF NOT EXISTS note_user ON note FIELDS user; + + DEFINE TABLE IF NOT EXISTS event SCHEMAFULL; + DEFINE FIELD IF NOT EXISTS creator ON event TYPE record; + DEFINE FIELD IF NOT EXISTS title ON event TYPE string; + DEFINE FIELD IF NOT EXISTS description ON event TYPE string; + DEFINE FIELD IF NOT EXISTS starts_at ON event TYPE option; + DEFINE FIELD IF NOT EXISTS ends_at ON event TYPE option; + + DEFINE TABLE IF NOT EXISTS attendance SCHEMAFULL; + DEFINE FIELD IF NOT EXISTS event ON attendance TYPE record; + DEFINE FIELD IF NOT EXISTS user ON attendance TYPE record; + DEFINE FIELD IF NOT EXISTS status ON attendance TYPE string; + DEFINE FIELD IF NOT EXISTS marked_by ON attendance TYPE record; + DEFINE INDEX IF NOT EXISTS attendance_event_user ON attendance FIELDS event, user UNIQUE; + + DEFINE TABLE IF NOT EXISTS exam SCHEMAFULL; + DEFINE FIELD IF NOT EXISTS creator ON exam TYPE record; + DEFINE FIELD IF NOT EXISTS title ON exam TYPE string; + DEFINE FIELD IF NOT EXISTS description ON exam TYPE string; + DEFINE FIELD IF NOT EXISTS kind ON exam TYPE string; + + DEFINE TABLE IF NOT EXISTS exam_result SCHEMAFULL; + DEFINE FIELD IF NOT EXISTS exam ON exam_result TYPE record; + DEFINE FIELD IF NOT EXISTS user ON exam_result TYPE record; + DEFINE FIELD IF NOT EXISTS mark ON exam_result TYPE int; + DEFINE FIELD IF NOT EXISTS graded_by ON exam_result TYPE record; + DEFINE INDEX IF NOT EXISTS exam_result_exam_user ON exam_result FIELDS exam, user UNIQUE; +"; + +/// Open the file-backed database and apply the schema. +pub async fn init(cfg: &Config) -> Result { + let db = Surreal::new::(cfg.db_path.clone()).await?; + db.use_ns(cfg.db_ns.clone()) + .use_db(cfg.db_name.clone()) + .await?; + migrate(&db).await?; + Ok(db) +} + +/// A fresh in-memory database with the schema applied. For tests. +pub async fn init_mem() -> Result { + let db = Surreal::new::(()).await?; + db.use_ns("hezarfen").use_db("hezarfen").await?; + migrate(&db).await?; + Ok(db) +} + +async fn migrate(db: &Database) -> Result<(), AppError> { + db.query(MIGRATION).await?.check()?; + Ok(()) +}