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 crate::test_support::strip_podcast_episode_order(&store.conn);
76 store
77 .conn
78 .execute_batch("DROP TABLE content_revisions; PRAGMA user_version=6")
79 .unwrap();
80 drop(store);
81 let store = Store::open(&path).unwrap();
82 let station = Station {
83 stationuuid: uuid::Uuid::new_v4().to_string(),
84 name: "existing".into(),
85 active: true,
86 ..Station::default()
87 };
88 store.upsert(&station).unwrap();
89 let filter = Filter::default();
90 let before = store.list_revision(&filter).unwrap();
91 let external = Connection::open(&path).unwrap();
92 external.execute_batch("BEGIN").unwrap();
93 external
94 .execute(
95 "INSERT INTO favorites VALUES (?1,'now')",
96 [&station.stationuuid],
97 )
98 .unwrap();
99 external.execute_batch("ROLLBACK").unwrap();
100 assert_eq!(store.list_revision(&filter).unwrap(), before);
101 external
102 .execute(
103 "INSERT INTO favorites VALUES (?1,'now')",
104 [&station.stationuuid],
105 )
106 .unwrap();
107 assert_ne!(store.list_revision(&filter).unwrap(), before);
108 let after = store.list_revision(&filter).unwrap();
109 external
110 .execute("INSERT INTO meta VALUES ('pref_volume','60')", [])
111 .unwrap();
112 assert_eq!(store.list_revision(&filter).unwrap(), after);
113 drop(store);
114 assert_eq!(
115 Store::open(&path).unwrap().list_revision(&filter).unwrap(),
116 after
117 );
118 }
119}