Skip to content

ci: flag pull requests that change a migration so the apply step is not forgotten - #736

Merged
gkhngyk merged 2 commits into
mainfrom
feature/728-flag-migration-prs
Aug 18, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
feature/728-flag-migration-prs

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Summary

A migration file is only half of a migration. The other half — applying it to the hosted database — is a manual step after merge, and nothing in the process carried that reminder across a merge someone else authored.

It has been missed twice, both on contributor PRs, and both times the gap surfaced as a customer-visible error rather than as something we caught:

This adds the reminder, and a way to confirm afterwards that it was acted on.

A label, not a failing check

The issue left this open. A hard failure cannot be ignored, but it blocks a PR for something that is not wrong with the code. A bare annotation is easy to scroll past — which is exactly how this was missed twice.

So: a PR touching supabase/migrations/ gets the needs-migration label. It shows in the PR list, it survives on the merged PR, and is:merged label:needs-migration still finds it a month later. The workflow-run annotation lists the specific files for whoever is looking at the run.

Two details that keep the label honest:

  • An edited migration counts as much as a new one. An amended file is as unapplied as a fresh one. Only removed is ignored.
  • Dropping the last migration from a branch clears the label. Otherwise it would come to mean "this once contained a migration" rather than "this still needs applying".

The workflow runs on pull_request_target because both incidents came from forks, where a pull_request token is read-only and cannot label anything. It never checks out or executes the PR's branch — the file list comes from the API — so an untrusted branch has nothing to influence. This follows the same pattern and the same reasoning as the existing first-interaction.yml.

The label is created on demand rather than assumed, so a fork or a self-hosted copy of this repo does not fail the run for missing it.

The second net: check-drift.sh

The CI label is the reminder. This is how you confirm it was acted on:

bash supabase/check-drift.sh          # prints SQL; paste it into the SQL Editor

It reads the repo and writes a read-only query. No credentials, no connection, nothing written — safe against production, and it works for self-hosters checking their own database. An empty result means no drift; every row is something the code expects and that database does not have.

Three checks, weakest to strongest:

  1. every function the migrations declare exists in pg_proc
  2. every table they declare exists in pg_tables
  3. every .rpc() the application actually calls resolves to a function that exists

The third is the one that matters. It verifies what users hit at run time rather than what the migrations declare, so it catches a function that was renamed, never applied, or applied under a different name — which is the shape of both incidents above.

Verification

Run against the hosted database, the generated query returns no rows across 19 functions, 43 tables and 14 called RPCs. Seeded with three names that do not exist, it returns all three — so the empty result is a real pass, not a query that never reports anything.

One bug was caught while testing and fixed before commit: migrations here are written both as public.topics and as "public"."topics", and the first pattern read the quoted form's schema as the table name — checking public twenty-six times instead of the twenty-six tables it stood for. Table coverage went from 18 to 43 and function coverage from 16 to 19 once quotes were stripped before the name is read.

The workflow's script was exercised against mocked API objects across all six branches — new migration, edited migration, unrelated PR, label already present, migration dropped from the branch, migration deleted — and each behaves as described.

Related issue

Closes #728

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

The workflow only takes effect once it is on main, so most of this is verified after merge:

  1. Open any PR that adds a file under supabase/migrations/ and confirm it is labelled needs-migration, with the file listed in the workflow-run annotation.
  2. Open a PR touching nothing under that path and confirm no label appears.
  3. Remove the migration from a labelled branch, push, and confirm the label is removed.
  4. Confirm the label persists on the PR after merge and that is:merged label:needs-migration finds it.
  5. Run bash supabase/check-drift.sh, paste the output into the SQL Editor, and confirm it returns no rows.

Checklist

No application code is touched, so web/ and server/ are unaffected. CONTRIBUTING.md documents both build-schema.sh (which CI already enforced but the guide never mentioned) and the new drift check. It is left otherwise unformatted because it already fails a repo-root Prettier run on main and reformatting it here would bury the change.

…ot forgotten

A migration file is only half of a migration. The other half — applying it to
the hosted database — is a manual step after merge, and nothing carried that
reminder across a merge someone else authored. It has been missed twice: #709
left prompt_suggestions absent from every fresh install, and 00050 merged with
#700 without being applied, so content_opportunity_aggregates did not exist and
the Content page failed for all fifty brands holding 7,561 opportunities. Both
were contributor pull requests, and both surfaced as a customer-visible error
rather than as something we caught.

A PR touching supabase/migrations/ is now labelled needs-migration. A label
rather than a failing check: a hard failure would block a PR for something that
is not wrong with the code, and a bare annotation scrolls away — which is
exactly how this was missed. The label shows in the PR list, survives on the
merged PR, and stays searchable afterwards.

The workflow runs on pull_request_target because both incidents came from forks
and labelling needs write permission there. It never checks out or executes the
PR's branch; the file list comes from the API, so an untrusted branch has
nothing to influence. Editing a migration counts as much as adding one, and
dropping the last migration from a branch clears the label again, so it always
means "this still needs applying".

check-drift.sh is the second net: it reads the repo and prints a read-only
query reporting anything the code expects and a database lacks. No credentials
and no connection, so it is safe to run against production and works for
self-hosters. The strongest of its three checks is the last — every .rpc() the
application actually calls resolves to a function that exists — because it
verifies what users hit rather than what the migrations declare. Run against
the hosted database it reports no drift across 19 functions, 43 tables and 14
called RPCs; seeded with names that do not exist, it reports all three.

Closes #728
Labels on a pull request go through the issues API, and creating a repo label
needs `issues: write` — `pull-requests: write` attaches an existing label but
cannot create a missing one. Without the extra scope the first PR to touch a
migration in a fork of this repo would 403 on setup.

The failure is also no longer thrown. Attaching the label is the job; failing
the run over the step that creates it would put a red mark on a PR while
telling nobody anything useful. The status is logged instead, and 422 (already
exists) stops being a special case since it is the normal path from the second
run onward.
@gkhngyk
gkhngyk merged commit 2fba133 into main Aug 18, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the feature/728-flag-migration-prs branch August 18, 2026 18:29
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.

CI: flag pull requests that add a migration, so it cannot be merged and forgotten

1 participant