Skip to main content

kanban_persistence_sqlite/
lib.rs

1pub mod backend_factory;
2pub mod sqlite_backend;
3pub mod sqlite_store;
4
5pub use backend_factory::SqliteBackendFactory;
6pub use sqlite_backend::SqliteBackend;
7
8pub use sqlite_store::SqliteStore;
9pub use sqlite_store::SUPPORTED_SCHEMA_VERSION;
10
11#[cfg(feature = "test-helpers")]
12use kanban_persistence::PersistenceError;
13
14/// Construct a SQLite database file at `path` with a `metadata` row whose
15/// `schema_version` is forced to `version`. Intended for cross-crate
16/// integration tests that need to exercise the `UnsupportedFutureVersion`
17/// refusal at every surface (service, MCP, CLI) without each test
18/// reimplementing the seed SQL.
19///
20/// Bypasses `SqliteStore::open` deliberately — opening would normalise the
21/// version via `migrate()` before the test could observe the pre-bumped
22/// state. Writes only the `metadata` table; the rest of the schema is
23/// created by `SqliteStore::open` on first real load.
24///
25/// Gated behind the `test-helpers` feature so it does not ship in release
26/// binaries. Mirrors the pattern in `kanban-persistence::test_helpers`.
27#[cfg(feature = "test-helpers")]
28pub async fn write_test_metadata_with_schema_version(
29    path: &std::path::Path,
30    version: u32,
31) -> Result<(), PersistenceError> {
32    use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
33    let pool = SqlitePoolOptions::new()
34        .max_connections(1)
35        .connect_with(
36            SqliteConnectOptions::new()
37                .filename(path)
38                .create_if_missing(true),
39        )
40        .await
41        .map_err(|e| PersistenceError::Database(e.to_string()))?;
42    sqlx::raw_sql(&format!(
43        "CREATE TABLE IF NOT EXISTS metadata (
44            id INTEGER PRIMARY KEY CHECK (id = 1),
45            instance_id TEXT NOT NULL,
46            saved_at TEXT NOT NULL,
47            schema_version INTEGER NOT NULL DEFAULT {SUPPORTED_SCHEMA_VERSION},
48            writer_version TEXT,
49            writer_commit TEXT
50        );
51        INSERT OR REPLACE INTO metadata (id, instance_id, saved_at, schema_version)
52        VALUES (1, '550e8400-e29b-41d4-a716-446655440000', '2030-01-01T00:00:00Z', {version});"
53    ))
54    .execute(&pool)
55    .await
56    .map_err(|e| PersistenceError::Database(e.to_string()))?;
57    pool.close().await;
58    Ok(())
59}
60
61/// Test-only companion to [`write_test_metadata_with_schema_version`]: probe
62/// the metadata row's `schema_version` without going through `SqliteStore::open`
63/// (which would normalise it). Used by integration tests that want to assert
64/// a refused open didn't bump the on-disk version.
65///
66/// Gated behind the `test-helpers` feature; see the sibling function for
67/// rationale.
68#[cfg(feature = "test-helpers")]
69pub async fn read_test_schema_version(
70    path: &std::path::Path,
71) -> Result<Option<u32>, PersistenceError> {
72    use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
73    let pool = SqlitePoolOptions::new()
74        .max_connections(1)
75        .connect_with(SqliteConnectOptions::new().filename(path))
76        .await
77        .map_err(|e| PersistenceError::Database(e.to_string()))?;
78    let version: Option<u32> =
79        sqlx::query_scalar("SELECT schema_version FROM metadata WHERE id = 1")
80            .fetch_optional(&pool)
81            .await
82            .map_err(|e| PersistenceError::Database(e.to_string()))?;
83    pool.close().await;
84    Ok(version)
85}