forge
Explore
Log in

Don't you dare go Hollow

Move cap/field_update/page toolkits from domain to new db layer

Browse files at this commit

3763468e17cdf06e401e93e53ecabb6c5d623ceaAhmet Kaan GümüşParent: 8b8a45d
  • chore
    • move cap/field_update/page toolkits from domain to new db layer
Show full message
🧹 chore: move cap/field_update/page toolkits from domain to new db layer
README.mdmodified+1 -1
@@ -4565,7 +4565,7 @@ losing races at one process. Every invariant is therefore guarded where the
45654565 database itself decides the winner. Three tiers:
45664566
45674567 1. **Single-row conditional writes** — compare-and-set (`save_if_unchanged`),
4568- `UPDATE … WHERE`, stored counters (`domain::cap`). A single-record write is
4568+ `UPDATE … WHERE`, stored counters (`db::cap`). A single-record write is
45694569 atomic, so of N concurrent tasks exactly the allowed number get a non-empty
45704570 result: the database decides the winner, the loser retries
45714571 (`CAS_UPDATE_RETRIES`) or gets a 409.
src/constant.rsmodified+3 -3
@@ -921,13 +921,13 @@ pub const BOARD_STROKE_TABLE: &str = "board_stroke";
921921 pub const BADGE_AWARD_TABLE: &str = "badge_award";
922922 /// One row per *name* the school's settings offer, keyed by the name itself:
923923 /// how many rows still reference it, and whether it has been retired out of the
924-/// list (see the reference counters in [`crate::domain::cap`]).
924+/// list (see the reference counters in [`crate::db::cap`]).
925925 pub const KIND_REF_TABLE: &str = "kind_ref";
926926 pub const SLOT_REF_TABLE: &str = "slot_ref";
927927
928928 // --- stored cap counters -------------------------------------------------
929929
930-/// The counter columns behind the count caps (see [`crate::domain::cap`]).
930+/// The counter columns behind the count caps (see [`crate::db::cap`]).
931931 /// Each one lives on the *parent* row, because a single-record conditional
932932 /// `UPDATE` is the only guard a concurrent writer cannot outrun (a lock is
933933 /// released around the round trip). Spelled here rather than at the call site
@@ -970,7 +970,7 @@ pub const CHATBOT_THREAD_COUNT_FIELD: &str = "chatbot_thread_count";
970970 /// The column a *grant* claim moves and puts back, so its transaction writes
971971 /// the holder's own `user` record — the one key
972972 /// [`crate::domain::user::User::set_role`] writes (see
973-/// [`crate::domain::cap::role_claim`]). Any `option<int>` on the row would do:
973+/// [`crate::db::cap::role_claim`]). Any `option<int>` on the row would do:
974974 /// the claim nets zero and never reads it, so this is an alias rather than a
975975 /// column of its own — a new one would mean a migration on a SCHEMAFULL table
976976 /// for a value nothing ever observes.
src/database.rsmodified+1 -1
@@ -283,7 +283,7 @@ pub async fn init_mem_tenants() -> Result<Tenants, AppError> {
283283 /// that is dropped and re-made on every call. For the tests whose subject is
284284 /// the store's own conflict detection, which [`init_mem`]'s embedded engine
285285 /// does not have: it drops one of two concurrent writes to a record and answers
286-/// `Ok` to both (see [`crate::domain::cap`]), which *forges* the very
286+/// `Ok` to both (see [`crate::db::cap`]), which *forges* the very
287287 /// integrity failure those tests exist to catch — measured 2026-07-30 at 2
288288 /// failed runs in 36 on `memory`, against 0 in 10 000 rounds here.
289289 ///
src/domain/cap.rs → src/db/cap.rsrenamed+1 -1
@@ -58,7 +58,7 @@ use crate::error::AppError;
5858 static CLAIM_LOCK: Mutex<()> = Mutex::const_new(());
5959
6060 /// [`CLAIM_LOCK`], for a counter write that rides someone else's transaction
61-/// instead of [`write`]'s — [`crate::domain::field_update::FieldUpdate`]'s term
61+/// instead of [`write`]'s — [`crate::db::field_update::FieldUpdate`]'s term
6262 /// move claims and releases inside the very `UPDATE`'s transaction, and still
6363 /// owes the process one counter write at a time.
6464 ///
src/domain/field_update.rs → src/db/field_update.rsrenamed+2 -2
@@ -25,7 +25,7 @@
2525 use surrealdb::types::{RecordId, SurrealValue, Value};
2626
2727 use crate::database::{Database, transaction_with_retry, write_with_retry};
28-use crate::domain::cap;
28+use crate::db::cap;
2929 use crate::error::AppError;
3030
3131 /// The `THROW` markers [`FieldUpdate::refcount`]'s transaction aborts with: the
@@ -131,7 +131,7 @@ impl FieldUpdate {
131131 /// count is what a delete guard reads, so the stranded one makes its parent
132132 /// undeletable forever. `refused` is the answer when the claimed row is gone
133133 /// (the conditional write doubles as the existence check, as in
134- /// [`crate::domain::cap`]); the transaction then aborts, so the release and
134+ /// [`crate::db::cap`]); the transaction then aborts, so the release and
135135 /// the row write never happened either.
136136 ///
137137 /// Only ever called alongside a `.set()` of the very column that carries the
src/db/mod.rsadded+5 -0
@@ -0,0 +1,5 @@
1+//! The only layer that executes queries.
2+
3+pub mod cap;
4+pub mod field_update;
5+pub mod page;
src/domain/page.rs → src/db/page.rsrenamed+0 -0
src/domain/appointment.rsmodified+3 -3
@@ -5,7 +5,7 @@
55 //! Two *cross-record* invariants live here, guarded very differently:
66 //!
77 //! - **One live booking per slot** — the `occupied` counter on the slot row, a
8-//! [`cap`](crate::domain::cap) of one. Booking claims it *with* the booking
8+//! [`cap`](crate::db::cap) of one. Booking claims it *with* the booking
99 //! row ([`claim_and_create`](cap::claim_and_create));
1010 //! rejecting or cancelling gives it back in the *same transaction* as the
1111 //! status flip, so the slot frees itself with nothing to sweep. Both are
@@ -41,8 +41,8 @@ use crate::constant::{
4141 };
4242 use crate::database::{Database, lost_the_race};
4343 use crate::domain::appointment_slot::{AppointmentSlot, AppointmentSlotId};
44-use crate::domain::cap;
45-use crate::domain::page::PagedList;
44+use crate::db::cap;
45+use crate::db::page::PagedList;
4646 use crate::domain::timestamp::Timestamp;
4747 use crate::domain::user::UserId;
4848 use crate::error::{AppError, ValidationError};
src/domain/appointment_slot.rsmodified+3 -3
@@ -1,6 +1,6 @@
11 //! A teacher's published availability: "I am free here, book me". A slot is
22 //! nearly pure calendar: the one piece of booking state it carries is the
3-//! `occupied` counter (a [`cap`](crate::domain::cap) of one), taken when a
3+//! `occupied` counter (a [`cap`](crate::db::cap) of one), taken when a
44 //! booking is made and given back in the same transaction as the reject or
55 //! cancel that settles it — so the slot frees itself again, and unlike a UNIQUE
66 //! index it does not keep a dead booking's seat. Stored rather than counted
@@ -19,7 +19,7 @@ use crate::constant::{
1919 };
2020 use crate::database::{Database, transaction_with_retry};
2121 use crate::domain::appointment::{APPOINTMENT_LOCK, Appointment};
22-use crate::domain::cap;
22+use crate::db::cap;
2323 use crate::domain::monotonic_id::next_ulid;
2424 use crate::domain::role::Role;
2525 use crate::domain::timestamp::Timestamp;
@@ -579,7 +579,7 @@ impl AppointmentSlot {
579579 mod tests {
580580 use super::*;
581581 use crate::constant::{MILLIS_PER_DAY, SLOT_OCCUPIED_FIELD};
582- use crate::domain::cap;
582+ use crate::db::cap;
583583
584584 fn at(millis: i64) -> Timestamp {
585585 Timestamp::from_millis(millis)
src/domain/attendance.rsmodified+1 -1
@@ -3,7 +3,7 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue};
33 use crate::constant::{ATTENDANCE_TABLE, REGISTRATION_COUNT_FIELD};
44 use crate::database::{Database, transaction_with_retry};
55 use crate::domain::event::EventId;
6-use crate::domain::page::PagedList;
6+use crate::db::page::PagedList;
77 use crate::domain::user::UserId;
88 use crate::error::{AppError, ValidationError};
99
src/domain/board.rsmodified+2 -2
@@ -20,9 +20,9 @@ use crate::constant::{
2020 USER_BOARD_COUNT_FIELD,
2121 };
2222 use crate::database::{Database, transaction_with_retry};
23-use crate::domain::cap;
23+use crate::db::cap;
2424 use crate::domain::monotonic_id::next_ulid;
25-use crate::domain::page::PagedList;
25+use crate::db::page::PagedList;
2626 use crate::domain::timestamp::Timestamp;
2727 use crate::domain::user::UserId;
2828 use crate::error::{AppError, ValidationError};
src/domain/board_stroke.rsmodified+2 -2
@@ -26,9 +26,9 @@ use crate::constant::{
2626 };
2727 use crate::database::{Database, transaction_with_retry};
2828 use crate::domain::board::{Board, BoardId};
29-use crate::domain::cap;
29+use crate::db::cap;
3030 use crate::domain::monotonic_id::next_ulid;
31-use crate::domain::page::PagedList;
31+use crate::db::page::PagedList;
3232 use crate::domain::timestamp::Timestamp;
3333 use crate::domain::user::UserId;
3434 use crate::error::AppError;
src/domain/chatbot_message.rsmodified+1 -1
@@ -19,7 +19,7 @@ use crate::constant::{
1919 };
2020 use crate::database::Database;
2121 use crate::domain::chatbot_thread::{ChatbotThreadId, touch_and_write};
22-use crate::domain::page::PagedList;
22+use crate::db::page::PagedList;
2323 use crate::domain::timestamp::Timestamp;
2424 use crate::domain::user::UserId;
2525 use crate::error::{AppError, ValidationError};
src/domain/chatbot_thread.rsmodified+2 -2
@@ -10,9 +10,9 @@ use crate::constant::{
1010 MAX_CHATBOT_THREAD_TITLE_LEN, SETTINGS_KEY, SETTINGS_TABLE,
1111 };
1212 use crate::database::{Database, transaction_with_retry};
13-use crate::domain::cap;
13+use crate::db::cap;
1414 use crate::domain::monotonic_id::next_ulid;
15-use crate::domain::page::PagedList;
15+use crate::db::page::PagedList;
1616 use crate::domain::timestamp::Timestamp;
1717 use crate::domain::user::UserId;
1818 use crate::error::{AppError, ValidationError};
src/domain/class_blueprint.rsmodified+1 -1
@@ -42,7 +42,7 @@ use crate::domain::class_course::ClassCourse;
4242 use crate::domain::class_group::{ClassGrade, ClassGroup, ClassGroupId};
4343 use crate::domain::class_pump::{Attached, Axis, detach};
4444 use crate::domain::course::CourseId;
45-use crate::domain::page::PagedList;
45+use crate::db::page::PagedList;
4646 use crate::domain::user::UserId;
4747 use crate::error::{AppError, ValidationError};
4848
src/domain/class_course.rsmodified+1 -1
@@ -12,7 +12,7 @@ use crate::domain::class_blueprint::ClassBlueprintId;
1212 use crate::domain::class_group::ClassGroupId;
1313 use crate::domain::class_pump::{Attached, Axis, attach, detach, link_id};
1414 use crate::domain::course::CourseId;
15-use crate::domain::page::PagedList;
15+use crate::db::page::PagedList;
1616 use crate::domain::timestamp::Timestamp;
1717 use crate::domain::user::UserId;
1818 use crate::error::AppError;
src/domain/class_group.rsmodified+5 -5
@@ -17,10 +17,10 @@ use crate::constant::{
1717 MAX_CLASS_NAME_LEN, TERM_CLASS_COUNT_FIELD,
1818 };
1919 use crate::database::{Database, transaction_with_retry};
20-use crate::domain::cap;
21-use crate::domain::field_update::FieldUpdate;
20+use crate::db::cap;
21+use crate::db::field_update::FieldUpdate;
2222 use crate::domain::monotonic_id::next_ulid;
23-use crate::domain::page::PagedList;
23+use crate::db::page::PagedList;
2424 use crate::domain::term::{self, TermId};
2525 use crate::domain::user::UserId;
2626 use crate::error::{AppError, ValidationError};
@@ -87,7 +87,7 @@ impl ClassGrade {
8787
8888 /// One class. `creator` is who made it; the two refcounts behind the delete
8989 /// guard are database-side columns only, so no whole-row save can clobber one
90-/// (see [`crate::domain::cap`]).
90+/// (see [`crate::db::cap`]).
9191 ///
9292 /// `teacher` is the section's homeroom teacher (sınıf öğretmeni): optional, not
9393 /// refcounted, and merely a label pointing at a teacher-or-higher account — the
@@ -415,7 +415,7 @@ mod tests {
415415 }
416416
417417 /// The stored counter, re-read — never off a return value, which the
418- /// in-memory engine forges wins on (see [`crate::domain::cap`]).
418+ /// in-memory engine forges wins on (see [`crate::db::cap`]).
419419 async fn stored_count(sql: &str, db: &Database) -> i64 {
420420 let mut result = db.query(sql).await.unwrap().check().unwrap();
421421 result
src/domain/class_member.rsmodified+2 -2
@@ -11,7 +11,7 @@ use crate::constant::{CLASS_MEMBER_TABLE, MAX_CLASS_COURSES, MAX_CLASS_MEMBERS};
1111 use crate::database::Database;
1212 use crate::domain::class_group::ClassGroupId;
1313 use crate::domain::class_pump::{Attached, Axis, attach, detach, link_id};
14-use crate::domain::page::PagedList;
14+use crate::db::page::PagedList;
1515 use crate::domain::timestamp::Timestamp;
1616 use crate::domain::user::UserId;
1717 use crate::error::{AppError, ValidationError};
@@ -252,7 +252,7 @@ pub(crate) mod tests {
252252 }
253253
254254 /// A counter, re-read out of the store — never off a return value, which
255- /// the in-memory engine forges wins on (see [`crate::domain::cap`]).
255+ /// the in-memory engine forges wins on (see [`crate::db::cap`]).
256256 pub(crate) async fn counter(field: &str, of: RecordId, db: &Database) -> i64 {
257257 let mut result = db
258258 .query(format!("SELECT VALUE {field} ?? 0 FROM $of"))
src/domain/class_pump.rsmodified+2 -2
@@ -18,7 +18,7 @@
1818 //! a direction flag would be one function containing two, so the seam stays
1919 //! where the SQL puts it.
2020 //!
21-//! Everything here writes counters the way [`crate::domain::cap`] does — a
21+//! Everything here writes counters the way [`crate::db::cap`] does — a
2222 //! single-record conditional `UPDATE`, never a count-then-write — and every
2323 //! `??` is parenthesized, because `n ?? 0 < $cap` parses as `n ?? (0 < $cap)`
2424 //! and is truthy for every row.
@@ -30,7 +30,7 @@ use crate::constant::{
3030 ENROLLMENT_COUNT_FIELD, ENROLLMENT_TABLE, MAX_CLASS_COURSES, MAX_CLASS_MEMBERS,
3131 };
3232 use crate::database::{Database, transaction_with_retry};
33-use crate::domain::cap;
33+use crate::db::cap;
3434 use crate::domain::class_group::{ClassGroup, ClassGroupId};
3535 use crate::error::AppError;
3636
src/domain/course.rsmodified+3 -3
@@ -5,10 +5,10 @@ use crate::constant::{
55 MAX_COURSE_DESCRIPTION_LEN, MAX_COURSE_TITLE_LEN, REF_COUNT_FIELD,
66 };
77 use crate::database::{Database, transaction_with_retry};
8-use crate::domain::cap;
9-use crate::domain::field_update::FieldUpdate;
8+use crate::db::cap;
9+use crate::db::field_update::FieldUpdate;
1010 use crate::domain::monotonic_id::next_ulid;
11-use crate::domain::page::PagedList;
11+use crate::db::page::PagedList;
1212 use crate::domain::term::{self, TermId};
1313 use crate::domain::user::UserId;
1414 use crate::error::{AppError, ValidationError};
src/domain/course_note.rsmodified+3 -3
@@ -4,12 +4,12 @@ use crate::constant::{
44 COURSE_NOTE_TABLE, ENROLLMENT_COUNT_FIELD, MAX_NOTE_CONTENT_LEN, MAX_NOTE_TITLE_LEN,
55 };
66 use crate::database::Database;
7-use crate::domain::cap;
7+use crate::db::cap;
88 use crate::domain::course::CourseId;
99 use crate::domain::course_note_file::CourseNoteFile;
10-use crate::domain::field_update::FieldUpdate;
10+use crate::db::field_update::FieldUpdate;
1111 use crate::domain::monotonic_id::next_ulid;
12-use crate::domain::page::PagedList;
12+use crate::db::page::PagedList;
1313 use crate::domain::user::UserId;
1414 use crate::error::{AppError, ValidationError};
1515 use crate::validate::{validate_optional, validate_required};
src/domain/course_note_file.rsmodified+2 -2
@@ -15,10 +15,10 @@ use crate::constant::{
1515 COURSE_NOTE_FILE_COUNT_FIELD, COURSE_NOTE_FILE_TABLE, MAX_COURSE_NOTE_FILES,
1616 };
1717 use crate::database::Database;
18-use crate::domain::cap;
18+use crate::db::cap;
1919 use crate::domain::course_note::CourseNoteId;
2020 use crate::domain::monotonic_id::next_ulid;
21-use crate::domain::page::PagedList;
21+use crate::db::page::PagedList;
2222 use crate::error::AppError;
2323
2424 #[derive(Debug, Clone, PartialEq, Eq, SurrealValue)]
src/domain/course_session.rsmodified+3 -3
@@ -2,11 +2,11 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue};
22
33 use crate::constant::{COURSE_SESSION_TABLE, ENROLLMENT_COUNT_FIELD, MAX_SESSION_TOPIC_LEN};
44 use crate::database::{Database, transaction_with_retry};
5-use crate::domain::cap;
5+use crate::db::cap;
66 use crate::domain::course::CourseId;
7-use crate::domain::field_update::FieldUpdate;
7+use crate::db::field_update::FieldUpdate;
88 use crate::domain::monotonic_id::next_ulid;
9-use crate::domain::page::PagedList;
9+use crate::db::page::PagedList;
1010 use crate::domain::timestamp::{Timestamp, range_error};
1111 use crate::domain::user::UserId;
1212 use crate::error::{AppError, ValidationError};
src/domain/dietary_profile.rsmodified+1 -1
@@ -14,7 +14,7 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue};
1414
1515 use crate::constant::{DIETARY_PROFILE_TABLE, MAX_DIETARY_NOTE_LEN, MAX_DIETARY_TAGS};
1616 use crate::database::Database;
17-use crate::domain::field_update::FieldUpdate;
17+use crate::db::field_update::FieldUpdate;
1818 use crate::domain::timestamp::Timestamp;
1919 use crate::domain::user::UserId;
2020 use crate::error::{AppError, ValidationError};
src/domain/enrollment.rsmodified+2 -2
@@ -2,10 +2,10 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue};
22
33 use crate::constant::{ENROLLMENT_COUNT_FIELD, ENROLLMENT_TABLE};
44 use crate::database::{Database, transaction_with_retry};
5-use crate::domain::cap;
5+use crate::db::cap;
66 use crate::domain::class_group::ClassGroupId;
77 use crate::domain::course::{Course, CourseId};
8-use crate::domain::page::PagedList;
8+use crate::db::page::PagedList;
99 use crate::domain::role::Role;
1010 use crate::domain::user::UserId;
1111 use crate::error::{AppError, ValidationError};
src/domain/event.rsmodified+1 -1
@@ -8,7 +8,7 @@ use crate::domain::class_group::ClassGroupId;
88 use crate::domain::class_member::{ClassMember, ClassMemberId};
99 use crate::domain::course::CourseId;
1010 use crate::domain::enrollment::Enrollment;
11-use crate::domain::field_update::FieldUpdate;
11+use crate::db::field_update::FieldUpdate;
1212 use crate::domain::monotonic_id::next_ulid;
1313 use crate::domain::registration::Registration;
1414 use crate::domain::role::Role;
src/domain/exam.rsmodified+1 -1
@@ -5,7 +5,7 @@ use crate::constant::{
55 UNLIMITED_EXAM_ATTEMPTS,
66 };
77 use crate::database::{Database, transaction_with_retry};
8-use crate::domain::cap;
8+use crate::db::cap;
99 use crate::domain::course::CourseId;
1010 use crate::domain::monotonic_id::next_ulid;
1111 use crate::domain::settings::ExamKindDef;
src/domain/exam_attempt.rsmodified+3 -2
@@ -6,7 +6,8 @@ use crate::domain::exam::Exam;
66 use crate::domain::exam::ExamId;
77 use crate::domain::timestamp::Timestamp;
88 use crate::domain::user::UserId;
9-use crate::domain::{badge, cap, key};
9+use crate::db::cap;
10+use crate::domain::{badge, key};
1011 use crate::error::AppError;
1112
1213 /// The `THROW` marker the freeze gate aborts with, and the one 409 both it and
@@ -426,7 +427,7 @@ impl ExamAttempt {
426427 //
427428 // corner-cut: the count and a concurrent `CREATE exam_attempt` are still not
428429 // serialized against each other — SurrealDB does not conflict-check a
429- // cross-record count (the write skew `domain::cap` exists for), so an
430+ // cross-record count (the write skew `db::cap` exists for), so an
430431 // attempt landing in the same instant as an edit can still interleave
431432 // either way. The mutex this replaces closed that inside one process only,
432433 // and there are two, so nothing is lost. Closing it properly means the
src/domain/exam_question.rsmodified+3 -3
@@ -9,11 +9,11 @@ use crate::constant::{
99 };
1010 use crate::database::Database;
1111 use crate::domain::bank_question::BankQuestionId;
12-use crate::domain::cap;
12+use crate::db::cap;
1313 use crate::domain::exam::ExamId;
1414 use crate::domain::exam_attempt::ExamAttempt;
1515 use crate::domain::monotonic_id::next_ulid;
16-use crate::domain::page::PagedList;
16+use crate::db::page::PagedList;
1717 use crate::domain::subject::SubjectId;
1818 use crate::error::{AppError, ValidationError};
1919 use crate::validate::{validate_question_kind, validate_question_points, validate_required};
@@ -569,7 +569,7 @@ impl ExamQuestion {
569569 // undeletable forever).
570570 //
571571 // The two are armed on *different* conditions, which is the whole of
572- // the rule ([`crate::domain::field_update::FieldUpdate::refcount`] states
572+ // the rule ([`crate::db::field_update::FieldUpdate::refcount`] states
573573 // it the same way): the counter statements only when the link actually
574574 // changes, but the CAS whenever this write *carries* the link — and it
575575 // always does, because the handler fills an omitted `subject_id` from
src/domain/exam_result.rsmodified+2 -2
@@ -7,7 +7,7 @@ use crate::constant::{
77 KIND_REF_TABLE, MARKS_GIVEN_TOTAL_FIELD, REF_COUNT_FIELD, REF_RETIRED_FIELD,
88 };
99 use crate::database::{Database, transaction_with_retry};
10-use crate::domain::cap;
10+use crate::db::cap;
1111 use crate::domain::course::CourseId;
1212 use crate::domain::exam::ExamId;
1313 use crate::domain::key;
@@ -23,7 +23,7 @@ pub(crate) fn draft_error() -> AppError {
2323
2424 /// The reference counter for one exam kind — how many marks are written under
2525 /// that name, and whether the school has retired it (see
26-/// [`crate::domain::cap`]). Keyed by the name itself: the kind is snapshotted
26+/// [`crate::db::cap`]). Keyed by the name itself: the kind is snapshotted
2727 /// text on the exam, and this row is what makes "a kind nothing is graded under
2828 /// may be removed" a decision the database takes, not a count a concurrent mark
2929 /// can invalidate.
src/domain/fee_plan.rsmodified+2 -2
@@ -20,9 +20,9 @@ use crate::constant::{
2020 FEE_PLAN_TABLE, FEE_PLAN_UNASSIGNED_GUARD, MAX_FEE_PLAN_INSTALLMENTS, MAX_FEE_PLAN_NAME_LEN,
2121 };
2222 use crate::database::{Database, write_with_retry};
23-use crate::domain::field_update::FieldUpdate;
23+use crate::db::field_update::FieldUpdate;
2424 use crate::domain::monotonic_id::next_ulid;
25-use crate::domain::page::PagedList;
25+use crate::db::page::PagedList;
2626 use crate::domain::payment_ledger::LedgerAmount;
2727 use crate::domain::timestamp::Timestamp;
2828 use crate::domain::user::UserId;
src/domain/fee_plan_assignment.rsmodified+2 -2
@@ -19,9 +19,9 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue};
1919
2020 use crate::constant::{FEE_PLAN_ASSIGNMENT_COUNT_FIELD, FEE_PLAN_ASSIGNMENT_TABLE};
2121 use crate::database::Database;
22-use crate::domain::cap::{self, Claimed};
22+use crate::db::cap::{self, Claimed};
2323 use crate::domain::fee_plan::{FeePlan, FeePlanId};
24-use crate::domain::page::PagedList;
24+use crate::db::page::PagedList;
2525 use crate::domain::payment_ledger::PaymentLedger;
2626 use crate::domain::timestamp::Timestamp;
2727 use crate::domain::user::UserId;
src/domain/homework.rsmodified+2 -2
@@ -17,9 +17,9 @@ use crate::constant::{
1717 SUBJECT_HOMEWORK_COUNT_FIELD,
1818 };
1919 use crate::database::Database;
20-use crate::domain::cap;
20+use crate::db::cap;
2121 use crate::domain::course::CourseId;
22-use crate::domain::field_update::FieldUpdate;
22+use crate::db::field_update::FieldUpdate;
2323 use crate::domain::monotonic_id::next_ulid;
2424 use crate::domain::subject::SubjectId;
2525 use crate::domain::timestamp::Timestamp;
src/domain/homework_file.rsmodified+1 -1
@@ -16,7 +16,7 @@ use crate::constant::{
1616 SUBMISSION_OPEN_GUARD,
1717 };
1818 use crate::database::{Database, transaction_with_retry};
19-use crate::domain::cap;
19+use crate::db::cap;
2020 use crate::domain::course::CourseId;
2121 use crate::domain::homework::HomeworkId;
2222 use crate::domain::homework_submission::{HomeworkSubmission, HomeworkSubmissionId};
src/domain/homework_result.rsmodified+1 -1
@@ -188,7 +188,7 @@ impl HomeworkResult {
188188 // no counter column and is SCHEMAFULL, so `due_at` is the one `int` it
189189 // already has; do not invent a second spelling.
190190 //
191- // Not [`crate::domain::cap::touch_and_create`], which is the same shape
191+ // Not [`crate::db::cap::touch_and_create`], which is the same shape
192192 // for a bare `CREATE`: a grade is an UPSERT (a regrade must overwrite),
193193 // and the freeze stamp and the grader's counter have to ride the same
194194 // transaction, which that helper has no room for.
src/domain/meal_attendance.rsmodified+1 -1
@@ -20,7 +20,7 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue};
2020 use crate::constant::{MEAL_ATTENDANCE_STATUSES, MEAL_ATTENDANCE_TABLE};
2121 use crate::database::Database;
2222 use crate::domain::menu::{MenuDate, MenuId, bump_menu_and_write};
23-use crate::domain::page::PagedList;
23+use crate::db::page::PagedList;
2424 use crate::domain::timestamp::Timestamp;
2525 use crate::domain::user::UserId;
2626 use crate::error::{AppError, ValidationError};
src/domain/meal_booking.rsmodified+2 -2
@@ -35,10 +35,10 @@ use crate::constant::{
3535 MENU_VERSION_FIELD,
3636 };
3737 use crate::database::{Database, backoff, lost_the_race};
38-use crate::domain::cap::{self, Claimed};
38+use crate::db::cap::{self, Claimed};
3939 use crate::domain::meal_ledger::{LedgerAmount, MealLedger};
4040 use crate::domain::menu::{Menu, MenuDate, MenuId, MenuSlot};
41-use crate::domain::page::PagedList;
41+use crate::db::page::PagedList;
4242 use crate::domain::settings::{MealSlotDef, Settings};
4343 use crate::domain::timestamp::Timestamp;
4444 use crate::domain::user::UserId;
src/domain/meal_ledger.rsmodified+1 -1
@@ -63,7 +63,7 @@ use crate::database::{Database, lost_the_race};
6363 use crate::domain::meal_booking::{MealBooking, MealBookingId};
6464 use crate::domain::menu::MenuId;
6565 use crate::domain::menu_dish::MenuDish;
66-use crate::domain::page::PagedList;
66+use crate::db::page::PagedList;
6767 // The client-chosen idempotence key both ledgers take — one grammar, one
6868 // validator, one type, rather than a second newtype that could drift from it.
6969 use crate::domain::payment_ledger::PaymentRequestKey;
src/domain/menu.rsmodified+4 -4
@@ -18,8 +18,8 @@ use crate::constant::{
1818 MENU_VERSION_FIELD, REF_COUNT_FIELD, SLOT_REF_TABLE,
1919 };
2020 use crate::database::{Database, transaction_with_retry, write_with_retry};
21-use crate::domain::cap;
22-use crate::domain::page::PagedList;
21+use crate::db::cap;
22+use crate::db::page::PagedList;
2323 use crate::domain::settings::MealSlotDef;
2424 use crate::domain::timestamp::Timestamp;
2525 use crate::domain::user::UserId;
@@ -30,7 +30,7 @@ use crate::error::{AppError, ValidationError};
3030 /// conflict-check). Publishing no longer needs it — the day+slot *is* the
3131 /// record id — and neither does a booking, a menu delete, or the settings
3232 /// slot-removal guard: those went to conditional single-record writes
33-/// ([`crate::domain::cap`]), which the store decides as this lock cannot.
33+/// ([`crate::db::cap`]), which the store decides as this lock cannot.
3434 //
3535 // corner-cut: the dish cap therefore rests on this lock alone — a dish write
3636 // added without taking it reopens the count-then-write hole silently. Closing
@@ -49,7 +49,7 @@ pub(crate) static MENU_LOCK: Mutex<()> = Mutex::const_new(());
4949
5050 /// The reference counter for one meal slot — how many menus are published under
5151 /// that name, and whether the school has retired it (see
52-/// [`crate::domain::cap`]). The mirror of
52+/// [`crate::db::cap`]). The mirror of
5353 /// [`kind_ref`](crate::domain::exam_result::kind_ref) for exam kinds: the slot
5454 /// is snapshotted text on the menu, so this row is the only place the two
5555 /// tables' relationship is a single record concurrent writes can contend on.
src/domain/menu_dish.rsmodified+1 -1
@@ -289,7 +289,7 @@ impl MenuDish {
289289 /// it takes the three-way shape: absent = keep, `Some(None)` = clear.
290290 ///
291291 /// The `SET` is built here rather than by
292- /// [`FieldUpdate`](crate::domain::field_update::FieldUpdate) because the
292+ /// [`FieldUpdate`](crate::db::field_update::FieldUpdate) because the
293293 /// revision bump lands on *another* row and has to share this write's
294294 /// transaction; the field-by-field scoping — an omitted field is never
295295 /// written, so a concurrent PATCH of another one is not reverted — is the
src/domain/message.rsmodified+1 -1
@@ -14,7 +14,7 @@ use crate::constant::{
1414 };
1515 use crate::database::Database;
1616 use crate::domain::monotonic_id::next_ulid;
17-use crate::domain::page::PagedList;
17+use crate::db::page::PagedList;
1818 use crate::domain::timestamp::Timestamp;
1919 use crate::domain::user::UserId;
2020 use crate::error::{AppError, ValidationError};
src/domain/mod.rsmodified+0 -3
@@ -12,7 +12,6 @@ pub mod bank_question_image;
1212 pub mod board;
1313 pub mod board_stroke;
1414 pub mod builder;
15-pub mod cap;
1615 pub mod chatbot_message;
1716 pub mod chatbot_thread;
1817 pub mod class_blueprint;
@@ -34,7 +33,6 @@ pub mod exam_question;
3433 pub mod exam_result;
3534 pub mod fee_plan;
3635 pub mod fee_plan_assignment;
37-pub mod field_update;
3836 pub mod homework;
3937 pub mod homework_file;
4038 pub mod homework_result;
@@ -49,7 +47,6 @@ pub mod message;
4947 pub mod monotonic_id;
5048 pub mod note;
5149 pub mod note_file;
52-pub mod page;
5350 pub mod parent_link;
5451 pub mod payment_ledger;
5552 pub mod pomodoro;
src/domain/note.rsmodified+2 -2
@@ -2,10 +2,10 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue};
22
33 use crate::constant::{MAX_NOTE_CONTENT_LEN, MAX_NOTE_TITLE_LEN, NOTE_TABLE};
44 use crate::database::Database;
5-use crate::domain::field_update::FieldUpdate;
5+use crate::db::field_update::FieldUpdate;
66 use crate::domain::monotonic_id::next_ulid;
77 use crate::domain::note_file::NoteFile;
8-use crate::domain::page::PagedList;
8+use crate::db::page::PagedList;
99 use crate::domain::user::UserId;
1010 use crate::error::{AppError, ValidationError};
1111 use crate::validate::{validate_optional, validate_required};
src/domain/note_file.rsmodified+2 -2
@@ -12,10 +12,10 @@ use crate::constant::{
1212 NOTE_FILE_TABLE,
1313 };
1414 use crate::database::Database;
15-use crate::domain::cap;
15+use crate::db::cap;
1616 use crate::domain::monotonic_id::next_ulid;
1717 use crate::domain::note::NoteId;
18-use crate::domain::page::PagedList;
18+use crate::db::page::PagedList;
1919 use crate::error::{AppError, ValidationError};
2020
2121 #[derive(Debug, Clone, PartialEq, Eq, SurrealValue)]
src/domain/payment_ledger.rsmodified+1 -1
@@ -98,7 +98,7 @@ use crate::domain::fee_plan_assignment::{FeePlanAssignment, FeePlanAssignmentId}
9898 // error message for both ledgers.
9999 pub use crate::domain::meal_ledger::{LedgerAmount, LedgerMethod, LedgerNote};
100100 use crate::domain::monotonic_id::next_ulid;
101-use crate::domain::page::PagedList;
101+use crate::db::page::PagedList;
102102 use crate::domain::timestamp::Timestamp;
103103 use crate::domain::user::UserId;
104104 use crate::error::{AppError, ValidationError};
src/domain/pomodoro.rsmodified+1 -1
@@ -497,7 +497,7 @@ mod tests {
497497 /// contention on one user row.
498498 ///
499499 /// Multi-threaded and off the embedded engine for the reason
500- /// [`crate::domain::cap`] spells out: the in-memory engine does not
500+ /// [`crate::db::cap`] spells out: the in-memory engine does not
501501 /// conflict-check two concurrent writes to one record, so it would answer
502502 /// `Ok` to a write it dropped and *forge* the integrity failure this test
503503 /// exists to catch.
src/domain/rag_output.rsmodified+1 -1
@@ -20,7 +20,7 @@ use crate::domain::course::CourseId;
2020 use crate::domain::course_note::CourseNoteId;
2121 use crate::domain::course_note_file::CourseNoteFileId;
2222 use crate::domain::monotonic_id::next_ulid;
23-use crate::domain::page::PagedList;
23+use crate::db::page::PagedList;
2424 use crate::domain::timestamp::Timestamp;
2525 use crate::error::AppError;
2626
src/domain/registration.rsmodified+1 -1
@@ -2,7 +2,7 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue};
22
33 use crate::constant::{REGISTRATION_COUNT_FIELD, REGISTRATION_TABLE};
44 use crate::database::Database;
5-use crate::domain::cap;
5+use crate::db::cap;
66 use crate::domain::event::{Event, EventId};
77 use crate::domain::role::Role;
88 use crate::domain::user::{User, UserId};
src/domain/session_attendance.rsmodified+1 -1
@@ -8,7 +8,7 @@ use crate::database::{Database, transaction_with_retry};
88 use crate::domain::attendance::AttendanceStatus;
99 use crate::domain::course::CourseId;
1010 use crate::domain::course_session::{CourseSession, CourseSessionId};
11-use crate::domain::page::PagedList;
11+use crate::db::page::PagedList;
1212 use crate::domain::timestamp::Timestamp;
1313 use crate::domain::user::UserId;
1414 use crate::error::AppError;
src/domain/settings.rsmodified+1 -1
@@ -46,7 +46,7 @@ use crate::error::{AppError, ValidationError};
4646 /// So the pair is serialized instead. Every writer of the singleton goes
4747 /// through `PATCH /settings`, and the deployment runs one process by contract
4848 /// (stop-the-world upgrades), so process-wide is deployment-wide here — the
49-/// same argument [`crate::domain::cap`]'s own lock makes for `retire_name`'s
49+/// same argument [`crate::db::cap`]'s own lock makes for `retire_name`'s
5050 /// two statements, one level up. The compare-and-set stays: it is what keeps a
5151 /// crashed or rolled-back attempt from writing a list nobody merged.
5252 ///
src/domain/solution.rsmodified+1 -1
@@ -15,7 +15,7 @@ use crate::constant::{MAX_SOLUTION_BODY_LEN, SOLUTION_TABLE};
1515 use crate::database::Database;
1616 use crate::domain::monotonic_id::next_ulid;
1717 use crate::domain::note_file::FileContentType;
18-use crate::domain::page::PagedList;
18+use crate::db::page::PagedList;
1919 use crate::domain::pool_question::{PoolQuestionId, bump_question_and_write};
2020 use crate::domain::timestamp::Timestamp;
2121 use crate::domain::user::UserId;
src/domain/subject.rsmodified+3 -3
@@ -4,11 +4,11 @@ use crate::constant::{
44 ENROLLMENT_COUNT_FIELD, MAX_SUBJECT_DESCRIPTION_LEN, MAX_SUBJECT_NAME_LEN, SUBJECT_TABLE,
55 };
66 use crate::database::{Database, transaction_with_retry};
7-use crate::domain::cap;
7+use crate::db::cap;
88 use crate::domain::course::CourseId;
9-use crate::domain::field_update::FieldUpdate;
9+use crate::db::field_update::FieldUpdate;
1010 use crate::domain::monotonic_id::next_ulid;
11-use crate::domain::page::PagedList;
11+use crate::db::page::PagedList;
1212 use crate::error::{AppError, ValidationError};
1313 use crate::validate::{validate_optional, validate_required};
1414
src/domain/term.rsmodified+2 -2
@@ -10,9 +10,9 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue};
1010
1111 use crate::constant::{COURSE_COUNT_FIELD, MAX_TERM_NAME_LEN, TERM_CLASS_COUNT_FIELD, TERM_TABLE};
1212 use crate::database::{Database, write_with_retry};
13-use crate::domain::field_update::FieldUpdate;
13+use crate::db::field_update::FieldUpdate;
1414 use crate::domain::monotonic_id::next_ulid;
15-use crate::domain::page::PagedList;
15+use crate::db::page::PagedList;
1616 use crate::domain::timestamp::{Timestamp, range_error};
1717 use crate::error::{AppError, ValidationError};
1818 use crate::validate::validate_required;
src/domain/user.rsmodified+4 -4
@@ -13,10 +13,10 @@ use crate::constant::{
1313 use crate::database::{Database, transaction_with_retry, write_with_retry};
1414 use crate::domain::appointment::AppointmentStatus;
1515 use crate::domain::board::Board;
16-use crate::domain::field_update::FieldUpdate;
16+use crate::db::field_update::FieldUpdate;
1717 use crate::domain::monotonic_id::next_ulid;
1818 use crate::domain::note_file::FileContentType;
19-use crate::domain::page::PagedList;
19+use crate::db::page::PagedList;
2020 use crate::domain::preferences::{Language, PaletteColor, Theme};
2121 use crate::domain::profile::{Bio, BirthDate, DisplayName, Email, PersonName, Phone};
2222 use crate::domain::role::Role;
@@ -31,7 +31,7 @@ use crate::validate::{validate_password, validate_username};
3131 /// guard is a count-then-write: read whether another admin exists, then lower
3232 /// this row. SurrealDB conflict-checks neither side of that pair — a
3333 /// `BEGIN…COMMIT` does not serialize a cross-record count against a concurrent
34-/// update (write-skew, see [`crate::domain::cap`]) and a statement that only
34+/// update (write-skew, see [`crate::db::cap`]) and a statement that only
3535 /// *reads* the rival's row never collides with it. So two admins demoting each
3636 /// other both counted the other and both committed, leaving **zero** admins and
3737 /// a school nobody can administer: `ensure_admin` refuses to promote an
@@ -41,7 +41,7 @@ use crate::validate::{validate_password, validate_username};
4141 /// Serializing the pair is what closes it, the same argument
4242 /// [`crate::domain::settings::SETTINGS_LOCK`] makes one level up: the deployment
4343 /// runs one process by contract (stop-the-world upgrades), so process-wide is
44-/// deployment-wide. A per-row counter (the [`crate::domain::cap`] shape) does
44+/// deployment-wide. A per-row counter (the [`crate::db::cap`] shape) does
4545 /// not fit — the count being capped is over *every* user row, with no parent
4646 /// record to hold it and no place to seed one without a backfill.
4747 ///
src/domain/work_entry.rsmodified+2 -2
@@ -2,9 +2,9 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue};
22
33 use crate::constant::WORK_ENTRY_TABLE;
44 use crate::database::{Database, transaction_with_retry};
5-use crate::domain::field_update::FieldUpdate;
5+use crate::db::field_update::FieldUpdate;
66 use crate::domain::monotonic_id::next_ulid;
7-use crate::domain::page::PagedList;
7+use crate::db::page::PagedList;
88 use crate::domain::timestamp::Timestamp;
99 use crate::domain::user::UserId;
1010 use crate::error::{AppError, ValidationError};
src/lib.rsmodified+1 -0
@@ -2,6 +2,7 @@ pub mod ai;
22 pub mod config;
33 pub mod constant;
44 pub mod database;
5+pub mod db;
56 pub mod domain;
67 pub mod error;
78 pub mod migration_sql;
src/migration_sql.rsmodified+3 -3
@@ -280,7 +280,7 @@ pub const MIGRATION: &str = "
280280 DEFINE FIELD IF NOT EXISTS audience.class ON event TYPE option<record<class_group>>;
281281 DEFINE FIELD IF NOT EXISTS audience.capacity ON event TYPE option<int>;
282282 -- Seats taken on the signup list, the stored capacity guard that closes
283- -- write-skew between concurrent request tasks (see `crate::domain::cap`).
283+ -- write-skew between concurrent request tasks (see `crate::db::cap`).
284284 -- Absent reads as zero, so no Rust struct needs it.
285285 DEFINE FIELD IF NOT EXISTS registration_count ON event TYPE option<int>;
286286 REMOVE FIELD IF EXISTS audience.users ON TABLE event;
@@ -311,7 +311,7 @@ pub const MIGRATION: &str = "
311311 -- is exactly what an absent stamp already means.
312312 DEFINE FIELD IF NOT EXISTS archived_at ON term TYPE option<int>;
313313 -- Courses still linking this term, the stored delete guard that closes
314- -- write-skew between concurrent request tasks (see `crate::domain::cap`).
314+ -- write-skew between concurrent request tasks (see `crate::db::cap`).
315315 -- Absent reads as zero, so no Rust struct needs it.
316316 DEFINE FIELD IF NOT EXISTS course_count ON term TYPE option<int>;
317317 -- Classes linking this term, the other half of the same guard. A column of
@@ -782,7 +782,7 @@ pub const MIGRATION: &str = "
782782 DEFINE FIELD IF NOT EXISTS date ON menu TYPE string READONLY;
783783 DEFINE FIELD IF NOT EXISTS slot ON menu TYPE string READONLY;
784784 DEFINE FIELD IF NOT EXISTS capacity ON menu TYPE option<int>;
785- -- `seats_booked` is the seat cap's counter (see `domain::cap`) and
785+ -- `seats_booked` is the seat cap's counter (see `db::cap`) and
786786 -- `version` the menu's revision: a booking claims its seat only while the
787787 -- menu still stands at the revision it read the price at, so a dish
788788 -- re-priced mid-booking cannot be billed as the old price. Both absent
src/tenant.rsmodified+1 -1
@@ -30,7 +30,7 @@ use surrealdb::types::{RecordId, RecordIdKey, SurrealValue};
3030 use crate::config::Config;
3131 use crate::constant::{MAX_SLUG_LEN, MIN_SLUG_LEN};
3232 use crate::database::{Database, lost_the_race, migrate, migrate_control};
33-use crate::domain::page::PagedList;
33+use crate::db::page::PagedList;
3434 use crate::domain::timestamp::Timestamp;
3535 use crate::error::{AppError, ValidationError};
3636 use crate::module::ModuleSet;
src/web/exams/mod.rsmodified+1 -1
@@ -72,7 +72,7 @@ impl Scheduled for Exam {
7272 }
7373
7474 /// Serializes the exam subsystem's cross-record check-then-writes, which
75-/// `BEGIN…COMMIT` cannot (write skew) — the reasoning in [`crate::domain::cap`].
75+/// `BEGIN…COMMIT` cannot (write skew) — the reasoning in [`crate::db::cap`].
7676 /// It orders requests, but only around what it wraps — every invariant that
7777 /// could be moved into the database itself has been, so a gap between a read
7878 /// and its write is decided by the store. What is left here needs a
src/web/homework.rsmodified+1 -1
@@ -78,7 +78,7 @@ use super::{
7878 /// delete used to take is on this list any more.
7979 ///
8080 /// Lock order, where both are taken: `HOMEWORK_LOCK` before the counter lock in
81-/// [`crate::domain::cap`], never the reverse.
81+/// [`crate::db::cap`], never the reverse.
8282 // corner-cut: global RwLock, shard per-homework if write latency ever matters.
8383 pub(crate) static HOMEWORK_LOCK: tokio::sync::RwLock<()> = tokio::sync::RwLock::const_new(());
8484
src/web/mod.rsmodified+2 -2
@@ -147,7 +147,7 @@ pub(crate) async fn undo_if_demoted(target: &UserId, db: &Database) -> Result<()
147147 /// If both ends are present, `ends_at` must not precede `starts_at`. Shared by
148148 /// everything that carries a time range (events, course sessions). The PATCH
149149 /// paths re-check this at write time via
150-/// [`crate::domain::field_update::FieldUpdate::ordered`], which answers with the
150+/// [`crate::db::field_update::FieldUpdate::ordered`], which answers with the
151151 /// same error — this is the pre-flight, that is the race closer.
152152 pub(crate) fn check_time_range(
153153 starts_at: Option<Timestamp>,
@@ -405,7 +405,7 @@ mod tests {
405405 /// refuse; with the account still staff it must undo nothing at all.
406406 ///
407407 /// Asserted on stored state, never on a return value: the in-memory engine
408- /// forges those (see [`crate::domain::cap`]).
408+ /// forges those (see [`crate::db::cap`]).
409409 #[tokio::test]
410410 async fn undo_if_demoted_repairs_both_assignments_or_neither() {
411411 use crate::domain::class_group::{ClassGroup, ClassName};
src/web/settings.rsmodified+1 -1
@@ -9,7 +9,7 @@ use surrealdb::types::RecordId;
99
1010 use crate::constant::CAS_UPDATE_RETRIES;
1111 use crate::database::Database;
12-use crate::domain::cap;
12+use crate::db::cap;
1313 use crate::domain::exam_result::kind_ref;
1414 use crate::domain::menu::slot_ref;
1515 use crate::domain::settings::{ExamKindDef, GradeBand, MealSlotDef, SETTINGS_LOCK, Settings};