Connect a FastAPI pull-request preview to its own Supabase branch
Test database changes on an isolated Supabase branch, explicitly bind the FastAPI preview, and verify that misrouted writes are rejected.
Give each FastAPI pull-request preview its own Supabase branch to test database changes without sharing production data. Keep Postgres and Auth on Supabase. Run the FastAPI previews on Gregale, and bind the two systems explicitly by PR number.
This example uses two disposable PRs with one migration and two different fixture rows. Each preview reads and writes only its matching fixture. If a preview is accidentally pointed at the other branch, the database function rejects the write before updating anything. There is no automatic pairing, production database access or tenant provisioning.
View the runnable example on GitHub or download the ZIP.
1. Prepare two Supabase branches
Use a disposable Supabase project with no production data. Supabase Branching provisions separate credentials per branch; check its plan and usage requirements before enabling it. You can provision branches with the GitHub integration or create them manually. Use one branch per PR, and wait for the migration to finish. For manual branches without migration automation, run the included migration in each disposable branch's SQL editor, once per branch.
The migration creates a private table and a narrowly scoped RPC. Create a synthetic user in each branch's Auth. In each branch's SQL editor, verify you have selected the intended branch, then insert its own row. Replace the number, label and UUID with the actual PR number, test label and that branch's test user ID:
insert into preview_demo.fixture (pr_number, label, owner_id)
values (17, 'branch-A', '<branch-test-user-uuid>');
For the other PR use its number and a different label, such as branch-B. Do not commit either marker as a global seed. It identifies the database that actually received the request. Only the branch fixture's owner can use the RPC; the public table API cannot read or edit the row directly.
2. Connect the Gregale previews
Use an app and repository you control. Enable Gregale PR previews for the FastAPI app. Each PR must change the workload so Gregale builds a preview; look for the actual preview URL in the GitHub check. Do not share live secrets with pull-request code.
Set PREVIEW_TARGETS on the bound app's pr-preview scope. Use the project ref and publishable key from each branch, not the base project. For example, with two PRs numbered 17 and 28:
{
"17": { "ref": "<branch-A-project-ref>", "publishable_key": "sb_publishable_REPLACE_A" },
"28": { "ref": "<branch-B-project-ref>", "publishable_key": "sb_publishable_REPLACE_B" }
}
Use gregale env set --app YOUR_APP --env pr-preview --interactive and paste the JSON as one line. Push a change to the FastAPI workload on each PR to pick up the new mapping. The map contains test project addresses and publishable keys only. Never add a database password, service-role key, Gregale token, or a production entry. Restrict who can trigger preview builds.
FastAPI selects a target from the map using GREGALE_PREVIEW and GREGALE_PR_NUMBER supplied by Gregale, not a PR number or host from the browser. It forwards the signed-in test user's JWT to the selected Supabase project over HTTPS. It doesn't follow redirects. Supabase checks the user token, and the Postgres function checks the PR marker and user before updating the row. A missing binding fails closed.
3. Check the write, not just the URL
Sign in as each branch's test user to obtain a short-lived Supabase session. Call GET /fixture with that user's bearer token, then call POST /fixture with {"note":"A only"} or {"note":"B only"}. Keep tokens out of source and logs. If Gregale access controls use the same Authorization header, use a compatible preview access policy for your disposable example before testing; the example expects that header to carry the Supabase session.
The example includes a helper that prompts for a synthetic user password and keeps the session token in memory. Copy the URL from the Gregale PR check and confirm it belongs to the test app before running:
python hosted_check.py --ref BRANCH_A_REF --preview https://pr-17-test-api.gregale.dev
python hosted_check.py --ref BRANCH_A_REF --preview https://pr-17-test-api.gregale.dev --note "A only"
| Test | Expected result |
|---|---|
| PR A with branch A's user; PR B with branch B's user | Each sees its own label and write count. |
| PR A temporarily mapped to B, with a valid B user token | The write returns 409 preview_branch_mismatch. Verify B's note and count did not change. |
| Correct branch but a different signed-in user | 403, with no write. |
| Missing PR mapping or request to the non-preview app | 409 without calling Supabase. |
Test wrong bindings only against your disposable branches. Restore the map, rebuild the changed preview and verify again. Close the test PRs and remove any test branches that were created manually and are no longer needed.
What was tested: FastAPI's local test ran the migration in three independent Postgres instances using PGlite, including a stand-in for main. Both intended writes succeeded. Misrouted writes were rejected and the destination rows were unchanged. The HTTP gateway and user session were simulated; hosted provisioning, user authentication and Gregale preview lifecycle were not verified.
For sign-in and row-level data access in a regular backend, see FastAPI with Supabase Auth and Postgres.
Questions
- Can a FastAPI pull-request preview use its own Supabase branch?
Yes. Bind the PR number to the Supabase branch project ref and publishable key, and verify that the database matches that PR before writing.
- Does Gregale automatically pair previews with Supabase branches?
This example uses an explicit PR-to-branch mapping. It does not provision or pair hosted Supabase branches automatically.