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 mut stmt = self.conn.prepare_cached(
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 let revisions = stmt.query_row([], |r| Ok([r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?]))?;
44 Ok(ListRevision(if filter.recordings {
45 [0, 0, 0, revisions[3]]
46 } else {
47 [
48 revisions[0],
49 revisions[1],
50 if filter.history { revisions[2] } else { 0 },
51 0,
52 ]
53 }))
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn version_six_migration_tracks_external_writes_and_rolled_back_changes() {
63 let dir = tempfile::tempdir().unwrap();
64 let path = dir.path().join("radio.db");
65 let store = Store::open(&path).unwrap();
66 for table in ["stations", "favorites", "history", "recordings"] {
67 for operation in ["INSERT", "UPDATE", "DELETE"] {
68 store
69 .conn
70 .execute_batch(&format!("DROP TRIGGER revision_{table}_{operation}"))
71 .unwrap();
72 }
73 }
74 crate::test_support::strip_podcast_episode_order(&store.conn);
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}