Skip to main content

kanban_persistence_sqlite/sqlite_store/
persistence_store.rs

1use chrono::{DateTime, Utc};
2use kanban_domain::Snapshot;
3use kanban_persistence::{
4    PersistenceError, PersistenceMetadata, PersistenceResult, PersistenceStore, StoreSnapshot,
5};
6use uuid::Uuid;
7
8use super::{SqliteStore, SUPPORTED_SCHEMA_VERSION};
9
10#[async_trait::async_trait]
11impl PersistenceStore for SqliteStore {
12    async fn save(&self, snapshot: StoreSnapshot) -> PersistenceResult<PersistenceMetadata> {
13        let domain_snapshot: Snapshot = serde_json::from_slice(&snapshot.data)
14            .map_err(|e| PersistenceError::Serialization(e.to_string()))?;
15        self.apply_snapshot_async(domain_snapshot)
16            .await
17            .map_err(|e| PersistenceError::Database(e.to_string()))?;
18
19        let saved_at = self
20            .stamp_writer()
21            .await
22            .map_err(|e| PersistenceError::Database(e.to_string()))?;
23        self.checkpoint()
24            .await
25            .map_err(|e| PersistenceError::Database(e.to_string()))?;
26
27        // Values are all knowable inline — stamp_writer just wrote them and
28        // returned the timestamp, the writer/commit are compile-time consts.
29        // format_version is SUPPORTED because migrate() on open() normalised
30        // schema_version to SUPPORTED and nothing in the save path touches
31        // it. The read paths (load, read_metadata_sync) re-read from the row
32        // to honour the "DB is the source of truth" contract.
33        Ok(PersistenceMetadata {
34            instance_id: self.instance_id,
35            saved_at,
36            writer_version: Some(kanban_core::KANBAN_VERSION.to_string()),
37            writer_commit: Some(kanban_core::KANBAN_COMMIT.to_string()),
38            format_version: Some(SUPPORTED_SCHEMA_VERSION),
39        })
40    }
41
42    async fn load(&self) -> PersistenceResult<(StoreSnapshot, PersistenceMetadata)> {
43        let domain_snapshot = self
44            .snapshot_async()
45            .await
46            .map_err(|e| PersistenceError::Database(e.to_string()))?;
47        let data = serde_json::to_vec(&domain_snapshot)
48            .map_err(|e| PersistenceError::Serialization(e.to_string()))?;
49
50        let row: (String, Option<String>, Option<String>, u32) = sqlx::query_as(
51            "SELECT saved_at, writer_version, writer_commit, schema_version \
52             FROM metadata WHERE id = 1",
53        )
54        .fetch_one(&self.pool)
55        .await
56        .map_err(|e| PersistenceError::Database(e.to_string()))?;
57        let saved_at = DateTime::parse_from_rfc3339(&row.0)
58            .map_err(|e| PersistenceError::Serialization(e.to_string()))?
59            .with_timezone(&Utc);
60        let meta = PersistenceMetadata {
61            instance_id: self.instance_id,
62            saved_at,
63            writer_version: row.1,
64            writer_commit: row.2,
65            format_version: Some(row.3),
66        };
67        Ok((
68            StoreSnapshot {
69                data,
70                metadata: meta.clone(),
71            },
72            meta,
73        ))
74    }
75
76    async fn exists(&self) -> bool {
77        self.path.exists()
78    }
79
80    fn path(&self) -> &std::path::Path {
81        &self.path
82    }
83
84    fn instance_id(&self) -> Uuid {
85        self.instance_id
86    }
87
88    async fn close(&self) {
89        self.pool.close().await;
90    }
91}