attune-sqlite 0.2.0

SQLite backend for attune.
Documentation
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use std::{
    collections::HashMap,
    path::{Path, PathBuf},
    sync::Mutex,
    thread::{self, JoinHandle},
    time::{Duration, SystemTime, UNIX_EPOCH},
};

use attune_core::{BackendError, StorageBackend, StoredValue};
use crossbeam_channel::{Receiver, RecvTimeoutError, Sender, unbounded};
use rusqlite::{Connection, params};

const POLL_INTERVAL: Duration = Duration::from_millis(1000);

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SqliteOptions {
    pub cross_process: bool,
    pub poll_interval: Duration,
    pub journal_mode: SqliteJournalMode,
    pub busy_timeout: Duration,
    pub synchronous: SqliteSynchronous,
}

impl Default for SqliteOptions {
    fn default() -> Self {
        Self {
            cross_process: true,
            poll_interval: POLL_INTERVAL,
            journal_mode: SqliteJournalMode::Wal,
            busy_timeout: Duration::from_millis(5000),
            synchronous: SqliteSynchronous::Normal,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SqliteJournalMode {
    Wal,
    Delete,
    Truncate,
    Persist,
    Memory,
    Off,
}

impl SqliteJournalMode {
    fn as_pragma(self) -> &'static str {
        match self {
            Self::Wal => "WAL",
            Self::Delete => "DELETE",
            Self::Truncate => "TRUNCATE",
            Self::Persist => "PERSIST",
            Self::Memory => "MEMORY",
            Self::Off => "OFF",
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SqliteSynchronous {
    Off,
    Normal,
    Full,
    Extra,
}

impl SqliteSynchronous {
    fn as_pragma(self) -> &'static str {
        match self {
            Self::Off => "OFF",
            Self::Normal => "NORMAL",
            Self::Full => "FULL",
            Self::Extra => "EXTRA",
        }
    }
}

pub struct SqliteBackend {
    conn: Mutex<Connection>,
    commits_rx: Option<Receiver<()>>,
    shutdown_tx: Option<Sender<()>>,
    poll_thread: Option<JoinHandle<()>>,
}

impl SqliteBackend {
    /// Opens a connection to the SQLite database.
    ///
    /// Uses [`SqliteOptions::default`] for SQLite pragmas and cross-process
    /// change detection.
    ///
    /// ## Errors
    ///
    /// Returns [`BackendError::Open`] when the database cannot be opened,
    /// SQLite pragmas cannot be applied, or the settings table cannot be
    /// created.
    pub fn open(path: impl AsRef<Path>) -> Result<Self, BackendError> {
        Self::open_with_options(path, SqliteOptions::default())
    }

    /// Opens a connection to the SQLite database with explicit backend options.
    ///
    /// Applies the configured SQLite pragmas, creates the settings table, and
    /// starts the cross-process watcher when `options.cross_process` is `true`.
    ///
    /// ## Errors
    ///
    /// Returns [`BackendError::Open`] when the database cannot be opened,
    /// SQLite pragmas cannot be applied, or the settings table cannot be
    /// created.
    pub fn open_with_options(
        path: impl AsRef<Path>,
        options: SqliteOptions,
    ) -> Result<Self, BackendError> {
        let path = path.as_ref().to_path_buf();

        // 1. Create the SQLite connection.
        let conn =
            rusqlite::Connection::open(&path).map_err(|e| BackendError::Open(e.to_string()))?;

        // 2. Set PRAGMAs.
        let pragmas = format!(
            "PRAGMA journal_mode = {};\
            PRAGMA busy_timeout = {};\
            PRAGMA synchronous = {};\
            PRAGMA foreign_keys = ON;",
            options.journal_mode.as_pragma(),
            options.busy_timeout.as_millis(),
            options.synchronous.as_pragma(),
        );
        conn.execute_batch(&pragmas)
            .map_err(|e| BackendError::Open(e.to_string()))?;

        // 3. Create settings table.
        let settings_table_sql = "CREATE TABLE IF NOT EXISTS settings (
            key TEXT PRIMARY KEY NOT NULL,
            value TEXT NOT NULL,
            updated_at INTEGER NOT NULL
        )";
        conn.execute(settings_table_sql, [])
            .map_err(|e| BackendError::Open(e.to_string()))?;

        // 4. Setup optional sidecar thread for cross-process change detection.
        let (commits_rx, shutdown_tx, poll_thread) = if options.cross_process {
            let (commits_tx, commits_rx) = unbounded::<()>();
            let (shutdown_tx, shutdown_rx) = unbounded::<()>();
            let sidecar_path = path.clone();
            let poll_interval = options.poll_interval;
            let poll_thread = thread::spawn(move || {
                polling_loop(sidecar_path, commits_tx, shutdown_rx, poll_interval);
            });

            (Some(commits_rx), Some(shutdown_tx), Some(poll_thread))
        } else {
            (None, None, None)
        };

        Ok(SqliteBackend {
            conn: Mutex::new(conn),
            commits_rx,
            shutdown_tx,
            poll_thread,
        })
    }
}

/// Run the cross-process change-detection loop on a sidecar SQLite connection.
///
/// Opens its own [`Connection`] to `path`, separate from the writer connection,
/// then polls `PRAGMA data_version` every `poll_interval`. Whenever the
/// version counter ticks (signalling that some connection committed to the
/// database), pushes `()` onto `commits_tx`. Own-process commits also tick the
/// counter; the diff loop in `attune-core` is responsible for dedup.
///
/// Returns silently, never panics, and never returns a `Result`. The loop exits
/// when any of these conditions occur:
///
/// - A shutdown value is received on `shutdown_rx` (clean shutdown requested
///   by the owning [`SqliteBackend`] being dropped).
/// - `shutdown_rx` becomes disconnected because its sender was dropped (also
///   indicates the backend has been dropped).
/// - `commits_tx` becomes disconnected because no receiver is listening for
///   change signals.
/// - Opening the sidecar connection fails, or any `PRAGMA data_version` query
///   fails. Cross-process detection degrades gracefully on storage errors
///   rather than propagating.
fn polling_loop(
    path: PathBuf,
    commits_tx: Sender<()>,
    shutdown_rx: Receiver<()>,
    poll_interval: Duration,
) {
    // 1. Open the sidecar connection.
    let sidecar_conn = match Connection::open(&path) {
        Ok(c) => c,
        Err(_) => return,
    };

    // 2. Get the initial data version.
    let mut last_version: i64 =
        match sidecar_conn.query_row("PRAGMA data_version", [], |row| row.get(0)) {
            Ok(v) => v,
            Err(_) => return,
        };

    loop {
        // Wait up to the poll interval for a shutdown signal.
        match shutdown_rx.recv_timeout(poll_interval) {
            Ok(()) => return,                              // Shutdown requested.
            Err(RecvTimeoutError::Disconnected) => return, // Sender dropped. (backend dropped)
            Err(RecvTimeoutError::Timeout) => {}           // Time to poll.
        }

        // Check the current data version.
        let version: i64 = match sidecar_conn.query_row("PRAGMA data_version", [], |row| row.get(0))
        {
            Ok(v) => v,
            Err(_) => return,
        };

        if version != last_version {
            last_version = version;

            // If the send fails, no one is listening. Silently exit.
            if commits_tx.send(()).is_err() {
                return;
            }
        }
    }
}

impl StorageBackend for SqliteBackend {
    fn load_all(&self) -> Result<HashMap<String, StoredValue>, BackendError> {
        // 1. Obtain lock to the DB connection.
        let conn = self.conn.lock().unwrap();

        // 2. Query DB for settings and deserialize values into StoredValue.
        let sql = "SELECT key, value FROM settings";
        let mut stmt = conn
            .prepare(sql)
            .map_err(|e| BackendError::Read(e.to_string()))?;
        let rows = stmt
            .query_map([], |row| {
                let key = row.get(0)?;
                let raw = row.get(1)?;
                Ok((key, StoredValue::from_raw(raw)))
            })
            .map_err(|e| BackendError::Read(e.to_string()))?;

        // 3. Add deserialized values to HashMap.
        let mut result = HashMap::new();
        for row in rows {
            let (k, v) = row.map_err(|e| BackendError::Read(e.to_string()))?;
            result.insert(k, v);
        }

        Ok(result)
    }

    fn set(&self, key: &str, value: &StoredValue) -> Result<(), BackendError> {
        // 1. Obtain lock to the DB connection.
        let conn = self.conn.lock().unwrap();

        // 2. Write setting to DB.
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs() as i64;
        let sql = "INSERT OR REPLACE INTO settings (key, value, updated_at) VALUES (?, ?, ?)";
        conn.execute(sql, params![key, value.as_str(), now])
            .map_err(|e| BackendError::Write(e.to_string()))?;

        Ok(())
    }

    fn delete(&self, key: &str) -> Result<(), BackendError> {
        // 1. Obtain lock to the DB connection.
        let conn = self.conn.lock().unwrap();

        // 2. Delete setting from DB.
        let sql = "DELETE FROM settings WHERE key = ?";
        conn.execute(sql, params![key])
            .map_err(|e| BackendError::Write(e.to_string()))?;

        Ok(())
    }

    fn watch_changes(&self) -> Option<Receiver<()>> {
        self.commits_rx.clone()
    }
}

impl Drop for SqliteBackend {
    fn drop(&mut self) {
        // Signal the polling thread to exit.
        if let Some(shutdown_tx) = &self.shutdown_tx {
            let _ = shutdown_tx.send(());
        }

        // Take the join handle out of Option and move it into `.join()`.
        if let Some(handle) = self.poll_thread.take() {
            let _ = handle.join();
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use rusqlite::OptionalExtension;

    #[test]
    fn test_open_correctly_inits_sqlite_db() {
        let sqlite_be = SqliteBackend::open(":memory:").unwrap();
        let conn = sqlite_be.conn.lock().unwrap();

        // Assert that the settings table is created.
        let mut stmt = conn
            .prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='settings'")
            .unwrap();
        let result: Option<String> = stmt.query_row([], |row| row.get(0)).optional().unwrap();
        assert!(result.is_some());
        let result = result.unwrap();
        assert_eq!(result, "settings");
    }

    #[test]
    fn test_load_all_returns_a_hashmap_with_no_values() {
        let sqlite_be = SqliteBackend::open(":memory:").unwrap();
        let stored_values = sqlite_be.load_all().unwrap();
        assert_eq!(stored_values.len(), 0)
    }

    #[test]
    fn test_set_successfully_writes_a_setting_to_the_db() {
        let key = "theme";
        let sqlite_be = SqliteBackend::open(":memory:").unwrap();
        let sv = StoredValue::encode(&"dark").unwrap();
        sqlite_be.set(&key, &sv).unwrap();

        let stored_values = sqlite_be.load_all().unwrap();
        let loaded = stored_values.get::<str>(&key).unwrap();
        assert_eq!(stored_values.len(), 1);
        assert_eq!(sv.as_str(), loaded.as_str())
    }

    #[test]
    fn test_delete_successfully_removes_a_setting_from_the_db() {
        // 1. Write setting.
        let key = "theme";
        let sqlite_be = SqliteBackend::open(":memory:").unwrap();
        let sv = StoredValue::encode(&"dark").unwrap();
        sqlite_be.set(&key, &sv).unwrap();

        // 2. Ensure setting persists.
        let stored_values = sqlite_be.load_all().unwrap();
        assert_eq!(stored_values.len(), 1);

        // 3. Remove setting and ensure it's no longer in the DB.
        sqlite_be.delete(&key).unwrap();
        let stored_values = sqlite_be.load_all().unwrap();
        assert_eq!(stored_values.len(), 0);
    }

    #[test]
    fn test_watch_changes_signals_on_external_commit() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        let path = tmp.path().to_path_buf();

        // Open the backend
        let backend = SqliteBackend::open(&path).unwrap();
        let rx = backend
            .watch_changes()
            .expect("polling thread should be running");

        // Write through a second backend connection. This simulates an external process.
        let other = rusqlite::Connection::open(&path).unwrap();
        other
            .execute(
                "INSERT INTO settings (key, value, updated_at) VALUES ('theme', '\"dark\"', 0)",
                [],
            )
            .unwrap();

        // Detect the commit through the polling thread.
        match rx.recv_timeout(Duration::from_millis(3000)) {
            Ok(()) => {}
            Err(e) => panic!("expected a change signal within 3s, got {:?}", e),
        }
    }

    #[test]
    fn test_watch_changes_times_out() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        let path = tmp.path().to_path_buf();

        // Open the backend
        let backend = SqliteBackend::open(&path).unwrap();
        let rx = backend
            .watch_changes()
            .expect("polling thread should be running");

        // Detect the commit through the polling thread.
        match rx.recv_timeout(Duration::from_millis(1)) {
            Ok(()) => panic!("did not expect a signal"),
            Err(_e) => {}
        }
    }

    #[test]
    fn test_open_with_options_can_disable_watch_changes() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        let path = tmp.path().to_path_buf();
        let options = SqliteOptions {
            cross_process: false,
            ..SqliteOptions::default()
        };

        let backend = SqliteBackend::open_with_options(&path, options).unwrap();

        assert!(backend.watch_changes().is_none());
    }

    #[test]
    fn test_open_with_options_applies_pragmas() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        let path = tmp.path().to_path_buf();
        let options = SqliteOptions {
            cross_process: false,
            journal_mode: SqliteJournalMode::Delete,
            busy_timeout: Duration::from_millis(1234),
            synchronous: SqliteSynchronous::Full,
            ..SqliteOptions::default()
        };

        let backend = SqliteBackend::open_with_options(&path, options).unwrap();
        let conn = backend.conn.lock().unwrap();

        let journal_mode: String = conn
            .query_row("PRAGMA journal_mode", [], |row| row.get(0))
            .unwrap();
        let busy_timeout: i64 = conn
            .query_row("PRAGMA busy_timeout", [], |row| row.get(0))
            .unwrap();
        let synchronous: i64 = conn
            .query_row("PRAGMA synchronous", [], |row| row.get(0))
            .unwrap();

        assert_eq!(journal_mode, "delete");
        assert_eq!(busy_timeout, 1234);
        assert_eq!(synchronous, 2);
    }
}