1.08 PostgreSQL Schema v1, Constraints and Roles: Exit Evidence
Evidence · Step: 1.08 PostgreSQL Schema v1, Constraints and Roles
| Step | 1.08 PostgreSQL Schema v1, Constraints and Roles |
| Date | 2026-11-11 |
| Database | PostgreSQL 18.6 (postgres:18-alpine) |
| Result | Passed |
Deliverables
Section titled “Deliverables”| Deliverable | File |
|---|---|
| Migration (up and down) | migrations/0001_ledger_schema.up.sql, migrations/0001_ledger_schema.down.sql |
| Roles | db/init/01-roles.sql (mezgeb_owner, mezgeb_app, mezgeb_readonly) |
| Local database | compose.yaml (PostgreSQL 18 on port 55432) |
| Schema documentation | docs/db-schema.md |
| Database tests | crates/engine/tests/schema.rs (13 tests) |
| CI | .github/workflows/ci.yml runs a PostgreSQL 18 service, creates the roles and runs the database tests; on CI they fail instead of skipping if the database is missing |
The scaffold migration 0001_init.sql (transfer model, never deployed) was
replaced.
Implementation tasks
Section titled “Implementation tasks”| Task | Where |
|---|---|
currencies |
Table with the 11 ISO 4217 currencies of crates/core |
accounts: status CHECK, non-negative totals, no negative normal-side balance unless allow_negative |
accounts CHECKs, including accounts_not_negative; also accounts_closed_at_zero and the accounts_guard trigger |
journal_entries: UNIQUE(client_id, idempotency_key), UNIQUE(reverses_entry_id) |
Both present |
journal_lines: amount > 0, side CHECK, currency FK, balance-after columns |
All present; the currency FK is composite with the account, so a line’s currency must be its account’s |
| Deferred trigger rejecting unbalanced entries | mezgeb_check_entry_balanced() on both entries and lines |
| Triggers rejecting UPDATE and DELETE on entries and lines | mezgeb_reject_change(), plus TRUNCATE |
| Roles and privileges | As specified; mezgeb_app may UPDATE only debits_total, credits_total, version and status |
Added beyond the list: the reversal triggers (never reverse a reversal; a reversal must mirror its original), the open-account check for lines (INV-24), and the account guard (identity columns immutable, no reopening, totals never decrease).
Testing requirements
Section titled “Testing requirements”All run as mezgeb_app, each in its own throwaway database.
| Requirement | Test | Result |
|---|---|---|
| An unbalanced insert fails at commit | unbalanced_entries_fail_at_commit |
Pass: MZ002 at commit, for an unbalanced entry, a one-line entry and an entry with no lines |
| UPDATE on lines fails | journal_history_cannot_change |
Pass: 42501 for the app; MZ001 even for the owner |
| DELETE fails | journal_history_cannot_change |
Pass: same, and TRUNCATE too |
Updating accounts.name fails due to privileges |
the_app_role_cannot_change_account_identity |
Pass: 42501 |
| A second reversal of the same entry fails | reversal_rules |
Pass: 23505 |
| Driving a wallet negative fails via the CHECK | a_wallet_cannot_go_negative |
Pass: 23514 |
| Migration up → down → up works on an empty DB | migrations_go_up_down_and_up |
Pass |
Other tests: a valid posting commits (the positive control, so failures are never caused by missing privileges), amounts must be positive, totals only increase, idempotency keys are unique and well formed, lines need their entry and their account’s currency, account status rules, and the read-only role.
MEZGEB_TEST_DATABASE_URL=postgres://postgres:postgres@localhost:55432/postgres \ cargo test -p mezgeb-engine
test result: ok. 13 passed; 0 failedAcceptance criteria
Section titled “Acceptance criteria”Every invariant marked “DB” in the catalogue has a passing DB test, for the tables that exist in this step: INV-1, INV-3, INV-4, INV-8, INV-9, INV-13, INV-14 and INV-24 each have one. The other DB-marked invariants (INV-10, INV-12’s timing, INV-16, INV-19, INV-20, INV-25, INV-26) depend on tables or behaviour added in later steps, listed with their step in db-schema.md. Pass.
Exit gate
Section titled “Exit gate”Gate: schema review: for each constraint, name the invariant it protects.
Done in db-schema.md: every constraint, trigger and privilege is listed with the invariant it protects and the test that proves it. Result: passed.