1#[cfg(any(test, feature = "test-utils"))]
6use super::table;
7use super::{Phase, SnapshotStaging, StagedTables};
8use crate::snapshot_error::SnapshotError;
9use crate::sqlite::StoreSql;
10use crate::DbError;
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub struct StagedDigest([u8; 32]);
18
19pub(super) fn digest(db: &StoreSql, tables: &StagedTables) -> Result<StagedDigest, DbError> {
22 let mut hasher = coven_crypto::ContentHasher::new();
23 let mut table = |name: &str, select: &str, params: &[&dyn sqlive::rusqlite::ToSql]| {
24 let mut rows = Vec::new();
25 db.for_each(select, params, |row| {
26 rows.push(encoded(row)?);
27 Ok::<_, DbError>(())
28 })?;
29 rows.sort_unstable();
30 hasher.update(&(name.len() as u64).to_be_bytes());
31 hasher.update(name.as_bytes());
32 hasher.update(&(rows.len() as u64).to_be_bytes());
33 for row in rows {
34 hasher.update(&row);
35 }
36 Ok::<_, DbError>(())
37 };
38 let staging = format!("_coven_staged_{}_", tables.id);
40 for name in &tables.tables {
41 table(&name.replacen(&staging, "", 1), &format!("SELECT * FROM {name}"), &[])?;
42 }
43 table(
44 "_coven_snapshot_writes",
45 "SELECT device,number,timestamp,read_through,slot FROM temp._coven_snapshot_writes WHERE source=?1",
46 &[&tables.id],
47 )?;
48 Ok(StagedDigest(*hasher.finish().as_bytes()))
49}
50
51pub(super) fn unchanged(db: &StoreSql, staging: &SnapshotStaging) -> Result<(), DbError> {
55 let (Phase::Staged { digest, .. }, Some(tables)) = (&staging.phase, &staging.tables) else {
56 return Ok(());
57 };
58 if self::digest(db, tables)? != *digest {
59 tracing::warn!("a snapshot's staged records changed on this device since staging");
60 return Err(DbError::DamagedDatabase);
61 }
62 Ok(())
63}
64
65fn encoded(row: &sqlive::rusqlite::Row<'_>) -> Result<Vec<u8>, DbError> {
68 use sqlive::rusqlite::types::ValueRef;
69 let mut bytes = Vec::new();
70 for i in 0..row.as_ref().column_count() {
71 match row.get_ref(i)? {
72 ValueRef::Null => bytes.push(0),
73 ValueRef::Integer(value) => {
74 bytes.push(1);
75 bytes.extend_from_slice(&value.to_be_bytes());
76 }
77 ValueRef::Real(value) => {
78 bytes.push(2);
79 bytes.extend_from_slice(&value.to_bits().to_be_bytes());
80 }
81 ValueRef::Text(value) | ValueRef::Blob(value) => {
82 bytes.push(3);
83 bytes.extend_from_slice(&(value.len() as u64).to_be_bytes());
84 bytes.extend_from_slice(value);
85 }
86 }
87 }
88 Ok(bytes)
89}
90
91pub(super) fn with_staged_digest(
96 db: &StoreSql,
97 stagings: &[SnapshotStaging],
98 error: DbError,
99) -> DbError {
100 let DbError::Snapshot(SnapshotError::Body { snapshot, error, staged: None }) = error else {
101 return error;
102 };
103 let tables = stagings
104 .iter()
105 .find(|staging| staging.expected == snapshot)
106 .and_then(|staging| staging.tables.as_ref());
107 match tables.map(|tables| digest(db, tables)) {
108 Some(Ok(staged)) => SnapshotError::Body { snapshot, error, staged: Some(staged) }.into(),
109 Some(Err(failure)) => failure,
110 None => SnapshotError::Body { snapshot, error, staged: None }.into(),
111 }
112}
113
114#[cfg(any(test, feature = "test-utils"))]
117#[derive(Clone, Copy, Debug, PartialEq, Eq)]
118pub enum StagedDamage {
119 Record,
122 Writes,
125 Row,
128}
129
130#[cfg(any(test, feature = "test-utils"))]
132pub(super) fn damage(db: &StoreSql, staging: i64, damage: StagedDamage) -> Result<(), DbError> {
133 let changed = match damage {
134 StagedDamage::Record => {
135 let records = table(staging, "records");
136 db.execute(
137 &format!(
138 "UPDATE {records} SET record=substr(record,1,1) WHERE rowid=(SELECT min(rowid) FROM {records})"
139 ),
140 [],
141 )?
142 }
143 StagedDamage::Writes => db.execute(
144 "UPDATE temp._coven_snapshot_writes SET read_through=slot WHERE source=?1",
145 [staging],
146 )?,
147 StagedDamage::Row => {
148 let others: Vec<String> =
149 ["generations", "columns", "parts", "authored", "creations", "records"]
150 .iter()
151 .map(|name| table(staging, name).trim_start_matches("temp.").to_owned())
152 .collect();
153 let rows = db.query(
154 "SELECT name FROM temp.sqlite_schema WHERE type='table' AND name GLOB ?1
155 ORDER BY rootpage",
156 [format!("_coven_staged_{staging}_*")],
157 |row| row.get::<_, String>(0),
158 )?;
159 let mut changed = 0;
160 for name in rows.into_iter().filter(|name| !others.contains(name)) {
161 let name = format!("temp.{}", sqlive::sql::identifier(&name));
162 changed = db.execute(
163 &format!("DELETE FROM {name} WHERE rowid=(SELECT min(rowid) FROM {name})"),
164 [],
165 )?;
166 if changed > 0 {
167 break;
168 }
169 }
170 changed
171 }
172 };
173 match changed {
174 0 => {
175 Err(DbError::Invariant { what: "a test damages a staging that holds what it changes" })
176 }
177 _ => Ok(()),
178 }
179}
180
181#[cfg(any(test, feature = "test-utils"))]
183pub(super) fn removal_failure() -> DbError {
184 DbError::Sqlite(sqlive::rusqlite::Error::SqliteFailure(
185 sqlive::rusqlite::ffi::Error::new(sqlive::rusqlite::ffi::SQLITE_IOERR),
186 Some("injected staging removal failure".into()),
187 ))
188}
189
190impl crate::StoreDatabase {
191 #[cfg(any(test, feature = "test-utils"))]
195 pub async fn fail_next_staging_removal(&self) -> Result<(), DbError> {
196 self.call(|inner| {
197 inner.inputs.fail_removal.store(true, std::sync::atomic::Ordering::SeqCst);
198 Ok::<_, DbError>(())
199 })
200 .await
201 }
202}
203
204impl SnapshotStaging {
205 pub async fn digest(&self) -> Result<StagedDigest, DbError> {
207 self.id()?;
208 let tables = self.tables.as_ref().map(|tables| (tables.id, tables.tables.clone()));
209 let (id, tables) = tables.expect("a staging with an id holds its tables");
210 self.database
211 .call(move |inner| inner.inputs.write(|db, _| digest(db, &StagedTables { id, tables })))
212 .await
213 }
214}