harness stores: a failed lookup is not no-row, bulk done is one transaction, the reminder cap is checked with the insert
This commit is contained in:
parent
19357bca2d
commit
e3bf401f6e
1 changed files with 155 additions and 17 deletions
|
|
@ -34,7 +34,7 @@ use std::sync::Mutex;
|
|||
use anyhow::{Context, Result};
|
||||
use chrono::{DateTime, Utc};
|
||||
use hive_sh4re::wire_time;
|
||||
use rusqlite::{Connection, params};
|
||||
use rusqlite::{Connection, OptionalExtension as _, params};
|
||||
|
||||
/// SQL bootstrap. `CREATE TABLE IF NOT EXISTS` so first-boot agents and
|
||||
/// existing ones converge on the same base shape; additive columns land via
|
||||
|
|
@ -161,7 +161,10 @@ impl Todos {
|
|||
params![subsystem, key],
|
||||
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)),
|
||||
)
|
||||
.ok()
|
||||
// Only "no such row" is `None`: a lookup that failed (a busy
|
||||
// database) must not fall through to an `INSERT` that would
|
||||
// duplicate a keyless row or trip the dedup index on a keyed one.
|
||||
.optional()?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
|
@ -295,7 +298,7 @@ impl Todos {
|
|||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Propagates the sqlite update failure.
|
||||
/// Propagates the sqlite update failure; nothing is acked then.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
|
|
@ -304,15 +307,19 @@ impl Todos {
|
|||
if ids.is_empty() {
|
||||
return Ok(0);
|
||||
}
|
||||
let conn = self.conn.lock().unwrap();
|
||||
let mut conn = self.conn.lock().unwrap();
|
||||
let now = Utc::now().timestamp();
|
||||
// One transaction: all of the ids are acked or none, and the commit
|
||||
// is one fsync rather than one per id.
|
||||
let tx = conn.transaction()?;
|
||||
let mut total = 0usize;
|
||||
for id in ids {
|
||||
total += conn.execute(
|
||||
total += tx.execute(
|
||||
"UPDATE todos SET acked = 1, acked_at = ?1 WHERE acked = 0 AND id = ?2",
|
||||
params![now, id],
|
||||
)?;
|
||||
}
|
||||
tx.commit()?;
|
||||
Ok(total)
|
||||
}
|
||||
|
||||
|
|
@ -666,6 +673,30 @@ mod tests {
|
|||
|
||||
/// `mark_done_many` acks exactly the ids passed, leaves the rest
|
||||
/// untouched — no threshold/range semantics.
|
||||
#[test]
|
||||
fn mark_done_many_is_all_or_nothing() {
|
||||
let (_dir, todos) = store();
|
||||
let (a, _) = todos.upsert("bash", Some("a"), "one", None, false).unwrap();
|
||||
let (b, _) = todos.upsert("bash", Some("b"), "two", None, false).unwrap();
|
||||
todos
|
||||
.conn
|
||||
.lock()
|
||||
.unwrap()
|
||||
.execute_batch(&format!(
|
||||
"CREATE TRIGGER fail_one BEFORE UPDATE ON todos WHEN NEW.id = {b} \
|
||||
BEGIN SELECT RAISE(ABORT, 'boom'); END;"
|
||||
))
|
||||
.unwrap();
|
||||
assert!(todos.mark_done_many(&[a, b]).is_err());
|
||||
let open: Vec<_> = todos
|
||||
.list(None)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|t| t.id)
|
||||
.collect();
|
||||
assert!(open.contains(&a) && open.contains(&b), "{open:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mark_done_many_acks_only_the_listed_ids() {
|
||||
let (_dir, s) = store();
|
||||
|
|
|
|||
Loading…
Reference in a new issue