Files
buzz/crates/buzz-push-gateway/schema
Luke Tornquist 379b501124 feat(push-gateway): declarative desired-state schema, one file per table (#8328)
## Summary

Gives the push gateway a declarative desired-state schema in the same
layout the relay uses since #8131: a `\i` manifest plus one file per
table, the shape SchemaBot reads.

- `crates/buzz-push-gateway/schema/schema.sql`: an ordered `\i` manifest
and nothing else.
- `crates/buzz-push-gateway/schema/tables/public/<table>.sql`: the end
state of migrations `0001`–`0008` for all six gateway tables. That
includes `0007`'s `installations.platform` column and `0008`'s dropped
delegation → installation foreign key (#9610), so the delegation table
declares no `REFERENCES`. Each file holds only its `CREATE TABLE` and
plain `CREATE [UNIQUE] INDEX` statements.
- `src/postgres.rs`: the six table names are now one `GATEWAY_TABLES`
constant shared by readiness and the runtime-role GRANT, instead of two
hand-copied lists.
- `src/postgres/desired_schema_postgres_tests.rs` adds two tests:
- `desired_state_schema_layout_is_complete_and_schemabot_shaped` (no
infra needed). It checks that the manifest includes every file exactly
once and holds only `\i` lines, that each table file creates exactly one
table, and that indexes use a plain `CREATE INDEX` on that table.
- `desired_state_schema_matches_migrations` (PostgreSQL lane). It builds
one probe database from `schema.sql` through the real `bin/pgschema
apply` and another through `GATEWAY_MIGRATOR`. It then requires the two
`public` catalogs to match: table set, columns from `pg_attribute` (full
type with typmod via `format_type`, default, nullability, identity,
generation, collation), table options (persistence, RLS flags,
`reloptions`), constraints (`pg_get_constraintdef`) and index shapes
(`pg_get_indexdef` + `indoption`). Neither probe may hold functions,
non-internal triggers, policies, views, sequences or user types, and the
declared table set must equal `GATEWAY_TABLES`. It compares the whole
`public` catalog, not a list of tables, so a future migration that
changes `public` tables, columns, table options, constraints or indexes
without a matching `schema/` change fails CI. It doesn't cover data-only
migrations or objects outside `public` (other schemas, roles, database
settings). Probe databases are created from `template0` and dropped even
when an assertion, `pgschema` or setup fails; the original failure is
re-raised unchanged. Connection details, including the password, come
from the resolved `PgConnectOptions`, so percent-encoded credentials,
`?password=` and Unix-socket URLs reach `pgschema` the way SQLx sees
them. `pgschema` 1.7.4 builds an unquoted keyword DSN, so a password or
socket path with whitespace, a quote, `=` or a backslash isn't supported
(noted in the test).

## Not changed

The gateway still migrates itself at startup: bootstrap guard, then SQLx
migrations, then runtime-role grants. The guard and grants are
operational steps, not desired-state DDL, so they stay in code. No
`schemabot.yaml` is added: SchemaBot isn't configured for the relay or
the gateway yet, so the yaml for both waits until that server-side setup
exists. The manifest header notes that the relay's post-pgschema
reconcile step (root `AGENTS.md` gotcha #7) doesn't apply, because the
gateway schema has no seed DML or storage parameters.

SchemaBot compatibility is not proven here. The parity test shows the
files are equivalent under `pgschema`. Since #9610 the gateway schema
has no foreign keys, so the earlier inline-FK caveat is gone.
`docs/database.md` Rule 4 now lists the gateway `schema/` directory as a
schema location.

## What got simpler

To read the gateway's schema today you have to replay eight migrations:
two CHECK rewrites, two dropped UNIQUE constraints, a dropped column, an
added column and a dropped foreign key. After this PR it's one
declarative file per table, in the same convention as the relay. There
is no new runtime path and no new abstraction, and the duplicated table
list in `postgres.rs` collapses to one constant.

## Testing

At `c4336392` (rebased on main after #9610; mirrors gateway
`0007`/`0008`; reads the `pgschema` password from a copy with a URL-safe
host and username, so a `PGHOST` or default-search socket no longer
panics and a `/role` username no longer drops the password):

- `cargo fmt --all -- --check` and `cargo clippy -p buzz-push-gateway
--all-targets -- -D warnings`: pass.
- `cargo test -p buzz-push-gateway --lib desired_schema`: pass,
including
`resolved_password_reads_through_a_socket_host_and_slash_username`.
Removing the username normalization makes that test fail.
- `scripts/postgres-test-run.sh -p buzz-push-gateway --lib --tests`:
17/17 passed, including `desired_state_schema_matches_migrations`.

At `df3ff104` (password from resolved connect options), same
workstation:

- fmt, clippy (`-D warnings`, with and without
`personal-dev-app-attest`): pass. `cargo nextest run -p
buzz-push-gateway`: 80/80, and 82/82 with the feature.
`scripts/postgres-test-run.sh -p buzz-push-gateway --lib --tests`:
13/13.
- `?password=`: with `PGPASSWORD` unset,
`postgres://<role>:wrong@localhost/postgres?password=<encoded
p@ss:w/rd#x>` passes the parity test, so `pgschema` gets the
query-parameter password that SQLx uses. A wrong `?password=` is
rejected (`28P01`).
- The CHECK-bound falsification still fails with `constraint parity
mismatch`. Every run leaves 0 probe databases.

At `f937924a` (probe cleanup and connection-URL follow-up), on a Linux
workstation against PostgreSQL 16 with the repo's pinned `bin/pgschema`:

- `cargo fmt --all -- --check`, plus `cargo clippy -p buzz-push-gateway
--all-targets -- -D warnings` with and without `--features
personal-dev-app-attest`: pass.
- `cargo nextest run -p buzz-push-gateway`: 60/60 passed, and 62/62 with
`personal-dev-app-attest`. `scripts/postgres-test-run.sh -p
buzz-push-gateway --lib --tests`: 12/12 passed.
- Probe cleanup: with a CHECK bound changed (2048→4096), the previous
test code fails and leaves both `push_gateway_{desired,migrated}_*`
databases behind. The new code fails with the same `constraint parity
mismatch` panic and leaves none. A table file that makes `pgschema
apply` fail (`no_such_column`) also reports `pgschema apply failed: ...
column "no_such_column" does not exist` and leaves none.
- Connection URLs: the parity test passes as a role whose password is
`p@ss:w/rd#%x`, given percent-encoded in the URL with `PGPASSWORD`
unset. It also passes over
`postgres://<user>@%2Fvar%2Frun%2Fpostgresql:5432/postgres` (Unix
socket, peer auth).

Earlier, at `ee2745cb78`:

- `scripts/postgres-test-packages.sh` discovers `buzz-push-gateway`. The
parity test lives under a `*_postgres_tests` module so the `postgres-ci`
nextest filter selects it.
- `cargo fmt --all -- --check` and `cargo clippy -p buzz-push-gateway
--all-targets -- -D warnings` both pass.
- `cargo nextest run -p buzz-push-gateway`: 60/60 passed, including the
layout test.
- `scripts/postgres-test-run.sh -p buzz-push-gateway`: 12/12 passed,
including `desired_state_schema_matches_migrations`.
- Falsification checks:
- Dropping the partial predicate from
`push_gateway_installations_active_token` makes the parity test fail
with an index-shape mismatch.
- Adding an unmirrored `0007` migration (`ADD COLUMN probe TEXT`) makes
the parity test fail with a column mismatch. Mirroring the column in the
table file makes it pass again.
- Putting `CREATE TABLE` DDL in `schema.sql` makes the layout test fail.
- A `0007` adding `probe VARCHAR(64)` mirrored as `VARCHAR(128)` fails
with a column mismatch (typmod); mirroring it exactly passes.
- An unmirrored `0007` `ALTER COLUMN expires_at TYPE TIMESTAMP` fails
with a column mismatch.
- An unmirrored `0007` `ENABLE ROW LEVEL SECURITY` fails with a
table-option mismatch.
- A `0007` creating a function and trigger fails the "only tables and
indexes" check.

Known gap: `sqlx::migrate!` doesn't notice a *new* migration file on an
incremental build. The crate has no `build.rs` `rerun-if-changed`, and
neither does any other crate in the workspace. For the `0007` check
above I touched `src/postgres.rs` to force the macro to re-expand. Clean
CI builds aren't affected.





buzz-review-completed

---------

Signed-off-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 15:50:39 -05:00
..