From 0b4bc103c6ed6b1db5ae6434f2785ee0504553c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 11 Jul 2026 21:39:14 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(db):=20embedded=20SurrealDB=20?= =?UTF-8?q?connection=20and=20schema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit init opens the file-backed surrealkv store and applies the schema; init_mem gives tests a fresh in-memory database with the same schema. The migration is one idempotent SCHEMAFULL block (DEFINE ... IF NOT EXISTS), safe to run on every boot. Every column is typed, foreign keys are record<..> links, and unique indexes enforce the invariants that matter: one username, one session token, one attendance row per event+user, and one result per exam+user. Table-name constants are exported so the domain layer never spells a table as a bare string. --- src/database.rs | 91 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/database.rs 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(()) +}