pub mod factory;
pub mod local_persistence;
pub mod remote_writes;
pub use factory::{KanbanBackendFactory, KanbanBackendRegistry};
pub use local_persistence::LocalPersistence;
pub use remote_writes::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(())
}
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 health_checker(&self) -> Option<Box<dyn kanban_core::HealthChecker>> {
None
}
fn remote_writes(&self) -> Option<&dyn crate::RemoteWrites> {
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:?})"
);
}
struct StubBackend;
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_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 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!()
}
fn snapshot(&self) -> KanbanResult<Snapshot> {
unimplemented!()
}
fn apply_snapshot(&self, _snapshot: Snapshot) -> KanbanResult<()> {
unimplemented!()
}
}
impl CommandStore for StubBackend {
fn append_batch(&self, _batch: &CommandBatch) -> KanbanResult<u64> {
unimplemented!()
}
fn batch_count(&self) -> KanbanResult<u64> {
unimplemented!()
}
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;
let backend: &dyn KanbanBackend = &backend;
assert!(backend.local_persistence().is_none());
}
}