radio_cli/store/
revisions.rs1use super::*;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub struct ListRevision([u64; 4]);
6
7pub(super) fn migrate(conn: &Connection) -> Result<()> {
8 conn.execute_batch(
9 "CREATE TABLE IF NOT EXISTS content_revisions (
10 name TEXT PRIMARY KEY,n INTEGER NOT NULL DEFAULT 0) WITHOUT ROWID;",
11 )?;
12 for table in ["stations", "favorites", "history", "recordings"] {
13 conn.execute(
14 "INSERT OR IGNORE INTO content_revisions(name) VALUES (?1)",
15 [table],
16 )?;
17 for operation in ["INSERT", "UPDATE", "DELETE"] {
18 conn.execute_batch(&format!(
21 "CREATE TRIGGER IF NOT EXISTS revision_{table}_{operation}
22 AFTER {operation} ON {table} BEGIN
23 UPDATE content_revisions SET n=n+1 WHERE name='{table}'; END;"
24 ))?;
25 }
26 }
27 Ok(())
28}
29
30impl Store {
31 pub fn list_revision(&self, filter: &Filter) -> Result<ListRevision> {
36 let revisions = self.conn.query_row(
37 "SELECT
38 (SELECT n FROM content_revisions WHERE name='stations'),
39 (SELECT n FROM content_revisions WHERE name='favorites'),
40 (SELECT n FROM content_revisions WHERE name='history'),
41 (SELECT n FROM content_revisions WHERE name='recordings')",
42 [],
43 |r| Ok([r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?]),
44 )?;
45 Ok(ListRevision(if filter.recordings {
46 [0, 0, 0, revisions[3]]
47 } else {
48 [
49 revisions[0],
50 revisions[1],
51 if filter.history { revisions[2] } else { 0 },
52 0,
53 ]
54 }))
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn version_six_migration_tracks_external_writes_and_rolled_back_changes() {
64 let dir = tempfile::tempdir().unwrap();
65 let path = dir.path().join("radio.db");
66 let store = Store::open(&path).unwrap();
67 for table in ["stations", "favorites", "history", "recordings"] {
68 for operation in ["INSERT", "UPDATE", "DELETE"] {
69 store
70 .conn
71 .execute_batch(&format!("DROP TRIGGER revision_{table}_{operation}"))
72 .unwrap();
73 }
74 }
75 store
76 .conn
77 .execute_batch("DROP TABLE content_revisions; PRAGMA user_version=6")
78 .unwrap();
79 drop(store);
80 let store = Store::open(&path).unwrap();
81 let station = Station {
82 stationuuid: uuid::Uuid::new_v4().to_string(),
83 name: "existing".into(),
84 active: true,
85 ..Station::default()
86 };
87 store.upsert(&station).unwrap();
88 let filter = Filter::default();
89 let before = store.list_revision(&filter).unwrap();
90 let external = Connection::open(&path).unwrap();
91 external.execute_batch("BEGIN").unwrap();
92 external
93 .execute(
94 "INSERT INTO favorites VALUES (?1,'now')",
95 [&station.stationuuid],
96 )
97 .unwrap();
98 external.execute_batch("ROLLBACK").unwrap();
99 assert_eq!(store.list_revision(&filter).unwrap(), before);
100 external
101 .execute(
102 "INSERT INTO favorites VALUES (?1,'now')",
103 [&station.stationuuid],
104 )
105 .unwrap();
106 assert_ne!(store.list_revision(&filter).unwrap(), before);
107 let after = store.list_revision(&filter).unwrap();
108 external
109 .execute("INSERT INTO meta VALUES ('pref_volume','60')", [])
110 .unwrap();
111 assert_eq!(store.list_revision(&filter).unwrap(), after);
112 drop(store);
113 assert_eq!(
114 Store::open(&path).unwrap().list_revision(&filter).unwrap(),
115 after
116 );
117 }
118}