surreal_sync_core/checkpoint/store.rs
1//! Checkpoint storage trait and types.
2//!
3//! This module defines the CheckpointStore trait for version-agnostic
4//! checkpoint storage operations, plus shared types.
5
6use anyhow::Result;
7use async_trait::async_trait;
8use chrono::{DateTime, Utc};
9use serde::{Deserialize, Serialize};
10
11/// Checkpoint identifier for storage
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct CheckpointID {
14 /// Database type (e.g., "postgresql-pgoutput", "postgresql-wal2json", "mysql", "mongodb")
15 pub database_type: String,
16 /// Sync phase ("full_sync_start" or "full_sync_end")
17 pub phase: String,
18}
19
20/// Checkpoint data stored in backend
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct StoredCheckpoint {
23 /// Serialized checkpoint (e.g., LSN for PostgreSQL, resume token for MongoDB)
24 pub checkpoint_data: String,
25 /// Database type for validation
26 pub database_type: String,
27 /// Sync phase for validation
28 pub phase: String,
29 /// Timestamp when checkpoint was created
30 pub created_at: DateTime<Utc>,
31}
32
33/// Trait for checkpoint storage operations.
34///
35/// This trait abstracts the storage backend for checkpoint operations,
36/// allowing the same checkpoint logic to work with:
37/// - Filesystem storage (`FilesystemStore`)
38/// - SurrealDB v2 (`Surreal2Store`)
39/// - SurrealDB v3 (`Surreal3Store` behind `surreal-sync-surreal` feature `v3`)
40#[async_trait]
41pub trait CheckpointStore: Send + Sync {
42 /// Store a checkpoint in the storage backend.
43 async fn store_checkpoint(&self, id: &CheckpointID, checkpoint_data: String) -> Result<()>;
44
45 /// Read a checkpoint from the storage backend.
46 ///
47 /// Returns None if the checkpoint doesn't exist.
48 async fn read_checkpoint(&self, id: &CheckpointID) -> Result<Option<StoredCheckpoint>>;
49}