1pub mod compatibility;
2pub mod factory;
3pub mod local_persistence;
4pub mod remote_writes;
5pub use compatibility::CompatibilityNotice;
6pub use factory::{KanbanBackendFactory, KanbanBackendRegistry};
7pub use local_persistence::LocalPersistence;
8pub use remote_writes::{
9 RemoteBatchOutcome, RemoteBatchWrites, RemoteBoardWrites, RemoteCardWrites, RemoteGraphWrites,
10 RemoteSprintWrites, RemoteWrites,
11};
12
13use async_trait::async_trait;
14use kanban_domain::command_store::CommandStore;
15use kanban_domain::data_store::DataStore;
16use kanban_domain::{KanbanError, KanbanResult};
17use uuid::Uuid;
18
19#[async_trait]
36pub trait KanbanBackend: DataStore + CommandStore + Send + Sync {
37 fn as_data_store(&self) -> &dyn DataStore;
39
40 async fn flush(&self) -> KanbanResult<()> {
42 Ok(())
43 }
44
45 async fn reload(&self) -> KanbanResult<()> {
47 Ok(())
48 }
49
50 async fn probe(&self) -> KanbanResult<()> {
55 self.batch_count().map(|_| ())
56 }
57
58 fn mark_dirty(&self) {}
63
64 fn needs_flush(&self) -> bool {
66 false
67 }
68
69 fn needs_save_worker(&self) -> bool {
72 false
73 }
74
75 fn instance_id(&self) -> Uuid {
77 Uuid::nil()
78 }
79
80 fn local_persistence(&self) -> Option<&dyn crate::LocalPersistence> {
85 None
86 }
87
88 fn compatibility_notice(&self) -> Option<crate::CompatibilityNotice> {
91 None
92 }
93
94 fn health_checker(&self) -> Option<Box<dyn kanban_core::HealthChecker>> {
98 None
99 }
100
101 fn remote_writes(&self) -> Option<&dyn crate::RemoteWrites> {
106 None
107 }
108
109 fn remote_board_writes(&self) -> Option<&dyn crate::RemoteBoardWrites> {
112 None
113 }
114
115 fn remote_card_writes(&self) -> Option<&dyn crate::RemoteCardWrites> {
117 None
118 }
119
120 fn remote_batch_writes(&self) -> Option<&dyn crate::RemoteBatchWrites> {
122 None
123 }
124
125 fn remote_sprint_writes(&self) -> Option<&dyn crate::RemoteSprintWrites> {
127 None
128 }
129
130 fn remote_graph_writes(&self) -> Option<&dyn crate::RemoteGraphWrites> {
132 None
133 }
134
135 fn as_any(&self) -> Option<&dyn std::any::Any> {
138 None
139 }
140
141 fn with_transaction(&self, f: TransactionFn<'_>) -> KanbanResult<()>;
172}
173
174pub type TransactionFn<'f> = Box<dyn FnOnce() -> KanbanResult<()> + 'f>;
178
179pub fn rollback_failed(batch_err: KanbanError, rollback_err: KanbanError) -> KanbanError {
183 KanbanError::Internal(format!(
184 "Batch failed ({batch_err}) and rollback also failed ({rollback_err}). State may be inconsistent."
185 ))
186}
187
188#[cfg(test)]
189mod tests {
190 use super::*;
191 use kanban_domain::command_batch::CommandBatch;
192 use kanban_domain::*;
193
194 #[test]
195 fn test_rollback_failed_names_both_the_batch_and_the_rollback_error() {
196 let err = rollback_failed(
197 KanbanError::Internal("batch boom".into()),
198 KanbanError::Internal("restore boom".into()),
199 );
200
201 let msg = err.to_string();
202 assert!(
203 msg.contains("batch boom"),
204 "batch error lost (got: {msg:?})"
205 );
206 assert!(
207 msg.contains("restore boom"),
208 "rollback error lost (got: {msg:?})"
209 );
210 assert!(
211 msg.contains("State may be inconsistent"),
212 "a failed rollback must say the store is left in an unknown state \
213 (got: {msg:?})"
214 );
215 }
216
217 #[derive(Default)]
218 struct StubBackend {
219 batch_count_calls: std::sync::atomic::AtomicUsize,
220 batch_count_fails: bool,
221 }
222
223 impl DataStore for StubBackend {
224 fn get_prefix(&self, _name: &str) -> KanbanResult<Option<kanban_domain::Prefix>> {
225 unimplemented!()
226 }
227 fn list_prefixes(&self) -> KanbanResult<Vec<kanban_domain::Prefix>> {
228 unimplemented!()
229 }
230 fn upsert_prefix(&self, _prefix: kanban_domain::Prefix) -> KanbanResult<()> {
231 unimplemented!()
232 }
233 fn get_label(&self, _name: &str) -> KanbanResult<Option<kanban_domain::Label>> {
234 unimplemented!()
235 }
236 fn list_labels(&self) -> KanbanResult<Vec<kanban_domain::Label>> {
237 unimplemented!()
238 }
239 fn upsert_label(&self, _label: kanban_domain::Label) -> KanbanResult<()> {
240 unimplemented!()
241 }
242 fn delete_label(&self, _name: &str) -> KanbanResult<()> {
243 unimplemented!()
244 }
245 fn get_board(&self, _id: Uuid) -> KanbanResult<Option<Board>> {
246 unimplemented!()
247 }
248 fn list_boards(&self) -> KanbanResult<Vec<Board>> {
249 unimplemented!()
250 }
251 fn upsert_board(&self, _board: Board) -> KanbanResult<()> {
252 unimplemented!()
253 }
254 fn delete_board(&self, _id: Uuid) -> KanbanResult<()> {
255 unimplemented!()
256 }
257 fn get_column(&self, _id: Uuid) -> KanbanResult<Option<Column>> {
258 unimplemented!()
259 }
260 fn list_columns_by_board(&self, _board_id: Uuid) -> KanbanResult<Vec<Column>> {
261 unimplemented!()
262 }
263 fn list_all_columns(&self) -> KanbanResult<Vec<Column>> {
264 unimplemented!()
265 }
266 fn upsert_column(&self, _column: Column) -> KanbanResult<()> {
267 unimplemented!()
268 }
269 fn delete_column(&self, _id: Uuid) -> KanbanResult<()> {
270 unimplemented!()
271 }
272 fn delete_columns_by_board(&self, _board_id: Uuid) -> KanbanResult<()> {
273 unimplemented!()
274 }
275 fn get_card(&self, _id: Uuid) -> KanbanResult<Option<Card>> {
276 unimplemented!()
277 }
278 fn list_all_cards(&self) -> KanbanResult<Vec<Card>> {
279 unimplemented!()
280 }
281 fn list_cards_by_column(&self, _column_id: Uuid) -> KanbanResult<Vec<Card>> {
282 unimplemented!()
283 }
284 fn list_cards_by_sprint(&self, _sprint_id: Uuid) -> KanbanResult<Vec<Card>> {
285 unimplemented!()
286 }
287 fn count_cards_in_column(&self, _column_id: Uuid) -> KanbanResult<usize> {
288 unimplemented!()
289 }
290 fn count_cards_in_column_excluding(
291 &self,
292 _column_id: Uuid,
293 _exclude: &[Uuid],
294 ) -> KanbanResult<usize> {
295 unimplemented!()
296 }
297 fn upsert_card(&self, _card: Card) -> KanbanResult<()> {
298 unimplemented!()
299 }
300 fn delete_card(&self, _id: Uuid) -> KanbanResult<()> {
301 unimplemented!()
302 }
303 fn delete_cards_by_columns(&self, _column_ids: &[Uuid]) -> KanbanResult<()> {
304 unimplemented!()
305 }
306 fn clear_sprint_from_cards(
307 &self,
308 _sprint_id: Uuid,
309 _timestamp: chrono::DateTime<chrono::Utc>,
310 ) -> KanbanResult<()> {
311 unimplemented!()
312 }
313 fn get_archived_card(&self, _card_id: Uuid) -> KanbanResult<Option<ArchivedCard>> {
314 unimplemented!()
315 }
316 fn list_archived_cards(&self) -> KanbanResult<Vec<ArchivedCard>> {
317 unimplemented!()
318 }
319 fn insert_archived_card(&self, _ac: ArchivedCard) -> KanbanResult<()> {
320 unimplemented!()
321 }
322 fn delete_archived_card(&self, _card_id: Uuid) -> KanbanResult<()> {
323 unimplemented!()
324 }
325 fn list_archived_boards(&self) -> KanbanResult<Vec<kanban_domain::ArchivedBoard>> {
326 unimplemented!()
327 }
328 fn get_sprint(&self, _id: Uuid) -> KanbanResult<Option<Sprint>> {
329 unimplemented!()
330 }
331 fn list_sprints_by_board(&self, _board_id: Uuid) -> KanbanResult<Vec<Sprint>> {
332 unimplemented!()
333 }
334 fn list_all_sprints(&self) -> KanbanResult<Vec<Sprint>> {
335 unimplemented!()
336 }
337 fn upsert_sprint(&self, _sprint: Sprint) -> KanbanResult<()> {
338 unimplemented!()
339 }
340 fn delete_sprint(&self, _id: Uuid) -> KanbanResult<()> {
341 unimplemented!()
342 }
343 fn delete_sprints_by_board(&self, _board_id: Uuid) -> KanbanResult<()> {
344 unimplemented!()
345 }
346 fn get_graph(&self) -> KanbanResult<DependencyGraph> {
347 unimplemented!()
348 }
349 fn set_graph(&self, _graph: DependencyGraph) -> KanbanResult<()> {
350 unimplemented!()
351 }
352 }
353
354 impl CommandStore for StubBackend {
355 fn append_batch(&self, _batch: &CommandBatch) -> KanbanResult<u64> {
356 unimplemented!()
357 }
358 fn batch_count(&self) -> KanbanResult<u64> {
359 self.batch_count_calls
360 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
361 if self.batch_count_fails {
362 Err(KanbanError::Internal("stub batch_count boom".into()))
363 } else {
364 Ok(0)
365 }
366 }
367 fn load_batches(&self, _offset: u64, _limit: u64) -> KanbanResult<Vec<CommandBatch>> {
368 unimplemented!()
369 }
370 }
371
372 impl KanbanBackend for StubBackend {
373 fn as_data_store(&self) -> &dyn DataStore {
374 self
375 }
376 fn with_transaction(&self, _f: TransactionFn<'_>) -> KanbanResult<()> {
377 unimplemented!()
378 }
379 }
380
381 #[test]
382 fn test_backend_without_local_persistence_returns_none() {
383 let backend = StubBackend::default();
384 let backend: &dyn KanbanBackend = &backend;
385 assert!(backend.local_persistence().is_none());
386 }
387
388 #[test]
389 fn test_default_compatibility_notice_is_none() {
390 let backend = StubBackend::default();
391 let backend: &dyn KanbanBackend = &backend;
392 assert!(backend.compatibility_notice().is_none());
393 }
394
395 #[test]
396 fn test_kanban_backend_as_any_defaults_to_none() {
397 let backend = StubBackend::default();
398 let backend: &dyn KanbanBackend = &backend;
399 assert!(backend.as_any().is_none());
400 }
401
402 #[test]
403 fn test_mark_dirty_is_callable_on_a_backend_that_does_not_override_it() {
404 let backend = StubBackend::default();
405 let backend: &dyn KanbanBackend = &backend;
406 backend.mark_dirty();
407 }
408
409 #[tokio::test]
410 async fn test_default_probe_delegates_to_batch_count() {
411 let backend = StubBackend::default();
412 let b: &dyn KanbanBackend = &backend;
413
414 let result = b.probe().await;
415
416 assert!(
417 result.is_ok(),
418 "expected probe() to succeed, got: {result:?}"
419 );
420 assert_eq!(
421 backend
422 .batch_count_calls
423 .load(std::sync::atomic::Ordering::Relaxed),
424 1,
425 "expected the default probe to call batch_count exactly once"
426 );
427 }
428
429 #[tokio::test]
430 async fn test_default_probe_propagates_a_batch_count_error() {
431 let backend = StubBackend {
432 batch_count_fails: true,
433 ..Default::default()
434 };
435 let b: &dyn KanbanBackend = &backend;
436
437 let result = b.probe().await;
438
439 match result {
440 Err(e) => assert!(
441 e.to_string().contains("stub batch_count boom"),
442 "expected the batch_count error to propagate, got: {e}"
443 ),
444 Ok(_) => panic!("expected probe() to propagate the batch_count failure"),
445 }
446 }
447}