Add PostgreSQL foundation, UUIDv7 IDs and coverage policy #39

Merged
amiryahaya merged 6 commits from feat/8-postgres-foundation into main 2026-10-22 08:11:59 +02:00
Owner

Summary

Adds canonical conversation/message persistence for #8 with separate migration and runtime identities.

migrate (rag_migrator) → embedded versioned migrations
API (rag_app) → verify schema + restricted role → bounded pool
  ConversationRepository → scoped transaction → FORCE RLS
    append → allocate sequence + insert atomically
    history → indexed newest-N → chronological records

Migration 0001 stays unchanged; 0002 adds lifecycle/ordering invariants and RLS. Runtime startup rejects missing/tampered migrations, table ownership, role memberships and excess privileges. Readiness probes are bounded; shutdown drains and closes storage. CI now requires real PostgreSQL 17 contracts.

Repository owner selectors are query scope, not identity proofs. Trusted identity/ABAC and chat routes remain subsequent issues. Initial appends require the parent's exact marking/policy version.

User-requested SRS FR-DATA-006 standardizes application-generated IDs on UUIDv7 throughout. PostgreSQL 17 stores UUIDv7; Rust generates it. Forward migration 0003 removes UUIDv4 defaults and rejects new/changed primary IDs of other versions/variants. Existing IDs/references remain intact; audit correlation/outbox aggregate columns use native UUID.

Closes #8.

Evidence

  • Before: inactive-message RLS regression returned one row; startup accepted audit-table TRUNCATE and server-program role membership; a hanging readiness probe exceeded its deadline.
    After: all regressions pass; 14 real PostgreSQL tests cover migrations/checksums, concurrent sequences, cancellation/rollback, owner/org isolation, indexed bounded history, non-bypass RLS, role restrictions, pool exhaustion and dependency recovery.
  • cargo fmt --all -- --check; cargo clippy --workspace --all-targets --locked -- -D warnings; cargo test --workspace --locked: pass (20 behavioral tests and 6 compile-fail doctests; service tests explicitly run separately).
  • bash scripts/test-postgres.sh --local: 14 passed, zero skipped.
  • npm run typecheck && npm run build: pass. Compose validation and normalized actionlint pass.
  • Built-process smoke: unmigrated startup rejected; separate migration CLI succeeded; live/ready returned 200; SIGTERM exited cleanly.
  • Independent review findings addressed with failing-then-passing database regressions. Hosted branch and PR CI passed at e12e70a78a209359f0da985336f26d4b6930b478, including all 14 required PostgreSQL contracts: branch run 17, PR run 18.

Merge Danger

Door: one-way

Migrations 0002/0003 change database invariants, grants, identifier defaults and row visibility; audit existing data/grants and back up before applying. Legacy rows with incomplete markings are quarantined. Migration 0003 fails transactionally on legacy non-UUID audit correlation/outbox aggregate text; resolve those references explicitly before retrying. Existing primary IDs are never regenerated. API now requires a separately migrated database and restricted runtime credentials. Bootstrap must be applied explicitly to existing development volumes; see docs/operations/postgres.md.

Blast Radius: persistence

CI provisions PostgreSQL 17 from the signed official PGDG repository inside each isolated Debian job container, tests over loopback and always stops the cluster. Pinned runner source confirmed missing service-port context and ignored workflow host mappings; shared runner settings and host ports are unchanged. UUIDv7 regression first returned version 4 instead of 7; it now passes, along with database guards and legacy migration/repair tests.

SRS NFR-MAINT-007 now requires at least 80% line coverage, measured separately for Rust and frontend, with explicit reasons/risk/remediation for shortfalls. Rust application-only coverage is 87.29% (632/724 lines) from unit/contracts plus all 14 instrumented PostgreSQL tests; the 80% threshold command passed. An existing inline unit test was moved into its own excluded test module; API/migration entry points remain in the denominator.

Frontend coverage shortfall: unmeasured because the scaffold currently has typecheck/build only, with no behavior-test/coverage instrumentation; UI behavior is not covered by those checks. P0 #40 adds meaningful frontend tests and separate backend/frontend coverage enforcement in CI. The current workflow does not yet enforce coverage. Recorded baseline, scope and commands.

## Summary Adds canonical conversation/message persistence for #8 with separate migration and runtime identities. ```text migrate (rag_migrator) → embedded versioned migrations API (rag_app) → verify schema + restricted role → bounded pool ConversationRepository → scoped transaction → FORCE RLS append → allocate sequence + insert atomically history → indexed newest-N → chronological records ``` Migration 0001 stays unchanged; 0002 adds lifecycle/ordering invariants and RLS. Runtime startup rejects missing/tampered migrations, table ownership, role memberships and excess privileges. Readiness probes are bounded; shutdown drains and closes storage. CI now requires real PostgreSQL 17 contracts. Repository owner selectors are query scope, not identity proofs. Trusted identity/ABAC and chat routes remain subsequent issues. Initial appends require the parent's exact marking/policy version. User-requested SRS FR-DATA-006 standardizes application-generated IDs on UUIDv7 throughout. PostgreSQL 17 stores UUIDv7; Rust generates it. Forward migration 0003 removes UUIDv4 defaults and rejects new/changed primary IDs of other versions/variants. Existing IDs/references remain intact; audit correlation/outbox aggregate columns use native UUID. Closes #8. ## Evidence - **Before:** inactive-message RLS regression returned one row; startup accepted audit-table TRUNCATE and server-program role membership; a hanging readiness probe exceeded its deadline. **After:** all regressions pass; 14 real PostgreSQL tests cover migrations/checksums, concurrent sequences, cancellation/rollback, owner/org isolation, indexed bounded history, non-bypass RLS, role restrictions, pool exhaustion and dependency recovery. - `cargo fmt --all -- --check`; `cargo clippy --workspace --all-targets --locked -- -D warnings`; `cargo test --workspace --locked`: pass (20 behavioral tests and 6 compile-fail doctests; service tests explicitly run separately). - `bash scripts/test-postgres.sh --local`: 14 passed, zero skipped. - `npm run typecheck && npm run build`: pass. Compose validation and normalized actionlint pass. - Built-process smoke: unmigrated startup rejected; separate migration CLI succeeded; live/ready returned 200; SIGTERM exited cleanly. - Independent review findings addressed with failing-then-passing database regressions. Hosted branch and PR CI passed at `e12e70a78a209359f0da985336f26d4b6930b478`, including all 14 required PostgreSQL contracts: [branch run 17](https://forgejo.primatekun.tech/primatekuntech/rag-rust/actions/runs/17), [PR run 18](https://forgejo.primatekun.tech/primatekuntech/rag-rust/actions/runs/18). ## Merge Danger **Door:** one-way Migrations 0002/0003 change database invariants, grants, identifier defaults and row visibility; audit existing data/grants and back up before applying. Legacy rows with incomplete markings are quarantined. Migration 0003 fails transactionally on legacy non-UUID audit correlation/outbox aggregate text; resolve those references explicitly before retrying. Existing primary IDs are never regenerated. API now requires a separately migrated database and restricted runtime credentials. Bootstrap must be applied explicitly to existing development volumes; see `docs/operations/postgres.md`. **Blast Radius:** persistence CI provisions PostgreSQL 17 from the signed official PGDG repository inside each isolated Debian job container, tests over loopback and always stops the cluster. Pinned runner source confirmed missing service-port context and ignored workflow host mappings; shared runner settings and host ports are unchanged. UUIDv7 regression first returned version 4 instead of 7; it now passes, along with database guards and legacy migration/repair tests. SRS NFR-MAINT-007 now requires at least **80% line coverage**, measured separately for Rust and frontend, with explicit reasons/risk/remediation for shortfalls. Rust application-only coverage is **87.29% (632/724 lines)** from unit/contracts plus all 14 instrumented PostgreSQL tests; the 80% threshold command passed. An existing inline unit test was moved into its own excluded test module; API/migration entry points remain in the denominator. **Frontend coverage shortfall:** unmeasured because the scaffold currently has typecheck/build only, with no behavior-test/coverage instrumentation; UI behavior is not covered by those checks. P0 #40 adds meaningful frontend tests and separate backend/frontend coverage enforcement in CI. The current workflow does not yet enforce coverage. [Recorded baseline, scope and commands](https://forgejo.primatekun.tech/primatekuntech/rag-rust/src/commit/e12e70a78a209359f0da985336f26d4b6930b478/docs/verification/2026-10-22-coverage.md).
feat(storage): add verified PostgreSQL repositories and least-privilege runtime
Some checks failed
CI / Web checks (push) Successful in 22s
CI / Rust checks (pull_request) Failing after 2m54s
CI / Web checks (pull_request) Successful in 19s
CI / Rust checks (push) Failing after 3m42s
22e81a1237
fix(ci): connect PostgreSQL through the runner bridge gateway
Some checks failed
CI / Web checks (push) Successful in 24s
CI / Rust checks (push) Failing after 2m36s
CI / Web checks (pull_request) Successful in 21s
CI / Rust checks (pull_request) Has been cancelled
0bb9e05820
fix(ci): probe the numeric bridge gateway before database tests
Some checks failed
CI / Web checks (push) Successful in 19s
CI / Rust checks (pull_request) Failing after 14s
CI / Web checks (pull_request) Successful in 19s
CI / Rust checks (push) Failing after 11s
e38ab2513a
fix(ci): provision PostgreSQL inside the isolated job container
All checks were successful
CI / Web checks (push) Successful in 25s
CI / Web checks (pull_request) Successful in 24s
CI / Rust checks (pull_request) Successful in 3m41s
CI / Rust checks (push) Successful in 3m44s
106a125d47
feat(ids): standardize application identifiers on UUIDv7
Some checks failed
CI / Web checks (push) Successful in 20s
CI / Web checks (pull_request) Successful in 38s
CI / Rust checks (push) Successful in 4m12s
CI / Rust checks (pull_request) Failing after 8m40s
f96e6f30a8
docs(testing): require 80 percent coverage and explain shortfalls
All checks were successful
CI / Web checks (push) Successful in 22s
CI / Web checks (pull_request) Successful in 21s
CI / Rust checks (push) Successful in 3m18s
CI / Rust checks (pull_request) Successful in 3m19s
e12e70a78a
amiryahaya changed title from WIP: Add PostgreSQL foundation and scoped conversation repositories to WIP: Add PostgreSQL foundation, UUIDv7 IDs and coverage policy 2026-10-22 07:54:20 +02:00
amiryahaya changed title from WIP: Add PostgreSQL foundation, UUIDv7 IDs and coverage policy to Add PostgreSQL foundation, UUIDv7 IDs and coverage policy 2026-10-22 08:11:52 +02:00
Sign in to join this conversation.
No description provided.