Repository navigation
Description
Surfaced while fixing #428. Two related problems that make "which migrations are live?" unanswerable without inspecting objects by hand.
1. schema_migrations under-records, and one migration was silently skipped
Production records 167 migrations (001–334) against 174 files in the repo. Seven are unrecorded:
| Migration | Actually applied? | How I checked |
|---|---|---|
| 331 user_profiles_view | ✅ yes | view exists |
| 332 api_keys_grants | ✅ yes | grants present |
| 333 api_keys_key_prefix_check | ✅ yes | constraint exists |
| 335 scheduling_grants | ✅ yes | grants present |
| 336 fix_validate_oauth_scope | ❌ no | function body still had migration 220's dead join |
| 337 notification_cron_vault | ✅ yes | cron jobs exist |
| 338 oauth_scope_real_authorization | ✅ yes | just applied |
So six were applied-but-unrecorded, and one was never applied at all — and there is no way to tell those apart from the ledger. The cause of the under-recording is that applying SQL through the Management API database/query endpoint bypasses supabase_migrations.schema_migrations entirely, which is how most out-of-band fixes in this repo have been applied.
I recorded 337 and 338 to stop the drift growing, but 331/332/333/335 remain unrecorded and 336 is a real gap.
2. Two OAuth scope catalogs, and a replay would swap them
| Seed migration | Naming | Scopes |
|---|---|---|
138_oauth_app_registry.sql |
resource:action |
7 — documents:read, profile:write, … |
224_seed_oauth_scopes.sql |
action:resource |
31 — read:documents, write:profile, openid, email, plus 5 admin:* |
Production holds 138's set. A database built from the full chain ends on 224's set — which is what local has. Migration 224 is recorded as applied in production, yet production has 138's rows, so something reseeded afterwards.
The sharp edge: 224 opens with DELETE FROM core.oauth_scopes. Replaying the chain against production would drop its catalog and install the other one, invalidating any per-scope configuration keyed on names — including the authorization classifications added in 338. (338 is written to classify both vocabularies for exactly this reason, so it survives; anything added later keyed on one naming scheme would not.)
Why it matters
Every conclusion drawn from reading packages/supabase/migrations/ is currently unreliable. #428 was analysed from the repo and reported the opposite of production's actual behaviour — the repo said "rubber stamp", production was "fails closed" — because 336 was in the repo but not the database. The same trap applies to any future schema reasoning, and it is worse for dev/preview, whose Supabase projects are not even reachable from this machine's login.
Suggested direction
- Reconcile: decide the canonical scope vocabulary, delete or neuter the losing seed, and remove 224's blanket
DELETE. - Backfill
schema_migrationsfor 331/332/333/335 and either apply 336 (harmless now — 338 supersedes it and is numbered higher) or delete it. - Stop applying schema changes through the Management API query endpoint, or wrap it in a script that records the version afterwards.
scripts/already has the pieces. - Audit dev/preview once those projects are accessible — they are likely further adrift than production.
Activity
fix(migrations): make the ledger reflect reality and stop it drifting (…
thinkclay commentedon Jul 30, 2026
Items 1–3 done in
6df8d2d8f. Production ledger: 175 of 175 recorded, zero unrecorded. Item 4 (dev/preview) is still blocked on access.The cause, closed off
scripts/apply-migration.shis now the one path in — it applies a file and records its version in the same run, refuses to re-apply an already-recorded version, and--statuslists what's unrecorded while stating plainly that unrecorded does not mean unapplied. The memory note that taught the raw-query-POST method has been corrected to point at the script, since that note is what perpetuated the drift.The ledger
Backfilled 331/332/333/335 after re-verifying each one's objects exist rather than trusting my earlier note — the
user_profilesview, both grant sets, and theapi_keys_key_prefix_checkconstraint.336 is neutralized, not recorded-as-applied and not deleted:
is_self_scoped, which only 338 introduces.So it keeps its slot, explains itself, and does nothing.
224's blanket DELETE
Gone. Its eleven inserts are additive via
ON CONFLICT (scope) DO NOTHING.Verified by simulating production rather than testing the convenient case. My first attempt was wrong — local carries 224's own vocabulary, so filtering to 138's scope names emptied the table and proved nothing. Rebuilt properly: production's 7-scope catalog with 338's classifications, then replayed 224.
documents:writedocument.manageorganizations:writeteam.settingsprofile:readis_self_scopedThe 31 new arrivals are unclassified, which 338 treats as denied. Previously the replay would have deleted the live catalog outright and installed the other vocabulary with no gating at all.
Still open
Which vocabulary is canonical — deliberately not decided here, it's a product call. Production runs 138's 7
resource:actionscopes with one registered app using four of them; 224's 31action:resourcescopes (plus fiveadmin:*) are unused by anything. The replay is now non-destructive either way, so deciding later costs nothing.Item 4 — dev/preview audit. Their Supabase projects (
pmtdqrfpumqwkdhpgwcz,uhjkipdwayqfihkanabk) aren't visible to this machine's Supabase login or PAT, so I can't read their ledgers.apply-migration.shalready acceptsdevandpreviewand will work the moment access exists.thinkclay commentedon Sep 10, 2026
Fresh evidence for this, from the drift audit on #725 (2026-09-10). Three migrations are pending on every environment, while a later one has already been applied:
348_role_assignments_unique_nulls_not_distinct·349_content_reports_and_user_blocks·350_move_user_feedback_to_core337_notification_cron_vault(known, #564)The CLI's wording is the important part — "Found local migration files to be inserted before the last migration on remote database". Something numbered above 350 is already applied, so these three now sort behind the head and will never be picked up by an ordinary push.
What is actually not in production, if the ledger is telling the truth
role_assignmentsUNIQUE NULLS NOT DISTINCT. This is the authorization defect from core.role_assignments: the uniqueness constraint has never held, because every legal row has a NULL scope column #675: without it, revoking a role by deleting "the" assignment can leave a duplicate behind and the user keeps the role. I closed core.role_assignments: the uniqueness constraint has never held, because every legal row has a NULL scope column #675 earlier today on local-database evidence; that was correct about the migration and wrong about the environments, and I have corrected the issue.content_reports/user_blocks. The moderation feature shipped in feat(moderation): report content, report people, block people #703 has no tables to write to.user_feedbackmoved fromlogs(which PostgREST does not serve) tocore. Every/v1/feedbackendpoint stays unreachable until this runs — the exact defect The /v1/feedback router is unreachable — PostgREST does not expose the logs schema #665 was closed for.The caveat this issue exists for
I cannot tell from CI alone whether those objects are genuinely absent or merely unrecorded. Migrations applied through the Management API query endpoint do not register in
schema_migrations, which is this issue's subject — so the ledger may be understating what is deployed. Both readings are bad, in different ways: either three real fixes are not live, or the ledger cannot be used to answer that question at all.Someone with environment access can settle it in one query per environment:
Three booleans, and it distinguishes the two readings completely.
Meanwhile it blocks migration PRs
Any PR adding a migration now fails drift-audit on all three environments for reasons that have nothing to do with it — #725 is the current example. That is the same failure mode as the checks fixed in #692 and #710: a gate that fails for a standing reason stops carrying signal, and the habit of merging past it forms quickly.
#726 fixes a smaller thing that made this harder to read: the audit's error text hardcoded dev's cause and told prod to go look at 337.
thinkclay commentedon Sep 10, 2026
Correcting the comment above — it is already out of date, and I would rather say so than leave it standing.
I read that drift audit at 20:00 UTC. By 20:12, on the very next run of the same PR, all three environments report:
Each would now push only the two migrations #725 itself adds (352, 353). So 337, 348, 349 and 350 were all applied during those twelve minutes — including dev's 337, which this repo had recorded as blocked on #564.
So the specific alarm I raised is void: 348 (the
role_assignmentsauthorization fix), 349 (the moderation tables) and 350 (user_feedbackintocore) are applied. Nothing is missing from production on account of them, and my note on #675 has been corrected too.What still stands
The underlying complaint of this issue is untouched by that. The audit and the ledger disagreed for long enough that I read a real, urgent-looking failure off it and acted on it — and the only reason I found out otherwise is that I happened to re-read the same job twelve minutes later. A ledger that can say "three migrations missing from production" while they are applied, or applied while it says missing, cannot be used to answer the question it exists to answer.
The verification query from my earlier comment is still the way to settle any individual case, and I would treat the audit as a prompt to run it rather than as an answer:
If someone applied these deliberately just now, worth saying so on this issue — whether it went through
db push --include-allor the Management API changes what the ledger will do next, and that is exactly what this issue is about.