Skip to content

feat(supabase): run the local applier as postgres - #3928

Open
twoimo wants to merge 4 commits into
mainfrom
feat/d2-local-applier-postgres
Open

twoimo wants to merge 4 commits into
mainfrom
feat/d2-local-applier-postgres

Conversation

@twoimo

@twoimo twoimo commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Why

Local migration apply should run as the role hosted uses. D2 in docs/operations/local-postgres17-transition.md makes postgres (non-superuser) the applier's role, as the Supabase CLI's migrateAndSeed does, and keeps supabase_admin only where PostgreSQL 17 needs a superuser or the platform owner. Refs #3323

What changed

  • PsqlExecutor defaults to postgres. The ledger, sequence markers, baseline record, auth API ledger and seed run as postgres, which creates and owns _tzudong_local. A ledger another role created fails with local_ledger_owner_mismatch.
  • The privacy rows move to local-seed-privacy-fixture.sql. It runs as supabase_admin under SET LOCAL ROLE privacy_workflow_owner. publish_privacy_policy_version exists, but it cannot write the fixture's fixed id and timestamps, and no function writes the age profile directly. The seed digest binds both files.
  • local-stack.py creates local_analytics as postgres, which 10000000000000_demote-postgres.sql grants ALL ON DATABASE postgres.
  • local_baseline.py ownership fails on any project object owned or granted by supabase_admin. --exact also requires covered objects to match ownership.txt, and --catalog-manifest compares owners and grants with the generator. The nightly runs the rule after migration_apply.

Scope

Remaining supabase_admin call sites, each for a role postgres cannot hold:

  • _apply_platform_bootstrap: policies on storage.objects, whose owner supabase_storage_admin the image revoked from postgres (20250421084701).
  • The privacy fixture, the receipt readback, and the closure runtime scan: each reads, writes or calls privacy_retention, which only privacy_workflow_owner may use. The G014 guards forbid postgres any membership in that role.
  • local-function-runtime-scan.py: ALTER FUNCTION needs the owner, and 14 of 128 candidates belong to privacy_workflow_owner or privacy_auth_bridge.
  • Nightly stages 11, 13 and 14: test probes that act as privacy_workflow_owner, grant themselves that role, or add triggers on auth.users (owner supabase_auth_admin, revoked from postgres).
  • local_baseline.py restore: the role file and multi-owner restore D2 names as superuser steps.

Tradeoffs

  • Running the whole seed as supabase_admin with SET ROLE postgres would keep one transaction, but session_user would differ from the CLI's. The seed is now two transactions. Both are idempotent.
  • After apply, the nightly checks only the supabase_admin rule. A later migration may legitimately change a covered object's grants, and provisioning and the drift gate already hold covered objects to ownership.txt.

Blast Radius

Local stacks and the nightly only. No hosted access and no migration body changes. An existing local stack whose ledger an older applier created needs local-stack.py reset.

Verification

  • Nightly with pg17_baseline=true: green at d0b0ade, https://github.com/twoimo/tzudong/actions/runs/45210447234. The drift gate found no drift. The next commit only adds this evidence to the plan doc.
  • Owners and grantors: after migration_apply, no project object is owned or granted by supabase_admin. ownership --exact --catalog-manifest on the run's post-apply capture shows 0 differences from ownership.txt (2,192 lines) and from the generator's manifest (Catalog run 45145493029: 61 relation owners, 419 grants).
  • Unit tests: 346 pass on Python 3.12. Planted mutations fail them: the default reverted, a migration body, ledger or seed moved to supabase_admin, the fixture without SET LOCAL ROLE, as postgres or with RESET ROLE, the runtime scan as postgres (red then green), and the owner and grantor rules disabled.
  • A socket-only scratch PostgreSQL 17 ran the real ledger DDL and fixture. Without the owner policy, forced RLS refuses the fixture insert, which shows the rows are written as privacy_workflow_owner.

🤖 Generated with Claude Code

twoimo and others added 4 commits November 24, 2026 00:53
PsqlExecutor now logs in as postgres unless a call names supabase_admin,
the way the Supabase CLI applies migrations and seeds. The ledger, the
sequence markers, the baseline record and the seed all run as postgres,
so postgres creates and owns _tzudong_local. A ledger that another role
created fails with local_ledger_owner_mismatch and names the reset.

Four calls keep supabase_admin, each for a role postgres cannot hold:
the storage.objects policies (owner supabase_storage_admin), the privacy
fixture, the receipt readback of privacy_retention rows, and the closure
runtime scan, which runs privacy_retention's G014 assertions. The G014
guards forbid postgres any membership in privacy_workflow_owner.

The privacy rows move from local-seed.sql to
local-seed-privacy-fixture.sql. No migration function can write them with
the fixture's fixed id and timestamps, so the file runs as supabase_admin
and writes them under SET LOCAL ROLE privacy_workflow_owner. The seed
marker and the receipt's seed_source_sha256 bind both files.

local-stack.py creates the local_analytics namespace as postgres, which
the image's demote-postgres migration grants ALL on the database.

Refs #3323

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`local_baseline.py ownership` reads every project object's owner and
exploded ACL and fails on any one owned or granted by supabase_admin,
since no migration assigns one. --exact also requires the objects the
baseline covers to match ownership.txt line for line, and
--catalog-manifest compares relation owners and grants with the catalog
generator's manifest, reading ALL as PostgreSQL 15's seven table
privileges. Either comparison fails when it has nothing to compare.

The nightly runs the superuser rule right after migration_apply.
Provisioning already holds covered objects to ownership.txt, and a later
migration may legitimately change them. The three nightly probes that
keep supabase_admin say why.

Refs #3323

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Refs #3323

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The baseline covers every migration, so the post-apply owner rule had
nothing new to judge. `local-migrate.py probe-uncovered` runs right after
migration_apply through the applier's executor as postgres. In one
transaction that ends in ROLLBACK it creates a table, a sequence and a
function in public and reads back their owners and exploded ACLs. It
fails unless the owner is postgres, no grantor is supabase_admin, and the
grants equal what pg_default_acl on the live database gives postgres in
public.

A socket-only scratch PostgreSQL now backs the tests for the probe, the
_tzudong_local owner guard and the privacy fixture. The fixture must be
one transaction with SET LOCAL ROLE privacy_workflow_owner before every
insert. A test pins the local_analytics bridge to postgres.

Refs #3323

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant