# kuatia-storage-sql SQL-backed `Store` implementation for the kuatia ledger. Uses `sqlx::Any` for database-agnostic queries. Enable features to select the backend: ```toml [dependencies] kuatia-storage-sql = { features = ["sqlite"] } # or "postgres" ``` ## Backends | Feature | Backend | Status | |---------|---------|--------| | `sqlite` (default) | SQLite via sqlx | Conformance tests pass | | `postgres` | PostgreSQL via sqlx | Portable DDL/queries; needs a running instance to test | The migrations in `src/migrations/` serve both backends. Applied migrations are tracked in a `_migrations` table, so `migrate()` is idempotent. Upserts use portable `ON CONFLICT … DO UPDATE`, and all ids are generated in Rust (no `AUTOINCREMENT`/`SERIAL`). Identifier and payload columns are text, so any row is readable in a plain SQL client for auditing: content-addressed ids as lower-case hex `TEXT`, structured payloads as their JSON `TEXT` serialization (never queried into). Amounts are the exception, stored natively so the database can sum and order them: `NUMERIC(39,0)` on PostgreSQL, a 16-byte order-preserving `BLOB` on SQLite. A SQLite amount reads back as a decimal string through `kuatia_i128_to_text(value)` in any process that called `install_drivers()`; a plain `sqlite3` CLI has no such function and sees the blob. See ADR-0024. ## Usage Most callers never name this crate: `kuatia` re-exports it as `kuatia::sql` behind its `sqlite` or `postgres` feature, and `Ledger::connect(url)` wraps the whole of the below. ```rust use kuatia_storage_sql::SqlStore; let store = SqlStore::connect("sqlite::memory:").await?; ``` `connect` installs the drivers, caps the pool at one connection and applies the schema. Supply your own pool to size it differently, and then two things are yours to get right. `install_drivers()` replaces `sqlx::any::install_default_drivers()` and must run **before** the pool connects: it registers the SQLite money functions as an auto-extension, so a connection opened earlier would not have them. And `sqlite::memory:` gives each connection its own separate database, so a pool wider than one connection splits the ledger into a copy per connection. ```rust use kuatia_storage_sql::{SqlStore, install_drivers}; install_drivers()?; let pool = sqlx::any::AnyPoolOptions::new() .max_connections(1) .connect("sqlite::memory:").await?; let store = SqlStore::new(pool); store.migrate().await?; ``` ## Schema Tables: `accounts`, `account_head`, `postings`, `live_postings`, `transfers`, `transfer_accounts`, `events`, `books`, `balance_projection`. Migrations run via `store.migrate()`; the full catalog is in `doc/storage-schema.md`.