pub mod compatibility;
pub mod factory;
pub mod local_persistence;
pub mod remote_writes;
pub use compatibility::CompatibilityNotice;
pub use factory::{KanbanBackendFactory, KanbanBackendRegistry};
pub use local_persistence::LocalPersistence;
pub use remote_writes::{
RemoteBatchOutcome, RemoteBatchWrites, RemoteBoardWrites, RemoteCardWrites, RemoteGraphWrites,
RemoteSprintWrites, RemoteWrites,
};
use async_trait::async_trait;
use kanban_domain::command_store::CommandStore;
use kanban_domain::data_store::DataStore;
use kanban_domain::{KanbanError, KanbanResult};
use uuid::Uuid;
#[async_trait]
pub trait KanbanBackend: DataStore + CommandStore + Send + Sync {
fn as_data_store(&self) -> &dyn DataStore;
async fn flush(&self) -> KanbanResult<()> {
Ok(())
}
async fn reload(&self) -> KanbanResult<()> {
Ok(())
}
async fn probe(&self) -> KanbanResult<()> {
self.batch_count().map(|_| ())
}
fn mark_dirty(&self) {}
fn needs_flush(&self) -> bool {
false
}
fn needs_save_worker(&self) -> bool {
false
}
fn instance_id(&self) -> Uuid {
Uuid::nil()
}
fn local_persistence(&self) -> Option<&dyn crate::LocalPersistence> {
None
}
fn compatibility_notice(&self) -> Option<crate::CompatibilityNotice> {
None
}
fn health_checker(&self) -> Option<Box<dyn kanban_core::HealthChecker>> {
None
}
fn remote_writes(&self) -> Option<&dyn crate::RemoteWrites> {
None
}
fn remote_board_writes(&self) -> Option<&dyn crate::RemoteBoardWrites> {
None
}
fn remote_card_writes(&self) -> Option<&dyn crate::RemoteCardWrites> {
None
}
fn remote_batch_writes(&self) -> Option<&dyn crate::RemoteBatchWrites> {
None
}
fn remote_sprint_writes(&self) -> Option<&dyn crate::RemoteSprintWrites> {
None
}
fn remote_graph_writes(&self) -> Option<&dyn crate::RemoteGraphWrites> {
None
}
fn as_any(&self) -> Option<&dyn std::any::Any> {
None
}
fn with_transaction(&self, f: TransactionFn<'_>) -> KanbanResult<()>;
}
pub type TransactionFn<'f> = Box<dyn FnOnce() -> KanbanResult<()> + 'f>;
pub fn rollback_failed(batch_err: KanbanError, rollback_err: KanbanError) -> KanbanError {
KanbanError::Internal(format!(
"Batch failed ({batch_err}) and rollback also failed ({rollback_err}). State may be inconsistent."
))
}
#[cfg(test)]
mod tests {
use super::*;
use kanban_domain::command_batch::CommandBatch;
use kanban_domain::*;
#[test]
fn test_rollback_failed_names_both_the_batch_and_the_rollback_error() {
let err = rollback_failed(
KanbanError::Internal("batch boom".into()),
KanbanError::Internal("restore boom".into()),
);
let msg = err.to_string();
assert!(
msg.contains("batch boom"),
"batch error lost (got: {msg:?})"
);
assert!(
msg.contains("restore boom"),
"rollback error lost (got: {msg:?})"
);
assert!(
msg.contains("State may be inconsistent"),
"a failed rollback must say the store is left in an unknown state \
(got: {msg:?})"
);
}
#[derive(Default)]
struct StubBackend {
batch_count_calls: std::sync::atomic::AtomicUsize,
batch_count_fails: bool,
}
impl DataStore for StubBackend {
fn get_prefix(&self, _name: &str) -> KanbanResult<Option<kanban_domain::Prefix>> {
unimplemented!()
}
fn list_prefixes(&self) -> KanbanResult<Vec<kanban_domain::Prefix>> {
unimplemented!()
}
fn upsert_prefix(&self, _prefix: kanban_domain::Prefix) -> KanbanResult<()> {
unimplemented!()
}
fn get_label(&self, _name: &str) -> KanbanResult<Option<kanban_domain::Label>> {
unimplemented!()
}
fn list_labels(&self) -> KanbanResult<Vec<kanban_domain::Label>> {
unimplemented!()
}
fn upsert_label(&self, _label: kanban_domain::Label) -> KanbanResult<()> {
unimplemented!()
}
fn delete_label(&self, _name: &str) -> KanbanResult<()> {
unimplemented!()
}
fn get_board(&self, _id: Uuid) -> KanbanResult<Option<Board>> {
unimplemented!()
}
fn list_boards(&self) -> KanbanResult<Vec<Board>> {
unimplemented!()
}
fn upsert_board(&self, _board: Board) -> KanbanResult<()> {
unimplemented!()
}
fn delete_board(&self, _id: Uuid) -> KanbanResult<()> {
unimplemented!()
}
fn get_column(&self, _id: Uuid) -> KanbanResult<Option<Column>> {
unimplemented!()
}
fn list_columns_by_board(&self, _board_id: Uuid) -> KanbanResult<Vec<Column>> {
unimplemented!()
}
fn list_all_columns(&self) -> KanbanResult<Vec<Column>> {
unimplemented!()
}
fn upsert_column(&self, _column: Column) -> KanbanResult<()> {
unimplemented!()
}
fn delete_column(&self, _id: Uuid) -> KanbanResult<()> {
unimplemented!()
}
fn delete_columns_by_board(&self, _board_id: Uuid) -> KanbanResult<()> {
unimplemented!()
}
fn get_card(&self, _id: Uuid) -> KanbanResult<Option<Card>> {
unimplemented!()
}
fn list_all_cards(&self) -> KanbanResult<Vec<Card>> {
unimplemented!()
}
fn list_cards_by_column(&self, _column_id: Uuid) -> KanbanResult<Vec<Card>> {
unimplemented!()
}
fn list_cards_by_sprint(&self, _sprint_id: Uuid) -> KanbanResult<Vec<Card>> {
unimplemented!()
}
fn count_cards_in_column(&self, _column_id: Uuid) -> KanbanResult<usize> {
unimplemented!()
}
fn count_cards_in_column_excluding(
&self,
_column_id: Uuid,
_exclude: &[Uuid],
) -> KanbanResult<usize> {
unimplemented!()
}
fn upsert_card(&self, _card: Card) -> KanbanResult<()> {
unimplemented!()
}
fn delete_card(&self, _id: Uuid) -> KanbanResult<()> {
unimplemented!()
}
fn delete_cards_by_columns(&self, _column_ids: &[Uuid]) -> KanbanResult<()> {
unimplemented!()
}
fn clear_sprint_from_cards(
&self,
_sprint_id: Uuid,
_timestamp: chrono::DateTime<chrono::Utc>,
) -> KanbanResult<()> {
unimplemented!()
}
fn get_archived_card(&self, _card_id: Uuid) -> KanbanResult<Option<ArchivedCard>> {
unimplemented!()
}
fn list_archived_cards(&self) -> KanbanResult<Vec<ArchivedCard>> {
unimplemented!()
}
fn insert_archived_card(&self, _ac: ArchivedCard) -> KanbanResult<()> {
unimplemented!()
}
fn delete_archived_card(&self, _card_id: Uuid) -> KanbanResult<()> {
unimplemented!()
}
fn list_archived_boards(&self) -> KanbanResult<Vec<kanban_domain::ArchivedBoard>> {
unimplemented!()
}
fn get_sprint(&self, _id: Uuid) -> KanbanResult<Option<Sprint>> {
unimplemented!()
}
fn list_sprints_by_board(&self, _board_id: Uuid) -> KanbanResult<Vec<Sprint>> {
unimplemented!()
}
fn list_all_sprints(&self) -> KanbanResult<Vec<Sprint>> {
unimplemented!()
}
fn upsert_sprint(&self, _sprint: Sprint) -> KanbanResult<()> {
unimplemented!()
}
fn delete_sprint(&self, _id: Uuid) -> KanbanResult<()> {
unimplemented!()
}
fn delete_sprints_by_board(&self, _board_id: Uuid) -> KanbanResult<()> {
unimplemented!()
}
fn get_graph(&self) -> KanbanResult<DependencyGraph> {
unimplemented!()
}
fn set_graph(&self, _graph: DependencyGraph) -> KanbanResult<()> {
unimplemented!()
}
}
impl CommandStore for StubBackend {
fn append_batch(&self, _batch: &CommandBatch) -> KanbanResult<u64> {
unimplemented!()
}
fn batch_count(&self) -> KanbanResult<u64> {
self.batch_count_calls
.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
if self.batch_count_fails {
Err(KanbanError::Internal("stub batch_count boom".into()))
} else {
Ok(0)
}
}
fn load_batches(&self, _offset: u64, _limit: u64) -> KanbanResult<Vec<CommandBatch>> {
unimplemented!()
}
}
impl KanbanBackend for StubBackend {
fn as_data_store(&self) -> &dyn DataStore {
self
}
fn with_transaction(&self, _f: TransactionFn<'_>) -> KanbanResult<()> {
unimplemented!()
}
}
#[test]
fn test_backend_without_local_persistence_returns_none() {
let backend = StubBackend::default();
let backend: &dyn KanbanBackend = &backend;
assert!(backend.local_persistence().is_none());
}
#[test]
fn test_default_compatibility_notice_is_none() {
let backend = StubBackend::default();
let backend: &dyn KanbanBackend = &backend;
assert!(backend.compatibility_notice().is_none());
}
#[test]
fn test_kanban_backend_as_any_defaults_to_none() {
let backend = StubBackend::default();
let backend: &dyn KanbanBackend = &backend;
assert!(backend.as_any().is_none());
}
#[test]
fn test_mark_dirty_is_callable_on_a_backend_that_does_not_override_it() {
let backend = StubBackend::default();
let backend: &dyn KanbanBackend = &backend;
backend.mark_dirty();
}
#[tokio::test]
async fn test_default_probe_delegates_to_batch_count() {
let backend = StubBackend::default();
let b: &dyn KanbanBackend = &backend;
let result = b.probe().await;
assert!(
result.is_ok(),
"expected probe() to succeed, got: {result:?}"
);
assert_eq!(
backend
.batch_count_calls
.load(std::sync::atomic::Ordering::Relaxed),
1,
"expected the default probe to call batch_count exactly once"
);
}
#[tokio::test]
async fn test_default_probe_propagates_a_batch_count_error() {
let backend = StubBackend {
batch_count_fails: true,
..Default::default()
};
let b: &dyn KanbanBackend = &backend;
let result = b.probe().await;
match result {
Err(e) => assert!(
e.to_string().contains("stub batch_count boom"),
"expected the batch_count error to propagate, got: {e}"
),
Ok(_) => panic!("expected probe() to propagate the batch_count failure"),
}
}
}