Repository navigation
ci: flag pull requests that change a migration so the apply step is not forgotten - #736
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
prompt_suggestionswas created straight against the hosted database and never written into a migration, so every fresh install broke once that migration shipped.00050_content_opportunity_aggregates.sqlmerged and was never applied. The table did not exist, so the Content page returned "Failed to list opportunities" for all fifty brands holding 7,561 opportunities. The file was correct; only the apply step was missing.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 theneeds-migrationlabel. It shows in the PR list, it survives on the merged PR, andis:merged label:needs-migrationstill 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:
removedis ignored.The workflow runs on
pull_request_targetbecause both incidents came from forks, where apull_requesttoken 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 existingfirst-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 EditorIt 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:
pg_procpg_tables.rpc()the application actually calls resolves to a function that existsThe 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.topicsand as"public"."topics", and the first pattern read the quoted form's schema as the table name — checkingpublictwenty-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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
The workflow only takes effect once it is on
main, so most of this is verified after merge:supabase/migrations/and confirm it is labelledneeds-migration, with the file listed in the workflow-run annotation.is:merged label:needs-migrationfinds it.bash supabase/check-drift.sh, paste the output into the SQL Editor, and confirm it returns no rows.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdNo application code is touched, so
web/andserver/are unaffected.CONTRIBUTING.mddocuments bothbuild-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 onmainand reformatting it here would bury the change.