mirror of
https://github.com/block/buzz.git
synced 2026-11-24 10:12:19 +00:00
## Summary Migrations up to `0104` dropped every trigger, function and foreign key. `docs/database.md` Rules 1 and 2 forbid adding them back, but nothing enforced that. This PR adds the guard. - **Migration lint.** `migrations_after_retirement_add_no_triggers_functions_or_foreign_keys` scans each migration numbered after `0104`. It fails on `CREATE [OR REPLACE] FUNCTION | PROCEDURE | TRIGGER | RULE`, `CREATE CONSTRAINT TRIGGER`, `CREATE EVENT TRIGGER`, `REFERENCES` and `FOREIGN KEY`, outside `--` comments. The error names the file and line, for example `migrations/0105_probe.sql:2: CREATE OR REPLACE FUNCTION`. Nested `/* */` comments are skipped. Quoted identifiers and string values are data, except a string passed to `EXECUTE` or `format(`. Each dollar-quoted body is scanned as SQL up to its closing delimiter, so DDL run through `EXECUTE` in a `DO` block is caught, and an apostrophe inside `$$...$$` cannot hide the rest of the file. It runs in the infra-free `buzz-db --lib` unit lane. - **Schema file check.** `desired_state_schema_layout_is_complete_and_schemabot_shaped` allows `schema/` files only under `tables/public/`, `types/`, `partitions/` and `seeds/`, and runs the same scan on every file. A `functions/` or `triggers/` file, or an inline `REFERENCES` in a table file, fails it. - **Catalog check.** `admin_schema_parity_between_desired_state_and_migrations` now asserts that both bootstrap paths (pgschema + reconcile, and all migrations) have zero non-internal triggers, event triggers, `public` functions and procedures, rules and foreign keys. Extension members (pgcrypto) are excluded. No allowlist is needed: main with all 104 migrations applied has none of these objects. Main now has `0105`, which passes the lint. Open PRs that add a migration only need to renumber to `0106` or later. The two open ones (https://github.com/block/buzz/pull/9621 and https://github.com/block/buzz/pull/9690) add no forbidden object. ### What got simpler - The parity test's per-table trigger comparisons (`events`, `event_mentions`, and the five tables `0096` unguarded) and its separate foreign-key check are replaced by one zero-object assertion. - The scoped-key lint no longer parses foreign keys. None may exist, so checking that they lead with `community_id` is dead logic. - `schema/schema.sql` drops its empty `-- functions` and `-- triggers` sections. Its header and `docs/database.md` stop describing `functions/` and `triggers/` directories that no longer exist, and the doc's "remaining triggers stay as a backstop" text now says they are gone. ### Checklist (`docs/database.md`) - [x] Phase: none. Test and docs change only. No migration and no runtime code. - [x] No trigger, function, procedure, rule or foreign key is added. - [x] No migration mutates rows. - [x] Rule 4 locations: no schema change. `schema/schema.sql` changes only comments. ### Related issue None found. ### Testing Run on a Linux workstation with PostgreSQL 17 at `b7254f90`, before the review fixes. CI passed with the first review fixes at `0fed6b1b`. `9749e618` adds dollar-quote, nested `EXECUTE $tag$` and `$1` cases to `schema_object_lint_reports_each_construct_with_its_line`, and CI checks it. - `cargo fmt --check` and `cargo clippy -p buzz-db --all-targets -D warnings`: pass. - `cargo test -p buzz-db --lib` and `cargo nextest run -p buzz-db --lib`: 223 passed. - `scripts/postgres-test-run.sh` for `admin_schema_parity_between_desired_state_and_migrations`, `migrated_catalog_retires_universal_fences_but_keeps_deletion_guards` and `pgschema_reapply_keeps_runtime_partitions`: 3 passed. - Proof the lint can fail: a temporary `migrations/0105_probe.sql` with a function and a `REFERENCES` column fails the lint with `migrations/0105_probe.sql:2: CREATE OR REPLACE FUNCTION` and `migrations/0105_probe.sql:3: REFERENCES`. - Proof the catalog check can fail: a temporary `0105` that creates `probe()` fails the parity test with `the migrations leave a trigger, function, rule or foreign key`, `left: ["function probe()"]`. - The catalog on main at `9c0bcd62`, with all 104 migrations applied through psql, has no triggers, functions, foreign keys, rules or event triggers. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Luke Tornquist <tornquist@squareup.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
170 lines
7.0 KiB
PL/PgSQL
170 lines
7.0 KiB
PL/PgSQL
-- Desired-state database manifest.
|
|
--
|
|
-- `tables/public/<table>.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.
|
|
--
|
|
-- Everything SchemaBot cannot apply lives in this directory's other
|
|
-- subdirectories: `types/`, `partitions/`, and `seeds/`. This file composes
|
|
-- them into one fresh database, in load order: types → tables → partitions →
|
|
-- seeds. docs/database.md forbids triggers, functions, and foreign keys.
|
|
--
|
|
-- `./bin/pgschema apply --file schema/schema.sql` resolves the `\i` includes
|
|
-- relative to this file (it refuses `..`). `pgschema` skips INSERTs and some
|
|
-- storage parameters, so every apply caller must then run
|
|
-- `scripts/reconcile-schema-after-pgschema.sql`. Raw `psql` resolves `\i`
|
|
-- against the working directory instead, so run it from here:
|
|
-- `cd schema && psql -f schema.sql`.
|
|
--
|
|
-- Adding a table: add `tables/public/<table>.sql` and include it below after
|
|
-- the tables it references; put its partitions and seed rows in the
|
|
-- matching subdirectories and include those too. This file holds only `\i`
|
|
-- lines and `CREATE EXTENSION`. Outside `tables/public/`, no file may create,
|
|
-- alter or drop a table or index, except `CREATE TABLE ... PARTITION OF` in
|
|
-- `partitions/`. The layout test enforces both.
|
|
|
|
-- Buzz initial Postgres schema — multi-tenant.
|
|
--
|
|
-- Source of truth for fresh database setup. This is a clean, from-scratch
|
|
-- schema in which `community_id` is a first-class, server-resolved key on
|
|
-- every tenant-scoped row. It is NOT additive over the single-community
|
|
-- schema; the rewrite replaces it. Existing single-community deployments
|
|
-- migrate via the documented backfill migration (0002), which assigns all
|
|
-- pre-existing rows to one default community.
|
|
--
|
|
-- The governing contract is docs/multi-tenant-conformance.md. Every table
|
|
-- below cites the conformance surface it implements. The invariant behind the
|
|
-- whole schema (conformance "row zero"): a request's community is resolved
|
|
-- from the connection host by the server, never supplied by the client, and
|
|
-- every scoped row carries that immutable `community_id`.
|
|
--
|
|
-- Migration-lint obligations enforced by the Lane 0 lint harness:
|
|
-- 1. Every tenant-scoped table has `community_id NOT NULL`.
|
|
-- 2. No UNIQUE / PRIMARY KEY / FK on a scoped table is observable across
|
|
-- communities: each leads with `community_id` (or, for child rows whose
|
|
-- parent already pins the community, joins carry the community tuple).
|
|
-- 3. `channels.community_id` is immutable (application writers; no UPDATE path).
|
|
-- 4. Operator-global tables are named in the explicit allowlist, not implied.
|
|
|
|
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
|
|
|
-- types
|
|
\i types/channel_type.sql
|
|
\i types/channel_visibility.sql
|
|
\i types/member_role.sql
|
|
\i types/workflow_status.sql
|
|
\i types/run_status.sql
|
|
\i types/approval_status.sql
|
|
\i types/delivery_method.sql
|
|
\i types/subscription_status.sql
|
|
\i types/pause_reason.sql
|
|
\i types/channel_add_policy.sql
|
|
|
|
-- tables
|
|
\i tables/public/communities.sql
|
|
\i tables/public/channels.sql
|
|
\i tables/public/channel_members.sql
|
|
\i tables/public/users.sql
|
|
\i tables/public/personal_read_accounts.sql
|
|
\i tables/public/personal_read_frontiers.sql
|
|
\i tables/public/events.sql
|
|
\i tables/public/event_mentions.sql
|
|
\i tables/public/subscriptions.sql
|
|
\i tables/public/delivery_log.sql
|
|
\i tables/public/workflows.sql
|
|
\i tables/public/workflow_runs.sql
|
|
\i tables/public/workflow_approvals.sql
|
|
\i tables/public/scheduled_workflow_fires.sql
|
|
\i tables/public/api_tokens.sql
|
|
\i tables/public/rate_limit_violations.sql
|
|
\i tables/public/thread_metadata.sql
|
|
\i tables/public/reactions.sql
|
|
\i tables/public/pubkey_allowlist.sql
|
|
\i tables/public/relay_members.sql
|
|
\i tables/public/join_policy_acceptances.sql
|
|
\i tables/public/relay_invites.sql
|
|
\i tables/public/archived_identities.sql
|
|
\i tables/public/audit_log.sql
|
|
\i tables/public/moderation_actions.sql
|
|
\i tables/public/moderation_reports.sql
|
|
\i tables/public/community_bans.sql
|
|
\i tables/public/community_nip_fi_issuers.sql
|
|
\i tables/public/_operator_global_tables.sql
|
|
\i tables/public/git_repo_names.sql
|
|
\i tables/public/parameterized_event_watermarks.sql
|
|
\i tables/public/product_feedback.sql
|
|
\i tables/public/push_leases.sql
|
|
\i tables/public/push_wake_outbox.sql
|
|
\i tables/public/push_match_queue.sql
|
|
\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
|
|
\i tables/public/replica_heartbeat.sql
|
|
\i tables/public/community_deletion_requests.sql
|
|
\i tables/public/entity_deletion_approvals.sql
|
|
\i tables/public/entity_deletion_requests.sql
|
|
\i tables/public/entity_deletion_stage_history.sql
|
|
\i tables/public/user_deletion_fences.sql
|
|
\i tables/public/community_deletion_approvals.sql
|
|
\i tables/public/community_deletion_checkpoints.sql
|
|
\i tables/public/community_deletion_manifest_keys.sql
|
|
\i tables/public/storage_taxonomy_sweeps.sql
|
|
\i tables/public/community_serving_write_leases.sql
|
|
\i tables/public/community_deletion_executor_heartbeats.sql
|
|
\i tables/public/relay_operators.sql
|
|
\i tables/public/relay_admin_actions.sql
|
|
\i tables/public/relay_admin_outbox.sql
|
|
\i tables/public/operator_listener_pubkeys.sql
|
|
\i tables/public/operator_listener_outbox.sql
|
|
\i tables/public/relay_operator_audit.sql
|
|
\i tables/public/storage_accounting_snapshots.sql
|
|
\i tables/public/artifact_heads.sql
|
|
\i tables/public/artifact_revisions.sql
|
|
|
|
-- partitions
|
|
\i partitions/events.sql
|
|
\i partitions/delivery_log.sql
|
|
|
|
-- seeds
|
|
\i seeds/communities.sql
|
|
\i seeds/rate_limit_violations.sql
|
|
\i seeds/_operator_global_tables.sql
|
|
\i seeds/product_feedback.sql
|
|
\i seeds/push_gateway_challenges.sql
|
|
\i seeds/push_gateway_installations.sql
|
|
\i seeds/push_gateway_delegations.sql
|
|
\i seeds/push_gateway_endpoint_quotas.sql
|
|
\i seeds/push_gateway_delivery_auth_replays.sql
|
|
\i seeds/push_gateway_delivery_request_replays.sql
|
|
\i seeds/replica_heartbeat.sql
|
|
\i seeds/community_deletion_requests.sql
|
|
\i seeds/entity_deletion_approvals.sql
|
|
\i seeds/entity_deletion_requests.sql
|
|
\i seeds/entity_deletion_stage_history.sql
|
|
\i seeds/user_deletion_fences.sql
|
|
\i seeds/community_deletion_approvals.sql
|
|
\i seeds/community_deletion_checkpoints.sql
|
|
\i seeds/community_deletion_manifest_keys.sql
|
|
\i seeds/storage_taxonomy_sweeps.sql
|
|
\i seeds/community_serving_write_leases.sql
|
|
\i seeds/community_deletion_executor_heartbeats.sql
|
|
\i seeds/relay_operators.sql
|
|
\i seeds/relay_admin_actions.sql
|
|
\i seeds/relay_admin_outbox.sql
|
|
\i seeds/operator_listener_pubkeys.sql
|
|
\i seeds/operator_listener_outbox.sql
|
|
\i seeds/relay_operator_audit.sql
|
|
\i seeds/storage_accounting_snapshots.sql
|
|
|
|
\i tables/public/nip_fi_profile_heads.sql
|
|
|
|
\i tables/public/community_auth_upgrades.sql
|
|
\i seeds/community_auth_upgrades.sql
|
|
|
|
\i tables/public/media_uploaders.sql
|
|
\i tables/public/media_publications.sql
|