| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
use rusqlite::{Connection, OptionalExtension}; |
| 27 |
use serde_json::Value; |
| 28 |
|
| 29 |
use crate::error::Result; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
pub(crate) const SNAPSHOT_DDL: &str = "\ |
| 41 |
CREATE TABLE IF NOT EXISTS sync_row_snapshot ( |
| 42 |
table_name TEXT NOT NULL, |
| 43 |
row_id TEXT NOT NULL, |
| 44 |
payload TEXT NOT NULL, |
| 45 |
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')), |
| 46 |
PRIMARY KEY (table_name, row_id) |
| 47 |
) WITHOUT ROWID; |
| 48 |
"; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
pub(crate) fn record(conn: &Connection, table: &str, row_id: &str, payload: &Value) -> Result<()> { |
| 56 |
if !payload.is_object() { |
| 57 |
return Ok(()); |
| 58 |
} |
| 59 |
conn.execute( |
| 60 |
"INSERT INTO sync_row_snapshot (table_name, row_id, payload) VALUES (?1, ?2, ?3) \ |
| 61 |
ON CONFLICT(table_name, row_id) DO UPDATE SET \ |
| 62 |
payload = excluded.payload, \ |
| 63 |
updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')", |
| 64 |
rusqlite::params![table, row_id, serde_json::to_string(payload)?], |
| 65 |
)?; |
| 66 |
Ok(()) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub(crate) fn forget(conn: &Connection, table: &str, row_id: &str) -> Result<()> { |
| 74 |
conn.execute( |
| 75 |
"DELETE FROM sync_row_snapshot WHERE table_name = ?1 AND row_id = ?2", |
| 76 |
rusqlite::params![table, row_id], |
| 77 |
)?; |
| 78 |
Ok(()) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
pub(crate) fn load(conn: &Connection, table: &str, row_id: &str) -> Value { |
| 89 |
let stored: Option<String> = match conn |
| 90 |
.query_row( |
| 91 |
"SELECT payload FROM sync_row_snapshot WHERE table_name = ?1 AND row_id = ?2", |
| 92 |
rusqlite::params![table, row_id], |
| 93 |
|r| r.get(0), |
| 94 |
) |
| 95 |
.optional() |
| 96 |
{ |
| 97 |
Ok(v) => v, |
| 98 |
Err(e) => { |
| 99 |
tracing::warn!( |
| 100 |
table, |
| 101 |
row_id, |
| 102 |
"snapshot lookup failed, treating as absent: {e}" |
| 103 |
); |
| 104 |
None |
| 105 |
} |
| 106 |
}; |
| 107 |
stored |
| 108 |
.and_then(|s| serde_json::from_str(&s).ok()) |
| 109 |
.unwrap_or(Value::Null) |
| 110 |
} |
| 111 |
|
| 112 |
#[cfg(test)] |
| 113 |
mod tests { |
| 114 |
use super::*; |
| 115 |
use serde_json::json; |
| 116 |
|
| 117 |
fn conn() -> Connection { |
| 118 |
let conn = Connection::open_in_memory().unwrap(); |
| 119 |
conn.execute_batch(SNAPSHOT_DDL).unwrap(); |
| 120 |
conn |
| 121 |
} |
| 122 |
|
| 123 |
#[test] |
| 124 |
fn round_trips_and_overwrites() { |
| 125 |
let c = conn(); |
| 126 |
record(&c, "note", "r1", &json!({"name": "one"})).unwrap(); |
| 127 |
assert_eq!(load(&c, "note", "r1"), json!({"name": "one"})); |
| 128 |
|
| 129 |
record(&c, "note", "r1", &json!({"name": "two"})).unwrap(); |
| 130 |
assert_eq!( |
| 131 |
load(&c, "note", "r1"), |
| 132 |
json!({"name": "two"}), |
| 133 |
"a later sync must re-base the row, not accumulate versions" |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
fn absent_and_forgotten_rows_read_as_null() { |
| 139 |
let c = conn(); |
| 140 |
assert_eq!(load(&c, "note", "missing"), Value::Null); |
| 141 |
|
| 142 |
record(&c, "note", "r1", &json!({"name": "one"})).unwrap(); |
| 143 |
forget(&c, "note", "r1").unwrap(); |
| 144 |
assert_eq!( |
| 145 |
load(&c, "note", "r1"), |
| 146 |
Value::Null, |
| 147 |
"a deleted row's base must not survive to be merged against" |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
#[test] |
| 155 |
fn a_non_object_payload_is_not_stored() { |
| 156 |
let c = conn(); |
| 157 |
record(&c, "note", "r1", &json!("not an object")).unwrap(); |
| 158 |
assert_eq!(load(&c, "note", "r1"), Value::Null); |
| 159 |
} |
| 160 |
} |
| 161 |
|