Skip to main content

meerkat_sqlite/
lib.rs

1//! Shared SQLite mechanics for Meerkat stores.
2//!
3//! This crate is the single home for the connection policy, schema-evolution,
4//! and maintenance-fence machinery that was previously copy-pasted across the
5//! store crates (six independently authored PRAGMA setups with divergent busy
6//! timeouts, `CREATE TABLE IF NOT EXISTS` everywhere, and no version ledger).
7//! See `docs/plans/storage-unification-plan.md` (Phase 3) in the workspace
8//! root for the design rationale.
9//!
10//! What lives here:
11//!
12//! - [`profile`]: DDL-free connection opening under named policy profiles
13//!   ([`ConnectionProfile::Primary`], [`ConnectionProfile::ReadOnly`],
14//!   [`ConnectionProfile::Maintenance`]). Opening a connection never runs
15//!   schema DDL; stores apply their [`ledger`] domain after opening. The
16//!   optional [`OpenOptions::schema_preflight`] proves an exact current,
17//!   released-predecessor, or fresh-domain shape before any mutating pragma,
18//!   [`JournalPolicy`] states per profile whether an open establishes WAL or
19//!   preserves the mode it finds, and [`WriteContact`] types each profile's
20//!   honest no-write guarantee (WAL reads may need sidecar files).
21//! - [`ledger`]: the per-file migration ledger (`meerkat_schema(domain,
22//!   version)`) with the pinned concurrent-open transaction protocol and the
23//!   typed refusals for future, pre-floor, gap, unledgered-owned, and
24//!   fingerprint-mismatched shapes. Ledger state that is malformed (wrong
25//!   shape, duplicate rows, non-positive versions) is refused typed, never
26//!   healed.
27//! - [`json_column`]: the TEXT-or-BLOB tolerant JSON column codec.
28//! - [`fence`]: the per-operation maintenance-fence guards. Store operations
29//!   take a shared guard; offline migration takes the exclusive side and
30//!   waits for in-flight operations to drain. Stores built on this crate hold
31//!   no bespoke opener, which is what makes them fence-aware for free.
32//! - [`watch`]: the cross-process change watch over a database file and its
33//!   sidecars (coalesced notification ticks plus a bounded sweep; a hint,
34//!   never permission or custody).
35//! - [`error`]: the crate error type plus the storage-level error
36//!   classification (transient / corrupt) that store crates layer their
37//!   staleness semantics on top of. Adoption contract: store crates route
38//!   every raw rusqlite error through [`classify_sqlite_error`] instead of
39//!   re-matching SQLite error codes locally.
40//!
41//! # Retryability
42//!
43//! Error classification alone does not authorize a retry: a transient failure
44//! can land after a write committed but before success became observable, and
45//! blind retry duplicates non-idempotent effects. Automatic retry is only
46//! sound for idempotent or CAS-keyed operations; an indeterminate
47//! non-idempotent write requires outcome reconciliation (read back, then
48//! decide) before any retry. Bounded waiting for lock contention is the
49//! connection busy handler's job, not a caller retry loop.
50
51pub mod error;
52pub mod fence;
53pub mod json_column;
54pub mod ledger;
55pub mod profile;
56#[cfg(not(target_arch = "wasm32"))]
57pub mod watch;
58
59pub use error::{
60    BridgeEligibility, CurrentCatalogUnavailability, SqliteErrorClass, SqliteStoreError,
61    classify_sqlite_error, is_busy_or_locked,
62};
63pub use fence::{ExclusiveFence, OperationGuard, fence_lock_path};
64pub use json_column::JsonColumnBytes;
65pub use ledger::{
66    LedgerReport, MaintenanceBridgeReport, MaintenancePrepareFn, MaintenancePrepareReport,
67    MaintenanceRecordRefusal, Migration, SchemaDomain, SchemaObject, SchemaObjectKind,
68    SchemaPredecessor, apply_domain_migrations, bridge_unledgered_domain, domain_version,
69    preflight_schema_eligibility, verify_released_schema_fingerprint,
70    verify_released_schema_structure,
71};
72pub use profile::{
73    ConnectionProfile, DatabaseFileIdentity, JournalPolicy, OpenOptions, SHARED_BUSY_TIMEOUT,
74    WriteContact, begin_immediate, open, open_with, validate_database_file,
75};
76/// The connection type [`open`] returns, so callers can name it without
77/// taking their own direct `rusqlite` dependency.
78pub use rusqlite::Connection;