Skip to content

Migration ledger does not reflect production, and two OAuth scope catalogs conflict #437

Open

Description

@thinkclay

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

  1. Reconcile: decide the canonical scope vocabulary, delete or neuter the losing seed, and remove 224's blanket DELETE.
  2. Backfill schema_migrations for 331/332/333/335 and either apply 336 (harmless now — 338 supersedes it and is numbered higher) or delete it.
  3. 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.
  4. Audit dev/preview once those projects are accessible — they are likely further adrift than production.

Activity

  1. added
    bug
    on Jul 30, 2026
  2. thinkclay commented on Jul 30, 2026

    @thinkclay
    ContributorAuthor

    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.sh is 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 --status lists 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_profiles view, both grant sets, and the api_keys_key_prefix_check constraint.

    336 is neutralized, not recorded-as-applied and not deleted:

    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.

    Scope After replay
    documents:write still requires document.manage
    organizations:write still requires team.settings
    profile:read still is_self_scoped

    The 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:action scopes with one registered app using four of them; 224's 31 action:resource scopes (plus five admin:*) 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.sh already accepts dev and preview and will work the moment access exists.

  3. thinkclay commented on Sep 10, 2026

    @thinkclay
    ContributorAuthor

    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:

    env pending
    prod 348_role_assignments_unique_nulls_not_distinct · 349_content_reports_and_user_blocks · 350_move_user_feedback_to_core
    preview the same three
    dev the same three, plus 337_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

    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:

    select
      to_regclass('core.content_reports')  is not null as has_349_tables,
      to_regclass('core.user_feedback')    is not null as has_350_move,
      (select count(*) from pg_constraint
        where conrelid = 'core.role_assignments'::regclass
          and contype = 'u'
          and pg_get_constraintdef(oid) like '%NULLS NOT DISTINCT%') = 1 as has_348_constraint;

    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.

  4. thinkclay commented on Sep 10, 2026

    @thinkclay
    ContributorAuthor

    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:

    prod: clean — local and remote history in sync.
    preview: clean — local and remote history in sync.
    dev: clean — local and remote history in sync.
    

    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_assignments authorization fix), 349 (the moderation tables) and 350 (user_feedback into core) 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:

    select
      to_regclass('core.content_reports')  is not null as has_349_tables,
      to_regclass('core.user_feedback')    is not null as has_350_move,
      (select count(*) from pg_constraint
        where conrelid = 'core.role_assignments'::regclass
          and contype = 'u'
          and pg_get_constraintdef(oid) like '%NULLS NOT DISTINCT%') = 1 as has_348_constraint;

    If someone applied these deliberately just now, worth saying so on this issue — whether it went through db push --include-all or the Management API changes what the ledger will do next, and that is exactly what this issue is about.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions