Skip to content
Latchkey

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_TOKEN and 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 --wait

Gotchas

  • --wait blocks 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

Run this faster and cheaper on Latchkey managed runners. Start free →