summaryrefslogtreecommitdiffstats
path: root/src/db/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/mod.rs')
-rw-r--r--src/db/mod.rs73
1 files changed, 22 insertions, 51 deletions
diff --git a/src/db/mod.rs b/src/db/mod.rs
index 403ced2..c271fe7 100644
--- a/src/db/mod.rs
+++ b/src/db/mod.rs
@@ -1,13 +1,3 @@
-//! SQLite access (DESIGN.md §11a).
-//!
-//! The app is multi-threaded and SQLite has one writer at a time, so there is
-//! exactly **one writer thread** owning the write connection. Everything that
-//! mutates is sent to it over a channel as a closure and answered with a
-//! `Result`. That makes "who can write" a compile-time fact (only [`Writer`]
-//! hands out `&mut`-equivalent access) and removes `SQLITE_BUSY` from the
-//! design entirely. Readers are thread-local, read-only connections; WAL lets
-//! them proceed concurrently with the writer.
-
pub mod approvals;
pub mod builds;
pub mod candidates;
@@ -18,11 +8,8 @@ pub mod queries;
pub use migrations::MIGRATIONS;
pub use model::*;
-/// Parse a state column into one of the state enums.
-///
-/// A value the code does not recognise fails the row instead of becoming a
-/// default: a build in a state we cannot name must not be listed, claimed, or
-/// rendered as if it were in one we could.
+/// Parse a state column into one of the state enums. A value the code does not
+/// recognise fails the row instead of becoming a default.
pub(crate) fn state_col<T: StateEnum>(col: usize, text: &str) -> rusqlite::Result<T> {
T::parse(text).ok_or_else(|| {
rusqlite::Error::FromSqlConversionFailure(
@@ -158,17 +145,13 @@ impl Writer {
self.alive.load(Ordering::SeqCst)
}
- /// Run `f` on the write connection and wait for its result.
- ///
- /// No transaction may span a build step: `f` should be short.
- /// Use [`Writer::tx`] for atomic multi-statement mutations.
+ /// Run `f` on the write connection and wait for its result. No transaction
+ /// may span a build step: `f` should be short.
///
- /// `pub(crate)`, not `pub`: the schema is the system's most protected asset
- /// and it has one owner, this module (§6 M-1). Code outside `db/` asks for a
- /// typed query instead of writing SQL. `pub(crate)` is what keeps the
- /// *crate's* users (the integration tests, the shim binaries) out; inside
- /// the crate the convention is enforced by `tests/schema_ownership.rs`,
- /// which fails the build if a SQL statement appears outside `src/db/`.
+ /// `pub(crate)`, not `pub`: the schema has one owner, this module (§6 M-1).
+ /// Inside the crate the convention is enforced by
+ /// `tests/schema_ownership.rs`, which fails the build if a SQL statement
+ /// appears outside `src/db/`.
pub(crate) fn call<T>(&self, f: impl FnOnce(&mut Connection) -> Result<T> + Send + 'static) -> Result<T>
where
T: Send + 'static,
@@ -182,8 +165,7 @@ impl Writer {
.map_err(|_| Error::db("writer actor gone"))?
}
- /// Run `f` inside a single `BEGIN IMMEDIATE` transaction on the write
- /// connection. This is how every multi-statement mutation is expressed.
+ /// Run `f` inside a single `BEGIN IMMEDIATE` transaction.
pub(crate) fn tx<T>(
&self,
f: impl FnOnce(&mut rusqlite::Transaction<'_>) -> Result<T> + Send + 'static,
@@ -216,17 +198,12 @@ impl Writer {
}
}
-/// The raw-SQL door, open only for the test harness (§6 M-9).
-///
-/// A fixture sometimes has to put the database in a state the typed API only
-/// reaches through several steps — a release published without a build, a
-/// pointer moved out from under a review. That is a reason to have a door, not
-/// a reason to leave it unlocked in the binary an operator runs, so it exists
-/// only under the `test-support` feature, which nothing but this crate's own
-/// dev-dependency enables.
+/// The raw-SQL door, open only for the test harness (§6 M-9). A fixture
+/// sometimes has to reach a state the typed API only reaches through several
+/// steps, but that is not a reason to leave the door unlocked in the binary an
+/// operator runs.
#[cfg(feature = "test-support")]
impl Writer {
- /// Run `f` in a transaction. Test fixtures only.
pub fn tx_for_tests<T>(
&self,
f: impl FnOnce(&mut rusqlite::Transaction<'_>) -> Result<T> + Send + 'static,
@@ -237,7 +214,6 @@ impl Writer {
self.tx(f)
}
- /// Run `f` on the write connection. Test fixtures only.
pub fn call_for_tests<T>(&self, f: impl FnOnce(&mut Connection) -> Result<T> + Send + 'static) -> Result<T>
where
T: Send + 'static,
@@ -246,10 +222,9 @@ impl Writer {
}
}
-/// Thread-local read-only connection pool.
-///
-/// One connection per thread per database file, opened read-only. HTTP handlers
-/// and watchers use this; they never touch the write connection.
+/// Thread-local read-only connection pool: one connection per thread per
+/// database file. HTTP handlers and watchers use this; they never touch the
+/// write connection.
pub struct Readers {
path: PathBuf,
}
@@ -275,9 +250,8 @@ impl Readers {
.iter()
.position(|(p, _)| p == &path)
.ok_or_else(|| Error::db("reader pool lost entry"))?;
- // Safety of the borrow: we only ever use one connection at a time
- // per thread and `f` cannot re-enter this function for the same pool
- // (it receives &Connection, not &Readers).
+ // We only ever use one connection at a time per thread, and `f`
+ // receives &Connection rather than &Readers, so it cannot re-enter.
let conn = &pool[idx].1;
f(conn)
})
@@ -321,13 +295,11 @@ pub fn backup_to(writer: &Writer, dest: &Path) -> Result<()> {
})
}
-/// The schema version a database file reports. A backup that does not open and
-/// answer with a migrated schema is a file, not a backup, and the only way to
-/// find that out is to ask it.
+/// The schema version a database file reports. A backup that does not answer
+/// with a migrated schema is a file, not a backup.
///
-/// Opened read-write and untouched by the connection pragmas: the point is to
-/// read one number from a file we are not going to modify, and a WAL database
-/// cannot even be opened read-only (it needs its sidecar).
+/// Opened read-write and untouched by the connection pragmas: a WAL database
+/// cannot even be opened read-only.
pub fn schema_version(path: &Path) -> Result<i64> {
let conn = rusqlite::Connection::open(path)
.map_err(|e| Error::db(format!("{} does not open: {e}", path.display())))?;
@@ -335,7 +307,6 @@ pub fn schema_version(path: &Path) -> Result<i64> {
.map_err(|e| Error::db(format!("{} has no schema version: {e}", path.display())))
}
-/// WAL size + checkpoint lag, surfaced on the Ops page and in metrics.
pub fn wal_stats(db: &Db) -> (u64, u64) {
let db_file = std::fs::metadata(db.writer.path()).map(|m| m.len()).unwrap_or(0);
let mut wal = db.writer.path().as_os_str().to_os_string();