| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
use rusqlite::Connection; |
| 30 |
use synckit_config::ConfigSpec; |
| 31 |
|
| 32 |
use super::schema::SyncTable; |
| 33 |
use crate::error::Result; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
const POLICY_TABLE: &str = "config_key_policy"; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
const POLICY_FILTER: &str = "EXISTS (SELECT 1 FROM config_key_policy p \ |
| 47 |
WHERE p.key = {row}.key AND p.replicated = 1)"; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
pub fn config_sync_table(spec: &ConfigSpec) -> SyncTable { |
| 56 |
SyncTable::full(spec.table(), &["key", "value"]) |
| 57 |
.pk(&["key"]) |
| 58 |
.exclude_where(POLICY_FILTER) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
pub fn install_policy(conn: &mut Connection, spec: &ConfigSpec) -> Result<()> { |
| 72 |
let tx = conn.transaction()?; |
| 73 |
tx.execute_batch(&format!( |
| 74 |
"CREATE TABLE IF NOT EXISTS {POLICY_TABLE} (\ |
| 75 |
key TEXT PRIMARY KEY,\ |
| 76 |
replicated INTEGER NOT NULL\ |
| 77 |
) WITHOUT ROWID;" |
| 78 |
))?; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
tx.execute(&format!("DELETE FROM {POLICY_TABLE}"), [])?; |
| 83 |
{ |
| 84 |
let mut stmt = tx.prepare(&format!( |
| 85 |
"INSERT INTO {POLICY_TABLE} (key, replicated) VALUES (?1, ?2)" |
| 86 |
))?; |
| 87 |
for row in spec.policy_rows() { |
| 88 |
stmt.execute(rusqlite::params![row.key, i64::from(row.replicated)])?; |
| 89 |
} |
| 90 |
} |
| 91 |
tx.commit()?; |
| 92 |
Ok(()) |
| 93 |
} |
| 94 |
|
| 95 |
#[cfg(test)] |
| 96 |
mod tests { |
| 97 |
use super::*; |
| 98 |
use crate::store::schema::SyncSchema; |
| 99 |
use synckit_config::{ConfigStore, Posture}; |
| 100 |
|
| 101 |
const SPEC: ConfigSpec = ConfigSpec::new( |
| 102 |
"app_config", |
| 103 |
&[ |
| 104 |
("theme", Posture::Synced), |
| 105 |
("sidebar_visible", Posture::Synced), |
| 106 |
("mirror_path", Posture::Local), |
| 107 |
], |
| 108 |
); |
| 109 |
|
| 110 |
|
| 111 |
fn captured(conn: &Connection) -> Vec<String> { |
| 112 |
let mut stmt = conn |
| 113 |
.prepare("SELECT row_id FROM sync_changelog ORDER BY id") |
| 114 |
.unwrap(); |
| 115 |
stmt.query_map([], |r| r.get::<_, String>(0)) |
| 116 |
.unwrap() |
| 117 |
.map(std::result::Result::unwrap) |
| 118 |
.collect() |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
fn wired() -> (Connection, ConfigStore) { |
| 127 |
let mut conn = Connection::open_in_memory().unwrap(); |
| 128 |
let store = ConfigStore::open(&conn, &SPEC).unwrap(); |
| 129 |
install_policy(&mut conn, &SPEC).unwrap(); |
| 130 |
let sql = SyncSchema::new(vec![config_sync_table(&SPEC)]).migration_sql(); |
| 131 |
conn.execute_batch(&sql).unwrap(); |
| 132 |
(conn, store) |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
#[test] |
| 138 |
fn a_synced_key_is_captured_and_a_local_key_is_not() { |
| 139 |
let (conn, store) = wired(); |
| 140 |
store.set(&conn, "theme", "akari-night").unwrap(); |
| 141 |
store.set(&conn, "mirror_path", "/mnt/samples").unwrap(); |
| 142 |
|
| 143 |
assert_eq!( |
| 144 |
captured(&conn), |
| 145 |
["theme"], |
| 146 |
"the preference syncs; the local path never enters the changelog", |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
#[test] |
| 154 |
fn an_undeclared_key_is_never_captured() { |
| 155 |
let (conn, store) = wired(); |
| 156 |
store |
| 157 |
.set(&conn, "some_new_local_path", "/etc/secret") |
| 158 |
.unwrap(); |
| 159 |
assert!( |
| 160 |
captured(&conn).is_empty(), |
| 161 |
"an unclassified key has no policy row and cannot be admitted", |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn every_synced_key_captures() { |
| 167 |
let (conn, store) = wired(); |
| 168 |
store.set(&conn, "theme", "flatwhite").unwrap(); |
| 169 |
store.set(&conn, "sidebar_visible", "true").unwrap(); |
| 170 |
assert_eq!(captured(&conn), ["theme", "sidebar_visible"]); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
#[test] |
| 176 |
fn reseeding_a_narrower_spec_stops_the_dropped_key_syncing() { |
| 177 |
let (mut conn, store) = wired(); |
| 178 |
store.set(&conn, "theme", "flatwhite").unwrap(); |
| 179 |
assert_eq!(captured(&conn), ["theme"]); |
| 180 |
|
| 181 |
const NARROWER: ConfigSpec = |
| 182 |
ConfigSpec::new("app_config", &[("mirror_path", Posture::Local)]); |
| 183 |
install_policy(&mut conn, &NARROWER).unwrap(); |
| 184 |
|
| 185 |
conn.execute("DELETE FROM sync_changelog", []).unwrap(); |
| 186 |
store.set(&conn, "theme", "akari-night").unwrap(); |
| 187 |
assert!( |
| 188 |
captured(&conn).is_empty(), |
| 189 |
"theme is no longer in the policy, so it no longer syncs", |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
#[test] |
| 194 |
fn install_policy_is_idempotent() { |
| 195 |
let (mut conn, _store) = wired(); |
| 196 |
|
| 197 |
install_policy(&mut conn, &SPEC).unwrap(); |
| 198 |
let count: i64 = conn |
| 199 |
.query_row("SELECT COUNT(*) FROM config_key_policy", [], |r| r.get(0)) |
| 200 |
.unwrap(); |
| 201 |
assert_eq!(count, 3, "one row per declared key, not doubled"); |
| 202 |
} |
| 203 |
} |
| 204 |
|