How to Branch a PlanetScale Database Per Preview in GitHub Actions
Run pscale branch create per PR so each preview has an isolated MySQL branch you can safely migrate and drop.
PlanetScale branches isolate schema changes. Create a branch named for the PR with the pscale CLI, authenticate with a service token, and connect the preview to it. Delete the branch on close.
Steps
- Add
PLANETSCALE_SERVICE_TOKENand its ID as secrets. - Run
pscale branch create <db> pr-<number>. - Create a connection string or password scoped to that branch.
Workflow
.github/workflows/preview.yml
jobs:
db-branch:
runs-on: ubuntu-latest
env:
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }}
steps:
- uses: planetscale/setup-pscale-action@v1
- run: |
pscale branch create myapp "pr-${{ github.event.pull_request.number }}" \
--org my-org --waitGotchas
--waitblocks until the branch is ready before you connect to it.- Open a deploy request to promote schema changes; drop the branch on PR close.
Related guides
How to Create an Ephemeral Database Per Preview With Neon Branching in GitHub ActionsGive each preview its own Postgres by creating a Neon branch per pull request in GitHub Actions, then deletin…
How to Seed Data Into a Preview Environment in GitHub ActionsLoad migrations and seed data into a fresh preview database from GitHub Actions so each per-PR environment st…