kanban_persistence_sqlite/
sqlite_backend.rs1use crate::SqliteStore;
2use async_trait::async_trait;
3use chrono::{DateTime, Utc};
4use kanban_backend_memory::InMemoryStore;
5use kanban_domain::command_batch::CommandBatch;
6use kanban_domain::command_store::CommandStore;
7use kanban_domain::data_store::DataStore;
8use kanban_domain::{
9 ArchivedBoard, ArchivedCard, Board, Card, Column, DependencyGraph, GraphMutFn, KanbanError,
10 KanbanResult, Snapshot, Sprint,
11};
12use kanban_persistence::{PersistenceMetadata, PersistenceStore};
13use uuid::Uuid;
14
15pub struct SqliteBackend {
16 db: SqliteStore,
17 mem: InMemoryStore,
20 last_metadata: std::sync::RwLock<Option<PersistenceMetadata>>,
26}
27
28impl SqliteBackend {
29 pub async fn open(locator: &str) -> KanbanResult<Self> {
30 let db = SqliteStore::open(locator).await?;
31 let initial = db.read_metadata_sync()?;
32 Ok(Self {
33 db,
34 mem: InMemoryStore::new(),
35 last_metadata: std::sync::RwLock::new(initial),
36 })
37 }
38}
39
40impl DataStore for SqliteBackend {
43 fn get_board(&self, id: Uuid) -> KanbanResult<Option<Board>> {
44 self.db.get_board(id)
45 }
46 fn list_boards(&self) -> KanbanResult<Vec<Board>> {
47 self.db.list_boards()
48 }
49 fn upsert_board(&self, board: Board) -> KanbanResult<()> {
50 self.db.upsert_board(board)
51 }
52 fn delete_board(&self, id: Uuid) -> KanbanResult<()> {
53 self.db.delete_board(id)
54 }
55
56 fn get_column(&self, id: Uuid) -> KanbanResult<Option<Column>> {
57 self.db.get_column(id)
58 }
59 fn list_columns_by_board(&self, board_id: Uuid) -> KanbanResult<Vec<Column>> {
60 self.db.list_columns_by_board(board_id)
61 }
62 fn list_all_columns(&self) -> KanbanResult<Vec<Column>> {
63 self.db.list_all_columns()
64 }
65 fn upsert_column(&self, column: Column) -> KanbanResult<()> {
66 self.db.upsert_column(column)
67 }
68 fn delete_column(&self, id: Uuid) -> KanbanResult<()> {
69 self.db.delete_column(id)
70 }
71 fn delete_columns_by_board(&self, board_id: Uuid) -> KanbanResult<()> {
72 self.db.delete_columns_by_board(board_id)
73 }
74
75 fn get_card(&self, id: Uuid) -> KanbanResult<Option<Card>> {
76 self.db.get_card(id)
77 }
78 fn list_all_cards(&self) -> KanbanResult<Vec<Card>> {
79 self.db.list_all_cards()
80 }
81 fn list_cards_by_column(&self, column_id: Uuid) -> KanbanResult<Vec<Card>> {
82 self.db.list_cards_by_column(column_id)
83 }
84 fn list_cards_by_columns(&self, column_ids: &[Uuid]) -> KanbanResult<Vec<Card>> {
85 self.db.list_cards_by_columns(column_ids)
86 }
87 fn list_cards_by_sprint(&self, sprint_id: Uuid) -> KanbanResult<Vec<Card>> {
88 self.db.list_cards_by_sprint(sprint_id)
89 }
90 fn count_cards_in_column(&self, column_id: Uuid) -> KanbanResult<usize> {
91 self.db.count_cards_in_column(column_id)
92 }
93 fn count_cards_in_column_excluding(
94 &self,
95 column_id: Uuid,
96 exclude: &[Uuid],
97 ) -> KanbanResult<usize> {
98 self.db.count_cards_in_column_excluding(column_id, exclude)
99 }
100 fn list_cards_by_column_filtered(
101 &self,
102 column_id: Uuid,
103 archived: kanban_domain::ArchivedFilter,
104 ) -> KanbanResult<Vec<Card>> {
105 self.db.list_cards_by_column_filtered(column_id, archived)
106 }
107 fn count_cards_in_column_filtered(
108 &self,
109 column_id: Uuid,
110 archived: kanban_domain::ArchivedFilter,
111 ) -> KanbanResult<usize> {
112 self.db.count_cards_in_column_filtered(column_id, archived)
113 }
114 fn upsert_card(&self, card: Card) -> KanbanResult<()> {
115 self.db.upsert_card(card)
116 }
117 fn delete_card(&self, id: Uuid) -> KanbanResult<()> {
118 self.db.delete_card(id)
119 }
120 fn delete_cards_by_columns(&self, column_ids: &[Uuid]) -> KanbanResult<()> {
121 self.db.delete_cards_by_columns(column_ids)
122 }
123 fn clear_sprint_from_cards(
124 &self,
125 sprint_id: Uuid,
126 timestamp: DateTime<Utc>,
127 ) -> KanbanResult<()> {
128 self.db.clear_sprint_from_cards(sprint_id, timestamp)
129 }
130
131 fn get_archived_card(&self, card_id: Uuid) -> KanbanResult<Option<ArchivedCard>> {
132 self.db.get_archived_card(card_id)
133 }
134 fn list_archived_cards(&self) -> KanbanResult<Vec<ArchivedCard>> {
135 self.db.list_archived_cards()
136 }
137 fn insert_archived_card(&self, ac: ArchivedCard) -> KanbanResult<()> {
138 self.db.insert_archived_card(ac)
139 }
140 fn delete_archived_card(&self, card_id: Uuid) -> KanbanResult<()> {
141 self.db.delete_archived_card(card_id)
142 }
143 fn get_archived_board(&self, board_id: Uuid) -> KanbanResult<Option<ArchivedBoard>> {
144 self.db.get_archived_board(board_id)
145 }
146 fn list_archived_boards(&self) -> KanbanResult<Vec<ArchivedBoard>> {
147 self.db.list_archived_boards()
148 }
149 fn insert_archived_board(&self, ab: ArchivedBoard) -> KanbanResult<()> {
150 self.db.insert_archived_board(ab)
151 }
152 fn delete_archived_board(&self, board_id: Uuid) -> KanbanResult<()> {
153 self.db.delete_archived_board(board_id)
154 }
155 fn unarchive_board(&self, board_id: Uuid) -> KanbanResult<()> {
156 self.db.unarchive_board(board_id)
157 }
158 fn list_archived_cards_by_board(&self, board_id: Uuid) -> KanbanResult<Vec<ArchivedCard>> {
159 self.db.list_archived_cards_by_board(board_id)
160 }
161 fn clear_sprint_from_archived_cards(
162 &self,
163 sprint_id: Uuid,
164 timestamp: DateTime<Utc>,
165 ) -> KanbanResult<()> {
166 self.db
167 .clear_sprint_from_archived_cards(sprint_id, timestamp)
168 }
169
170 fn get_sprint(&self, id: Uuid) -> KanbanResult<Option<Sprint>> {
171 self.db.get_sprint(id)
172 }
173 fn list_sprints_by_board(&self, board_id: Uuid) -> KanbanResult<Vec<Sprint>> {
174 self.db.list_sprints_by_board(board_id)
175 }
176 fn list_all_sprints(&self) -> KanbanResult<Vec<Sprint>> {
177 self.db.list_all_sprints()
178 }
179 fn upsert_sprint(&self, sprint: Sprint) -> KanbanResult<()> {
180 self.db.upsert_sprint(sprint)
181 }
182 fn delete_sprint(&self, id: Uuid) -> KanbanResult<()> {
183 self.db.delete_sprint(id)
184 }
185 fn delete_sprints_by_board(&self, board_id: Uuid) -> KanbanResult<()> {
186 self.db.delete_sprints_by_board(board_id)
187 }
188
189 fn get_graph(&self) -> KanbanResult<DependencyGraph> {
190 self.db.get_graph()
191 }
192 fn set_graph(&self, graph: DependencyGraph) -> KanbanResult<()> {
193 self.db.set_graph(graph)
194 }
195 fn modify_graph(&self, f: GraphMutFn) -> KanbanResult<()> {
196 self.db.modify_graph(f)
197 }
198
199 fn snapshot(&self) -> KanbanResult<Snapshot> {
200 self.db.snapshot()
201 }
202 fn apply_snapshot(&self, snapshot: Snapshot) -> KanbanResult<()> {
203 self.db.apply_snapshot(snapshot)
204 }
205}
206
207impl CommandStore for SqliteBackend {
212 fn append_batch(&self, batch: &CommandBatch) -> KanbanResult<u64> {
213 self.mem.append_batch(batch)
214 }
215 fn batch_count(&self) -> KanbanResult<u64> {
216 self.mem.batch_count()
217 }
218 fn load_batches(&self, from: u64, to: u64) -> KanbanResult<Vec<CommandBatch>> {
219 self.mem.load_batches(from, to)
220 }
221 fn load_all_batches(&self) -> KanbanResult<(Vec<CommandBatch>, u64)> {
225 self.mem.load_all_batches()
226 }
227}
228
229#[async_trait]
232impl kanban_backend::KanbanBackend for SqliteBackend {
233 fn as_data_store(&self) -> &dyn DataStore {
234 self
235 }
236
237 async fn flush(&self) -> KanbanResult<()> {
238 self.db.stamp_writer().await?;
242 self.db.checkpoint().await?;
243 let fresh = self.db.read_metadata_sync()?;
248 let mut guard = self.last_metadata.write().map_err(|_| {
249 KanbanError::Internal("sqlite_backend: last_metadata RwLock poisoned".into())
250 })?;
251 *guard = fresh;
252 Ok(())
253 }
254
255 fn instance_id(&self) -> Uuid {
256 <SqliteStore as PersistenceStore>::instance_id(&self.db)
257 }
258
259 fn local_persistence(&self) -> Option<&dyn kanban_backend::LocalPersistence> {
260 Some(self)
261 }
262}
263
264impl kanban_backend::LocalPersistence for SqliteBackend {
265 fn persistence_metadata(&self) -> Option<PersistenceMetadata> {
266 self.last_metadata.read().ok().and_then(|g| g.clone())
267 }
268}
269
270#[cfg(test)]
271mod tests {
272 use kanban_domain::{Board, DataStore};
273
274 use super::SqliteBackend;
275 use kanban_backend::KanbanBackend;
276
277 #[tokio::test(flavor = "multi_thread")]
278 async fn test_sqlite_backend_needs_flush_returns_false() {
279 let dir = tempfile::tempdir().unwrap();
280 let path = dir.path().join("t.sqlite3");
281 let backend = SqliteBackend::open(path.to_str().unwrap()).await.unwrap();
282 assert!(!backend.needs_flush());
283 }
284
285 #[tokio::test(flavor = "multi_thread")]
286 async fn test_sqlite_backend_flush_executes_wal_checkpoint() {
287 let dir = tempfile::tempdir().unwrap();
288 let path = dir.path().join("t.sqlite3");
289 let backend = SqliteBackend::open(path.to_str().unwrap()).await.unwrap();
290 backend
291 .upsert_board(Board::new("B", None::<String>))
292 .unwrap();
293 backend
294 .flush()
295 .await
296 .expect("WAL checkpoint should not error");
297 }
298
299 #[tokio::test(flavor = "multi_thread")]
300 async fn test_sqlite_backend_exposes_local_persistence() {
301 use super::SqliteBackend;
302 use kanban_backend::KanbanBackend;
303
304 let dir = tempfile::tempdir().unwrap();
305 let path = dir.path().join("md.sqlite3");
306 let backend = SqliteBackend::open(path.to_str().unwrap()).await.unwrap();
307 backend.flush().await.unwrap();
308 let meta = backend
309 .local_persistence()
310 .expect("sqlite backend must expose a LocalPersistence capability")
311 .persistence_metadata()
312 .expect("flushed sqlite backend must expose metadata");
313 assert_eq!(
314 meta.writer_version.as_deref(),
315 Some(kanban_core::KANBAN_VERSION),
316 "flushed sqlite backend must stamp writer_version"
317 );
318 assert_eq!(
319 meta.writer_commit.as_deref(),
320 Some(kanban_core::KANBAN_COMMIT),
321 );
322 }
323
324 #[tokio::test(flavor = "multi_thread")]
325 async fn test_sqlite_backend_reload_is_noop() {
326 let dir = tempfile::tempdir().unwrap();
327 let path = dir.path().join("t.sqlite3");
328 let backend = SqliteBackend::open(path.to_str().unwrap()).await.unwrap();
329 backend
330 .upsert_board(Board::new("A", None::<String>))
331 .unwrap();
332 backend.reload().await.unwrap();
333 let boards = backend.list_boards().unwrap();
334 assert_eq!(boards.len(), 1);
335 assert_eq!(boards[0].name, "A");
336 }
337}