kuatia-money, kuatia-storage, kuatia-storage-sqlADR-0011 made the Cent backing a
compile-time choice between i64 and i128, and stored every amount as a
decimal TEXT column. That hid the width and sidestepped the fact that neither
backend has a native 128-bit integer. It also accepted a cost, recorded there:
"the database cannot order or range-filter on the amount."
That cost is now on the hot path. The in-transaction overdraft floor
(commit.rs::live_balance) fetched every live posting row for an
(owner, asset) pair, parsed each decimal string and folded them in Rust,
inside the commit transaction, once per overdraft-forbidding account a transfer
touches. Ledger::compute_balance did the same through the Store trait. The
schema could not index, order or aggregate an amount at all.
Can amounts be stored in a form the database understands, without reintroducing the two things text was protecting against: a width that leaks into the API, and inexact or wrapping money arithmetic?
i128 cargo feature being toggled without a
migration, so the stored width cannot be the backing's width.Cent::checked_sum.Cent API must keep the backing width out of public signatures, and the
content-addressed hash preimage (to_canonical_bytes, frozen by
CANONICAL_VERSION) must not change: switching backings must not rehash the
ledger.sqlx::Any is the whole SQL backend. Its value universe is
Bool/Int/Real/Text/Blob, and it never exposes the underlying driver handle.Change nothing.
Pros:
unsafe.Cons:
sqlx::Any, with casts at the dialect seamNUMERIC(39,0) on PostgreSQL, a fixed 16-byte BLOB on SQLite. Amounts cross
the Any boundary as String (Postgres) or Vec<u8> (SQLite), with the SQL
carrying CAST($n AS NUMERIC) / CAST(col AS TEXT) on Postgres. SQLite gets a
registered kuatia_i128_sum aggregate; Postgres uses plain SUM().
Pros:
NUMERIC is arbitrary precision, and
kuatia_i128_sum folds with i128::checked_add and raises on overflow.Dialect seam
instead of spreading through the query code.Cons:
AnyRow converts every column's type before any column is read
and has no mapping for NUMERIC, so one forgotten cast fails the whole row.
SELECT * over a money table is therefore no longer allowed.unsafe in the
repository.sqlx::Any for concrete Pool<Sqlite> / Pool<Postgres>Write real Type/Encode/Decode impls per driver, so Cent binds as binary
NUMERIC on Postgres and as a blob on SQLite.
Pros:
SqliteConnectOptions can register the aggregate per
connection, with no global state and no ordering trap.SELECT * keeps working and compile-time-checked queries
become possible.Cons:
account/posting/transfer/commit/event/
book/projection must be duplicated per backend or made generic over a
Database bound, whose where-clauses fight "clarity over cleverness".Type/Encode/Decode<Any> for a Cent wrapperInstead of binding String/Vec<u8> and converting by hand, teach Any about
Cent directly.
Pros:
.bind(cent) and row.try_get::<Cent, _>().Cons:
AnyValueKind and AnyArgumentBuffer are not re-exported from
the sqlx facade and are #[doc(hidden)] in sqlx-core, so this needs a
direct dependency on a crate whose own description says "not for direct use".AnyValueKind has no numeric variant either, so
the Postgres cast is still required.Chosen option: Option 2. It gets the whole payload (native storage, exact
in-database summation, orderable amounts) for a diff confined to the Dialect
seam, two row helpers and one new module, where Option 3 is a rewrite of the
storage crate and Option 4 buys nothing for a #[doc(hidden)] dependency.
Concretely:
Backing is. Postgres
uses NUMERIC(39,0), which is exactly the decimal width of i128. SQLite
uses a 16-byte BLOB. Toggling the i128 feature needs no migration, and a
database written under i128 fails loudly when read under i64 rather than
truncating.encode_storage_i128 in kuatia-money). SQLite compares
blobs byte-wise, and all amounts are exactly 16 bytes, so unsigned
lexicographic order is numeric order: i128::MIN is all zeros, 0 is
0x80 00…, i128::MAX is all 0xFF. ORDER BY value and
WHERE value > ? are correct for free, which is what Postgres NUMERIC gives
natively — so both backends gain the same capability rather than diverging.to_storage_bytes byte-identical to to_canonical_bytes, and a
cross-use of the frozen hash preimage would then have silently worked until
either encoding was touched. The flipped bit makes that mistake fail
immediately.kuatia_i128_sum (an aggregate
folding with i128::checked_add, raising on overflow or on any argument that
is not a 16-byte amount, and summing an empty group to zero rather than to
NULL), plus kuatia_i128_from_text and kuatia_i128_to_text. The scalars
do the 011 migration's conversion and let tooling built on this crate read a
column that is no longer human-readable.sqlite3_auto_extension, because
sqlx::Any never exposes the sqlite3* handle (AnyConnection.backend is
private, so there is nothing to downcast). Callers use
kuatia_storage_sql::install_drivers() in place of
sqlx::any::install_default_drivers(), before connecting a pool.PostingStore::live_balance is a Store method, so the ledger's
authoritative balance read goes through it too, with the in-memory backend
summing in Rust as the conformance oracle.Ledger::compute_balance.Cent::checked_sum over the same rows on every backend, so the two paths
cannot drift apart silently.sqlx::Any, so a read
costs a numeric → text → parse hop. Only the storage is native, not the wire.SELECT touching money must go through Dialect::money_read.
A missed cast fails the entire row, and only the Postgres CI job catches it,
never the default SQLite run. SELECT * over a money table is banned, so a
future column added to postings will not be picked up automatically.install_drivers() must run before the first connection is opened. migrate()
probes for the functions on SQLite and returns
StoreError::Misconfigured rather than a bare "no such function", but the
ordering requirement is real.sqlite3 CLI, which does not
have kuatia_i128_to_text and shows a blob. Reading one by hand there means
taking hex(value), subtracting 0x80 from the leading byte and decoding a
128-bit big-endian two's-complement integer. Identifier and payload columns
are unaffected, and PostgreSQL NUMERIC stays fully legible.kuatia-storage-sql now depends directly on libsqlite3-sys, a
links = "sqlite3" crate. Only one version can exist in the graph, so the pin
must be kept in step with whatever sqlx-sqlite depends on; a sqlx bump is now
a deliberate event rather than a routine one.unsafe in the repository. It is confined to
crates/kuatia-storage-sql/src/sqlite_ext.rs. The one non-obvious hazard
there: sqlite3_aggregate_context returns memory aligned for 8 bytes, not the
16 an i128 needs, so the accumulator holds raw bytes rather than an i128
field.CentBacking / cargo-feature decision itself
stands.BIGINT → text → backend-native.balance_projection.balance) and
ADR-0022 (live_postings.value).