From 379b50112476ceb790823a2ae265d5076b18d4bd Mon Sep 17 00:00:00 2001 From: Luke Tornquist Date: Thu, 19 Nov 2026 15:50:39 -0500 Subject: [PATCH] feat(push-gateway): declarative desired-state schema, one file per table (#8328) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 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/.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://:wrong@localhost/postgres?password=` 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://@%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 --- Cargo.lock | 1 + crates/buzz-push-gateway/Cargo.toml | 1 + crates/buzz-push-gateway/schema/schema.sql | 36 ++ .../tables/public/push_gateway_challenges.sql | 13 + .../public/push_gateway_delegations.sql | 19 + .../push_gateway_delivery_auth_replays.sql | 9 + .../push_gateway_delivery_request_replays.sql | 9 + .../public/push_gateway_endpoint_quotas.sql | 9 + .../public/push_gateway_installations.sql | 25 ++ crates/buzz-push-gateway/src/postgres.rs | 35 +- .../postgres/desired_schema_postgres_tests.rs | 397 ++++++++++++++++++ docs/database.md | 2 +- docs/push-gateway-deployment.md | 2 +- 13 files changed, 539 insertions(+), 19 deletions(-) create mode 100644 crates/buzz-push-gateway/schema/schema.sql create mode 100644 crates/buzz-push-gateway/schema/tables/public/push_gateway_challenges.sql create mode 100644 crates/buzz-push-gateway/schema/tables/public/push_gateway_delegations.sql create mode 100644 crates/buzz-push-gateway/schema/tables/public/push_gateway_delivery_auth_replays.sql create mode 100644 crates/buzz-push-gateway/schema/tables/public/push_gateway_delivery_request_replays.sql create mode 100644 crates/buzz-push-gateway/schema/tables/public/push_gateway_endpoint_quotas.sql create mode 100644 crates/buzz-push-gateway/schema/tables/public/push_gateway_installations.sql create mode 100644 crates/buzz-push-gateway/src/postgres/desired_schema_postgres_tests.rs diff --git a/Cargo.lock b/Cargo.lock index 312d706843..4d70e1a38b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1412,6 +1412,7 @@ dependencies = [ "nostr 0.44.7", "openssl", "p256", + "percent-encoding", "proptest", "rand 0.10.1", "reqwest 0.13.4", diff --git a/crates/buzz-push-gateway/Cargo.toml b/crates/buzz-push-gateway/Cargo.toml index 3732db9dd8..bb8c467fc2 100644 --- a/crates/buzz-push-gateway/Cargo.toml +++ b/crates/buzz-push-gateway/Cargo.toml @@ -54,5 +54,6 @@ uuid = { workspace = true } [dev-dependencies] openssl = { version = "0.10", features = ["vendored"] } ciborium = "0.2" +percent-encoding = "2.3" proptest = { workspace = true } reqwest = { workspace = true } diff --git a/crates/buzz-push-gateway/schema/schema.sql b/crates/buzz-push-gateway/schema/schema.sql new file mode 100644 index 0000000000..45d9b517bc --- /dev/null +++ b/crates/buzz-push-gateway/schema/schema.sql @@ -0,0 +1,36 @@ +-- Push gateway desired-state database manifest. +-- +-- The gateway's authority store is deployment-global and lives in its own +-- dedicated database, outside relay community tenancy. This directory declares +-- its end state; `../migrations/` remains the incremental path the gateway +-- binary applies at startup. +-- `src/postgres/desired_schema_postgres_tests.rs` proves that, applied through +-- `pgschema`, the two produce the same `public` tables, columns, table options, +-- constraints, and indexes. Like the migrated schema, it holds no foreign +-- keys (docs/database.md Rule 2). +-- +-- `tables/public/
.sql` declares one table per file: its CREATE TABLE +-- and the CREATE INDEX statements on it, the one-table-per-file shape SchemaBot +-- reads. `tables/` is the SchemaBot schema directory and `public` is its +-- namespace, so nothing else may live under it. The gateway has no types, +-- functions, triggers, or seed rows. Its pre-migration bootstrap guard and +-- runtime-role grants are operational steps in `src/postgres.rs`, not schema. +-- It also has nothing pgschema cannot express (no seed DML, no storage +-- parameters), so the relay's `scripts/reconcile-schema-after-pgschema.sql` +-- step (root `AGENTS.md` gotcha #7) does not apply to it. +-- +-- `./bin/pgschema apply --file crates/buzz-push-gateway/schema/schema.sql` +-- resolves the `\i` includes relative to this file. Raw `psql` resolves `\i` +-- against the working directory instead, so run it from here: +-- `cd crates/buzz-push-gateway/schema && psql -f schema.sql`. +-- +-- Adding a table: add `tables/public/
.sql` and include it below. +-- This file holds only `\i` lines. The layout +-- test in `src/postgres/desired_schema_postgres_tests.rs` enforces both. + +\i tables/public/push_gateway_challenges.sql +\i tables/public/push_gateway_installations.sql +\i tables/public/push_gateway_delegations.sql +\i tables/public/push_gateway_endpoint_quotas.sql +\i tables/public/push_gateway_delivery_auth_replays.sql +\i tables/public/push_gateway_delivery_request_replays.sql diff --git a/crates/buzz-push-gateway/schema/tables/public/push_gateway_challenges.sql b/crates/buzz-push-gateway/schema/tables/public/push_gateway_challenges.sql new file mode 100644 index 0000000000..0ada41a534 --- /dev/null +++ b/crates/buzz-push-gateway/schema/tables/public/push_gateway_challenges.sql @@ -0,0 +1,13 @@ +-- Single-use App Attest enrollment challenges. Only the hash is stored. +CREATE TABLE push_gateway_challenges ( + id UUID PRIMARY KEY, + challenge_hash BYTEA NOT NULL CHECK (length(challenge_hash) = 32), + expires_at TIMESTAMPTZ NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE INDEX push_gateway_challenges_expiry ON push_gateway_challenges (expires_at); + +-- The unauthenticated challenge route applies a deployment-global rolling +-- issuance quota. Keep its count query bounded as challenge volume grows. +CREATE INDEX push_gateway_challenges_created_at ON push_gateway_challenges (created_at); diff --git a/crates/buzz-push-gateway/schema/tables/public/push_gateway_delegations.sql b/crates/buzz-push-gateway/schema/tables/public/push_gateway_delegations.sql new file mode 100644 index 0000000000..252a8b61b7 --- /dev/null +++ b/crates/buzz-push-gateway/schema/tables/public/push_gateway_delegations.sql @@ -0,0 +1,19 @@ +-- An installation's delegation to one relay signing key. Installations may +-- authorize multiple relay deployments, one row per relay key. No foreign key +-- to the installation (docs/database.md Rule 2): the gateway checks it in the +-- writing transaction. +CREATE TABLE push_gateway_delegations ( + id UUID PRIMARY KEY, + installation_id UUID NOT NULL, + relay_pubkey BYTEA NOT NULL CHECK (length(relay_pubkey) = 32), + endpoint_epoch BIGINT NOT NULL CHECK (endpoint_epoch > 0), + generation BIGINT NOT NULL CHECK (generation > 0), + not_before TIMESTAMPTZ NOT NULL, + expires_at TIMESTAMPTZ NOT NULL, + revoked_at TIMESTAMPTZ, + updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), + UNIQUE (installation_id, relay_pubkey), + CHECK (not_before < expires_at) +); + +CREATE INDEX push_gateway_delegations_expiry ON push_gateway_delegations (expires_at) WHERE revoked_at IS NULL; diff --git a/crates/buzz-push-gateway/schema/tables/public/push_gateway_delivery_auth_replays.sql b/crates/buzz-push-gateway/schema/tables/public/push_gateway_delivery_auth_replays.sql new file mode 100644 index 0000000000..0a16dbf721 --- /dev/null +++ b/crates/buzz-push-gateway/schema/tables/public/push_gateway_delivery_auth_replays.sql @@ -0,0 +1,9 @@ +-- Replay ledger for relay delivery auth events, retained until expiry. +CREATE TABLE push_gateway_delivery_auth_replays ( + relay_pubkey BYTEA NOT NULL CHECK (length(relay_pubkey) = 32), + auth_event_id BYTEA NOT NULL CHECK (length(auth_event_id) = 32), + expires_at TIMESTAMPTZ NOT NULL, + PRIMARY KEY (relay_pubkey, auth_event_id) +); + +CREATE INDEX push_gateway_delivery_auth_replays_expiry ON push_gateway_delivery_auth_replays (expires_at); diff --git a/crates/buzz-push-gateway/schema/tables/public/push_gateway_delivery_request_replays.sql b/crates/buzz-push-gateway/schema/tables/public/push_gateway_delivery_request_replays.sql new file mode 100644 index 0000000000..101b98a372 --- /dev/null +++ b/crates/buzz-push-gateway/schema/tables/public/push_gateway_delivery_request_replays.sql @@ -0,0 +1,9 @@ +-- Replay ledger for relay delivery request ids, retained until expiry. +CREATE TABLE push_gateway_delivery_request_replays ( + relay_pubkey BYTEA NOT NULL CHECK (length(relay_pubkey) = 32), + request_id UUID NOT NULL, + expires_at TIMESTAMPTZ NOT NULL, + PRIMARY KEY (relay_pubkey, request_id) +); + +CREATE INDEX push_gateway_delivery_request_replays_expiry ON push_gateway_delivery_request_replays (expires_at); diff --git a/crates/buzz-push-gateway/schema/tables/public/push_gateway_endpoint_quotas.sql b/crates/buzz-push-gateway/schema/tables/public/push_gateway_endpoint_quotas.sql new file mode 100644 index 0000000000..f1b55551d0 --- /dev/null +++ b/crates/buzz-push-gateway/schema/tables/public/push_gateway_endpoint_quotas.sql @@ -0,0 +1,9 @@ +-- Per-APNs-token delivery admission window. +CREATE TABLE push_gateway_endpoint_quotas ( + token_fingerprint BYTEA PRIMARY KEY CHECK (length(token_fingerprint) = 32), + window_started_at TIMESTAMPTZ NOT NULL, + admitted BIGINT NOT NULL CHECK (admitted >= 0), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE INDEX push_gateway_endpoint_quotas_updated ON push_gateway_endpoint_quotas (updated_at); diff --git a/crates/buzz-push-gateway/schema/tables/public/push_gateway_installations.sql b/crates/buzz-push-gateway/schema/tables/public/push_gateway_installations.sql new file mode 100644 index 0000000000..74ecdbaae4 --- /dev/null +++ b/crates/buzz-push-gateway/schema/tables/public/push_gateway_installations.sql @@ -0,0 +1,25 @@ +-- Attested device installations and their encrypted push tokens. platform +-- names the push service (APNs or FCM) that delivers the installation's wakes. +-- Revoked installations remain as retry tombstones until authority expiry, so +-- ownership uniqueness covers live rows only: a replacement can enroll without +-- deleting the old installation's idempotency state. +CREATE TABLE push_gateway_installations ( + id UUID PRIMARY KEY, + app_attest_key_id BYTEA NOT NULL CHECK (octet_length(app_attest_key_id) BETWEEN 1 AND 128), + app_attest_public_key BYTEA NOT NULL CHECK (octet_length(app_attest_public_key) BETWEEN 33 AND 256), + assertion_counter BIGINT NOT NULL CHECK (assertion_counter BETWEEN 0 AND 4294967295), + token_ciphertext BYTEA NOT NULL CHECK (octet_length(token_ciphertext) BETWEEN 1 AND 2048), + token_fingerprint BYTEA NOT NULL CHECK (length(token_fingerprint) = 32), + endpoint_epoch BIGINT NOT NULL CHECK (endpoint_epoch > 0), + expires_at TIMESTAMPTZ NOT NULL, + revoked_at TIMESTAMPTZ, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), + platform TEXT NOT NULL DEFAULT 'ios' CHECK (platform IN ('ios', 'android')) +); + +CREATE INDEX push_gateway_installations_expiry ON push_gateway_installations (expires_at) WHERE revoked_at IS NULL; + +CREATE UNIQUE INDEX push_gateway_installations_active_app_attest_key ON push_gateway_installations (app_attest_key_id) WHERE revoked_at IS NULL; + +CREATE UNIQUE INDEX push_gateway_installations_active_token ON push_gateway_installations (token_fingerprint) WHERE revoked_at IS NULL; diff --git a/crates/buzz-push-gateway/src/postgres.rs b/crates/buzz-push-gateway/src/postgres.rs index 7b068bacac..9a848bb6c3 100644 --- a/crates/buzz-push-gateway/src/postgres.rs +++ b/crates/buzz-push-gateway/src/postgres.rs @@ -10,8 +10,23 @@ use uuid::Uuid; #[path = "postgres/bootstrap_tests.rs"] mod bootstrap_postgres_tests; +#[cfg(test)] +#[path = "postgres/desired_schema_postgres_tests.rs"] +mod desired_schema_postgres_tests; + static GATEWAY_MIGRATOR: sqlx::migrate::Migrator = sqlx::migrate!("./migrations"); +/// Every gateway authority table. Readiness probes and runtime-role grants +/// cover exactly this set; the desired-state parity test pins it to `schema/`. +const GATEWAY_TABLES: [&str; 6] = [ + "push_gateway_challenges", + "push_gateway_installations", + "push_gateway_delegations", + "push_gateway_endpoint_quotas", + "push_gateway_delivery_auth_replays", + "push_gateway_delivery_request_replays", +]; + #[cfg(test)] async fn migrate_gateway_through( pool: &PgPool, @@ -58,14 +73,8 @@ impl PostgresAuthorityStore { REVOKE CREATE ON SCHEMA public FROM {role}; GRANT CONNECT ON DATABASE {database} TO {role}; GRANT USAGE ON SCHEMA public TO {role}; - GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE - push_gateway_challenges, - push_gateway_installations, - push_gateway_delegations, - push_gateway_endpoint_quotas, - push_gateway_delivery_auth_replays, - push_gateway_delivery_request_replays - TO {role};" + GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE {tables} TO {role};", + tables = GATEWAY_TABLES.join(", "), ); sqlx::raw_sql(AssertSqlSafe(grants)).execute(pool).await?; Ok(()) @@ -93,16 +102,8 @@ fn bytes32(v: Vec) -> Result<[u8; 32], AuthorityError> { #[async_trait] impl AuthorityStore for PostgresAuthorityStore { async fn ready(&self) -> Result<(), AuthorityError> { - const TABLES: [&str; 6] = [ - "push_gateway_challenges", - "push_gateway_installations", - "push_gateway_delegations", - "push_gateway_endpoint_quotas", - "push_gateway_delivery_auth_replays", - "push_gateway_delivery_request_replays", - ]; let mut tx = self.pool.begin().await.map_err(db)?; - for table in TABLES { + for table in GATEWAY_TABLES { let ready: bool = sqlx::query_scalar( "SELECT to_regclass($1) IS NOT NULL AND COALESCE(has_table_privilege(current_user, to_regclass($1), 'SELECT'), false) diff --git a/crates/buzz-push-gateway/src/postgres/desired_schema_postgres_tests.rs b/crates/buzz-push-gateway/src/postgres/desired_schema_postgres_tests.rs new file mode 100644 index 0000000000..e888acc99f --- /dev/null +++ b/crates/buzz-push-gateway/src/postgres/desired_schema_postgres_tests.rs @@ -0,0 +1,397 @@ +//! `schema/` is the gateway's desired-state schema in the one-table-per-file +//! shape SchemaBot reads; `migrations/` is the incremental path the binary +//! applies at startup. These tests keep the two from drifting apart. +use super::*; +use sqlx::postgres::PgConnectOptions; +use sqlx::ConnectOptions; +use std::collections::BTreeSet; +use std::path::{Path, PathBuf}; + +fn schema_dir() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("schema") +} + +fn strip_comments(text: &str) -> String { + text.lines() + .map(|line| line.split_once("--").map_or(line, |(code, _)| code)) + .collect::>() + .join("\n") +} + +/// `schema/schema.sql` includes every file under `schema/` exactly once and +/// holds nothing but `\i` lines. `schema/tables/public/
.sql` declares +/// exactly that table plus plain `CREATE [UNIQUE] INDEX ... ON
` +/// statements (SchemaBot refuses `IF NOT EXISTS` and `CONCURRENTLY`). +#[test] +fn desired_state_schema_layout_is_complete_and_schemabot_shaped() { + fn sql_files(dir: &Path, root: &Path, out: &mut BTreeSet) { + for entry in std::fs::read_dir(dir).expect("read schema directory") { + let path = entry.expect("schema directory entry").path(); + if path.is_dir() { + sql_files(&path, root, out); + } else { + let relative = path.strip_prefix(root).expect("schema-relative path"); + out.insert(relative.to_str().expect("utf-8 path").replace('\\', "/")); + } + } + } + + let dir = schema_dir(); + let manifest = std::fs::read_to_string(dir.join("schema.sql")).expect("read schema.sql"); + let mut included = Vec::new(); + for line in strip_comments(&manifest).lines().map(str::trim) { + if line.is_empty() { + continue; + } + let include = line.strip_prefix("\\i ").unwrap_or_else(|| { + panic!( + "schema/schema.sql may only hold \\i includes; \ + put table DDL in tables/public/
.sql: {line:.80}" + ) + }); + included.push(include.trim().to_owned()); + } + let included_set: BTreeSet<_> = included.iter().cloned().collect(); + assert_eq!( + included.len(), + included_set.len(), + "schema.sql includes a file twice" + ); + + let mut on_disk = BTreeSet::new(); + sql_files(&dir, &dir, &mut on_disk); + on_disk.remove("schema.sql"); + assert_eq!( + on_disk, included_set, + "every schema/ file must be included by schema/schema.sql exactly once" + ); + + for file in &included { + let table = file + .strip_prefix("tables/public/") + .and_then(|name| name.strip_suffix(".sql")) + .unwrap_or_else(|| { + panic!("{file}: the gateway schema holds only tables/public/
.sql") + }); + let text = std::fs::read_to_string(dir.join(file)).expect("read table file"); + let mut creates = 0; + for statement in strip_comments(&text) + .split(';') + .map(|s| s.split_whitespace().collect::>().join(" ")) + .filter(|s| !s.is_empty()) + { + if let Some(rest) = statement.strip_prefix("CREATE TABLE ") { + assert!( + rest.starts_with(&format!("{table} (")), + "{file} may only create table {table}: {statement:.80}" + ); + creates += 1; + continue; + } + let index = statement + .strip_prefix("CREATE INDEX ") + .or_else(|| statement.strip_prefix("CREATE UNIQUE INDEX ")) + .unwrap_or_else(|| { + panic!("{file} may only hold CREATE TABLE/INDEX: {statement:.80}") + }); + assert!( + !index.starts_with("IF NOT EXISTS ") && !index.starts_with("CONCURRENTLY "), + "{file} must use a plain CREATE INDEX: {statement:.80}" + ); + assert!( + index.contains(&format!(" ON {table} ")), + "{file} may only index {table}: {statement:.80}" + ); + } + assert_eq!(creates, 1, "{file} must create exactly one table"); + } + assert!( + !included.is_empty(), + "schema/tables/public declares no tables" + ); +} + +/// The password SQLx resolved for `options`, which has no public getter. +/// Serializes a copy with a URL-safe host and username only to read the +/// password back. SQLx's URL builder interpolates both raw: a socket-directory +/// host found through `PGHOST` or SQLx's default socket search makes +/// `to_url_lossy` panic, and a username such as `/role` drops the password +/// silently. The caller's `options` keep both. +fn resolved_password(options: &PgConnectOptions) -> String { + let url = options + .clone() + .host("localhost") + .username("probe") + .to_url_lossy(); + percent_encoding::percent_decode_str(url.password().unwrap_or_default()) + .decode_utf8() + .expect("database password utf-8") + .into_owned() +} + +#[test] +fn resolved_password_reads_through_a_socket_host_and_slash_username() { + let options = PgConnectOptions::new_without_pgpass() + .host("/var/run/postgresql") + .username("/gateway-test") + .password("p@ss:/#% word"); + assert_eq!(resolved_password(&options), "p@ss:/#% word"); + assert_eq!(options.get_host(), "/var/run/postgresql"); + assert_eq!(options.get_username(), "/gateway-test"); +} + +/// One probe database is bootstrapped from `schema/schema.sql` through the +/// real `bin/pgschema apply` binary (the SchemaBot-equivalent desired-state +/// path); another runs every gateway migration. Their `public` catalogs must +/// match: the same tables, column shapes (full type with typmod, default, +/// nullability, identity, generation, collation; keyed by name, since +/// migrations drop and append columns), table options, constraints, and index +/// shapes including per-key `indoption`. Neither side may hold functions, +/// triggers, policies, views, sequences, or user types. So an unmirrored +/// migration that changes `public` tables, columns, table options, +/// constraints, or indexes fails here. Data-only migrations and objects +/// outside `public` (other schemas, roles, database settings) are not +/// compared. This proves equivalence under `pgschema`, not full SchemaBot +/// support. +#[tokio::test] +#[ignore = "requires PostgreSQL"] +async fn desired_state_schema_matches_migrations() { + type Rows = Vec>>; + async fn catalog(pool: &PgPool, sql: &str) -> Rows { + sqlx::query(AssertSqlSafe(sql.to_owned())) + .fetch_all(pool) + .await + .expect("read catalog") + .iter() + .map(|row| { + (0..row.len()) + .map(|i| row.get::, _>(i)) + .collect() + }) + .collect() + } + const TABLES: &str = "SELECT tablename::text FROM pg_tables \ + WHERE schemaname = 'public' AND tablename <> '_sqlx_migrations' ORDER BY 1"; + // `pg_attribute` rather than `information_schema.columns`: the latter's + // `data_type` drops typmods and array element types (`varchar(64)` and + // `varchar(128)` both read `character varying`). + const COLUMNS: &str = "SELECT t.relname::text, a.attname::text, \ + format_type(a.atttypid, a.atttypmod), a.attnotnull::text, \ + pg_get_expr(d.adbin, d.adrelid), a.attidentity::text, a.attgenerated::text, \ + a.attcollation::regcollation::text \ + FROM pg_attribute a JOIN pg_class t ON t.oid = a.attrelid \ + JOIN pg_namespace n ON n.oid = t.relnamespace \ + LEFT JOIN pg_attrdef d ON d.adrelid = a.attrelid AND d.adnum = a.attnum \ + WHERE n.nspname = 'public' AND t.relkind = 'r' AND t.relname <> '_sqlx_migrations' \ + AND a.attnum > 0 AND NOT a.attisdropped ORDER BY 1, 2"; + const TABLE_OPTIONS: &str = "SELECT t.relname::text, t.relpersistence::text, \ + t.relrowsecurity::text, t.relforcerowsecurity::text, t.reloptions::text \ + FROM pg_class t JOIN pg_namespace n ON n.oid = t.relnamespace \ + WHERE n.nspname = 'public' AND t.relkind = 'r' AND t.relname <> '_sqlx_migrations' \ + ORDER BY 1"; + const CONSTRAINTS: &str = "SELECT t.relname::text, c.conname::text, c.contype::text, \ + pg_get_constraintdef(c.oid) \ + FROM pg_constraint c JOIN pg_class t ON t.oid = c.conrelid \ + JOIN pg_namespace n ON n.oid = t.relnamespace \ + WHERE n.nspname = 'public' AND t.relname <> '_sqlx_migrations' ORDER BY 1, 2"; + const INDEXES: &str = "SELECT t.relname::text, c.relname::text, \ + pg_get_indexdef(i.indexrelid), i.indoption::int2[]::text \ + FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid \ + JOIN pg_class t ON t.oid = i.indrelid \ + JOIN pg_namespace n ON n.oid = t.relnamespace \ + WHERE n.nspname = 'public' AND t.relname <> '_sqlx_migrations' ORDER BY 1, 2"; + // `schema/` may only declare tables and indexes, so neither path may + // create anything else in `public`: SchemaBot would never reproduce it. + const NON_TABLE_OBJECTS: &str = "SELECT 'function', p.proname::text FROM pg_proc p \ + JOIN pg_namespace n ON n.oid = p.pronamespace WHERE n.nspname = 'public' \ + UNION ALL SELECT 'trigger', tg.tgname::text FROM pg_trigger tg \ + JOIN pg_class t ON t.oid = tg.tgrelid JOIN pg_namespace n ON n.oid = t.relnamespace \ + WHERE n.nspname = 'public' AND NOT tg.tgisinternal \ + UNION ALL SELECT 'policy', pol.polname::text FROM pg_policy pol \ + JOIN pg_class t ON t.oid = pol.polrelid JOIN pg_namespace n ON n.oid = t.relnamespace \ + WHERE n.nspname = 'public' \ + UNION ALL SELECT 'relation', c.relname::text FROM pg_class c \ + JOIN pg_namespace n ON n.oid = c.relnamespace \ + WHERE n.nspname = 'public' AND c.relkind NOT IN ('r', 'i') \ + UNION ALL SELECT 'type', ty.typname::text FROM pg_type ty \ + JOIN pg_namespace n ON n.oid = ty.typnamespace \ + WHERE n.nspname = 'public' AND ty.typtype IN ('d', 'e', 'r', 'm') \ + ORDER BY 1, 2"; + + // pgschema takes discrete connection flags. `PgConnectOptions` decodes the + // URL the way the gateway does, including a Unix-socket host, which + // pgschema accepts as a socket directory in `--host` like libpq does. + // The password comes from the resolved options, so the userinfo, + // `?password=`, `PGPASSWORD` and `.pgpass` precedence matches SQLx's. + // pgschema 1.7.4 joins these flags into an unquoted keyword DSN, so a + // password or socket path containing whitespace, a quote, `=` or a + // backslash is not supported here. + let base_url = std::env::var("BUZZ_TEST_DATABASE_URL").expect("test database URL"); + let options: PgConnectOptions = base_url.parse().expect("parse test database URL"); + let host = match options.get_socket() { + Some(dir) => dir.to_str().expect("socket directory utf-8").to_owned(), + None => options.get_host().to_owned(), + }; + let password = resolved_password(&options); + let connection = PgschemaConnection { + host, + port: options.get_port().to_string(), + user: options.get_username().to_owned(), + password, + }; + let admin = PgPool::connect_with(options.clone()) + .await + .expect("connect admin database"); + + let suffix = Uuid::new_v4().simple(); + let probes = [ + format!("push_gateway_desired_{suffix}"), + format!("push_gateway_migrated_{suffix}"), + ]; + // Run the comparison in its own task so a failed assertion, pgschema run + // or probe setup still reaches the cleanup below; the task's panic is + // re-raised unchanged afterwards. + let outcome = tokio::spawn(compare_probes( + admin.clone(), + options, + connection, + probes.clone(), + )) + .await; + // Attempt every drop before reporting any failure, so one failed drop + // never leaks the other probe. + let mut drop_failures = Vec::new(); + for db in &probes { + if let Err(error) = sqlx::query(AssertSqlSafe(format!( + "DROP DATABASE IF EXISTS {db} WITH (FORCE)" + ))) + .execute(&admin) + .await + { + drop_failures.push(format!("drop probe database {db}: {error}")); + } + } + match outcome { + Err(failure) => { + for message in &drop_failures { + eprintln!("{message}"); + } + std::panic::resume_unwind(failure.into_panic()); + } + Ok(()) => assert!(drop_failures.is_empty(), "{}", drop_failures.join("; ")), + } + + struct PgschemaConnection { + host: String, + port: String, + user: String, + password: String, + } + + async fn compare_probes( + admin: PgPool, + options: PgConnectOptions, + connection: PgschemaConnection, + [desired_db, migrated_db]: [String; 2], + ) { + // `template0` so neither probe inherits objects from `template1`. + for db in [&desired_db, &migrated_db] { + sqlx::query(AssertSqlSafe(format!( + "CREATE DATABASE {db} TEMPLATE template0" + ))) + .execute(&admin) + .await + .expect("create probe database"); + } + + let PgschemaConnection { + host, + port, + user, + password, + } = &connection; + let pgschema = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../bin/pgschema"); + let schema_file = schema_dir().join("schema.sql"); + let apply = std::process::Command::new(&pgschema) + .args([ + "apply", + "--auto-approve", + "--file", + schema_file.to_str().expect("schema path utf-8"), + "--host", + host, + "--port", + port, + "--user", + user, + "--password", + password, + "--db", + &desired_db, + "--plan-host", + host, + "--plan-port", + port, + "--plan-user", + user, + "--plan-password", + password, + "--plan-db", + &desired_db, + ]) + .output() + .expect("run bin/pgschema apply (hermit env required)"); + assert!( + apply.status.success(), + "pgschema apply failed: {}\n{}", + String::from_utf8_lossy(&apply.stdout), + String::from_utf8_lossy(&apply.stderr), + ); + + let desired = PgPool::connect_with(options.clone().database(&desired_db)) + .await + .expect("connect desired-state probe"); + let migrated = PgPool::connect_with(options.database(&migrated_db)) + .await + .expect("connect migrated probe"); + GATEWAY_MIGRATOR + .run(&migrated) + .await + .expect("apply gateway migrations"); + + let declared: BTreeSet = catalog(&desired, TABLES) + .await + .into_iter() + .filter_map(|row| row.into_iter().next().flatten()) + .collect(); + let served: BTreeSet = GATEWAY_TABLES.iter().map(|t| t.to_string()).collect(); + assert_eq!( + declared, served, + "schema/ must declare exactly GATEWAY_TABLES, the set readiness probes and \ + runtime grants cover" + ); + for pool in [&desired, &migrated] { + assert_eq!( + catalog(pool, NON_TABLE_OBJECTS).await, + Rows::new(), + "the gateway schema holds only tables and indexes" + ); + } + for (what, sql) in [ + ("table set", TABLES), + ("column", COLUMNS), + ("table option", TABLE_OPTIONS), + ("constraint", CONSTRAINTS), + ("index", INDEXES), + ] { + assert_eq!( + catalog(&desired, sql).await, + catalog(&migrated, sql).await, + "{what} parity mismatch: crates/buzz-push-gateway/schema/ has drifted from \ + migrations/; update the table file to the post-migration end state" + ); + } + desired.close().await; + migrated.close().await; + } +} diff --git a/docs/database.md b/docs/database.md index 43d62cdf23..1b65037386 100644 --- a/docs/database.md +++ b/docs/database.md @@ -270,7 +270,7 @@ apply: | `schema/schema.sql` | An `\i` line for each new file, after the tables it depends on. | | `schema/types/`, `schema/partitions/`, `schema/seeds/` | The matching non-table objects. The layout test permits nothing else here. Rule 1 forbids any addition to `functions/` and `triggers/`. | | `scripts/reconcile-schema-after-pgschema.sql` | Idempotent convergence, plus a live assertion, for anything `pgschema` does not preserve: seed rows, some storage parameters, partition attachment, and the shapes listed in AGENTS.md *Common Gotchas* #7. | -| `crates/buzz-push-gateway/migrations/` **and** the relay copies | The six `push_gateway_*` tables have two homes. The gateway applies its own migration history to its own database, through a pre-install/pre-upgrade Helm Job ([push-gateway-deployment.md](push-gateway-deployment.md)). The relay schema also defines them: `migrations/` (`0015`, `0043`, `0045`, `0060`), `schema/tables/public/push_gateway_*.sql`, and `schema/seeds/push_gateway_*.sql`. Until the duplication is removed, a gateway schema change goes in both, as relay `0060` mirrors gateway `0006`. Every rule here applies to both. The target is for the gateway to own its tables alone, in its own migrations and declarative schema; do not add new gateway tables to the relay schema. | +| `crates/buzz-push-gateway/migrations/`, `crates/buzz-push-gateway/schema/`, **and** the relay copies | The six `push_gateway_*` tables have two homes. The gateway applies its own migration history to its own database, through a pre-install/pre-upgrade Helm Job ([push-gateway-deployment.md](push-gateway-deployment.md)). `crates/buzz-push-gateway/schema/tables/public/` holds the gateway's declarative end state, one table per file; its parity test fails when it drifts from the gateway migrations. The relay schema also defines them: `migrations/` (`0015`, `0043`, `0045`, `0060`), `schema/tables/public/push_gateway_*.sql`, and `schema/seeds/push_gateway_*.sql`. Until the duplication is removed, a gateway schema change goes in both, as relay `0060` mirrors gateway `0006`. Every rule here applies to both. The target is for the gateway to own its tables alone, in its own migrations and declarative schema; do not add new gateway tables to the relay schema. | A fresh desired-state database and a migrated database MUST converge on the same live catalog. The desired-state layout test in diff --git a/docs/push-gateway-deployment.md b/docs/push-gateway-deployment.md index 925cbf4e91..c675a487d0 100644 --- a/docs/push-gateway-deployment.md +++ b/docs/push-gateway-deployment.md @@ -159,7 +159,7 @@ that app identity; omitting it is not a push shutdown mechanism. The gateway's dedicated pool does not consume the relay-oriented `BUZZ_DB_LOCK_TIMEOUT_MS`, `BUZZ_DB_IDLE_TXN_TIMEOUT_MS`, or `BUZZ_DB_STATEMENT_TIMEOUT_MS` settings. Its session-timeout policy remains separate from the `buzz-db` writer policy and must be designed and rolled out independently. -All replicas must share one PostgreSQL database. Delivery authority, replay admission, and endpoint quota reservation are transactional there, so replica count does not multiply the abuse ceiling. The gateway owns a scoped migration history under `crates/buzz-push-gateway/migrations`; it creates only the six `push_gateway_*` authority tables plus SQLx's migration-history table and never runs relay migrations. +All replicas must share one PostgreSQL database. Delivery authority, replay admission, and endpoint quota reservation are transactional there, so replica count does not multiply the abuse ceiling. The gateway owns a scoped migration history under `crates/buzz-push-gateway/migrations`; it creates only the six `push_gateway_*` authority tables plus SQLx's migration-history table and never runs relay migrations. `crates/buzz-push-gateway/schema/` mirrors those migrations' end state as declarative desired-state DDL, one file per table; a parity test keeps their `public` tables, columns, table options, constraints, and indexes identical. The Helm chart runs a single pre-install/pre-upgrade migration Job using `migration.existingSecret`; that secret contains a DDL-capable `DATABASE_URL`. The URL MUST name a dedicated gateway database, not the relay database: SQLx stores its `_sqlx_migrations` history in `public`, so sharing a database would collide with another application's migration history. `migration.runtimeDatabaseRole` names an existing LOGIN role (the default is `buzz_push_gateway_runtime`) used by runtime `DATABASE_URL`. After scoped migrations, the Job revokes database `CREATE` from that role and schema `CREATE` from both `PUBLIC` and the role, then grants only database `CONNECT`, schema `USAGE`, and `SELECT, INSERT, UPDATE, DELETE` on the six gateway tables. The migration role must own the database/schema objects or otherwise be allowed to issue those grants; it is never provided to runtime replicas. Readiness rejects an empty/partial schema, missing DML, or a runtime role that retains database/schema `CREATE`. Helm waits for the migration hook before updating replicas, so rolling deployments never race unconditional startup migration. Readiness must be removed from load-balancer service endpoints before terminating a pod.