diff options
Diffstat (limited to 'src/db/queries.rs')
| -rw-r--r-- | src/db/queries.rs | 63 |
1 files changed, 54 insertions, 9 deletions
diff --git a/src/db/queries.rs b/src/db/queries.rs index 23b95ec..66bab73 100644 --- a/src/db/queries.rs +++ b/src/db/queries.rs @@ -10,11 +10,16 @@ use crate::time; // ---------------------------------------------------------------- settings -- /// Every setting, for the settings page. +/// +/// Row-error policy, applied everywhere in `db` (m-9): a row that cannot be read +/// fails the query. `filter_map(|r| r.ok())` used to drop such rows silently, +/// which is fine for a page that merely looks incomplete and fatal for the +/// callers that decide what to reap, what to claim, and what is stale. pub fn all_settings(db: &Db) -> Result<Vec<(String, String)>> { db.read(|c| { let mut st = c.prepare("SELECT key, value FROM settings ORDER BY key")?; let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?; - Ok(rows.filter_map(|r| r.ok()).collect()) + Ok(rows.collect::<::rusqlite::Result<Vec<_>>>()?) }) } @@ -26,12 +31,27 @@ pub fn setting(db: &Db, key: &str) -> Result<Option<String>> { }) } +/// An integer setting, or `default`. +/// +/// The policy, stated once (m-9): a *missing* setting is normal — the default is +/// the answer, and every knob in `settings` has one. A setting that is present +/// but unparseable, or a read that failed outright, is not normal, and silently +/// returning the default would hide an operator's typo behind a config that +/// looks applied. Both still fall back — these are tuning knobs, and refusing to +/// start over `stage_b_cooldown = 86400x` is worse than using the default — but +/// they fall back out loud. pub fn setting_i64(db: &Db, key: &str, default: i64) -> i64 { - setting(db, key) - .ok() - .flatten() - .and_then(|v| v.parse().ok()) - .unwrap_or(default) + match setting(db, key) { + Ok(None) => default, + Ok(Some(v)) => v.parse().unwrap_or_else(|_| { + eprintln!("settings: {key}={v:?} is not an integer; using default {default}"); + default + }), + Err(e) => { + eprintln!("settings: could not read {key} ({e}); using default {default}"); + default + } + } } pub fn set_setting(writer: &Writer, key: &str, value: &str) -> Result<()> { @@ -506,8 +526,33 @@ const RELEASE_COLS_R: &str = "r.id, r.pkgbase_id, r.candidate_id, r.approval_id, /// Withdraw a published release and remove the packages it served (§9 /// `repo-remove`, decision 23). Runs in the caller's transaction so the removal /// and the audit row that explains it are one fact. +/// Take a release out of the served index (§9, decision 23). +/// +/// Refuses a release that is not currently published (m-10): re-withdrawing used +/// to report success and rewrite nothing, so an operator who clicked twice — or +/// withdrew a release that had never been live — got a green result and no +/// explanation. The write is a compare-and-swap on `state='published'`, and the +/// refusal names the state that was actually there. pub fn withdraw_release_in(tx: &rusqlite::Transaction<'_>, release_id: i64) -> Result<()> { - tx.execute("UPDATE release SET state='withdrawn' WHERE id=?1", params![release_id])?; + let n = tx.execute( + "UPDATE release SET state='withdrawn' WHERE id=?1 AND state='published'", + params![release_id], + )?; + if n == 0 { + let state: Option<String> = tx + .query_row("SELECT state FROM release WHERE id=?1", params![release_id], |r| r.get(0)) + .optional()?; + return Err(match state.as_deref().map(ReleaseState::parse) { + None => Error::not_found(format!("no release {release_id}")), + Some(Some(s)) => Error::conflict(format!( + "release {release_id} is {s}, not published: nothing to withdraw" + )), + Some(None) => Error::invalid(format!( + "release {release_id} has a state outside the state machine: {:?}", + state.unwrap_or_default() + )), + }); + } tx.execute("UPDATE package SET state='removed' WHERE release_id=?1", params![release_id])?; Ok(()) } @@ -853,7 +898,7 @@ pub fn published_world_members(db: &Db) -> Result<Vec<(String, String)>> { WHERE r.state = 'published'", )?; let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?; - Ok(rows.filter_map(|r| r.ok()).collect::<Vec<_>>()) + Ok(rows.collect::<::rusqlite::Result<Vec<_>>>()?) }) } @@ -877,7 +922,7 @@ pub fn served_vs_offered_pkgvers(db: &Db) -> Result<Vec<(String, String, String) r.get::<_, Option<String>>(2)?.unwrap_or_default(), )) })?; - Ok(rows.filter_map(|r| r.ok()).collect::<Vec<_>>()) + Ok(rows.collect::<::rusqlite::Result<Vec<_>>>()?) }) } |
