Skip to content

Instantly share code, notes, and snippets.

@drenther
Forked from flybayer/planetscale.yaml
Created April 6, 2023 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drenther/f08d5dea1aa5f3ea57adabcffce1b2fd to your computer and use it in GitHub Desktop.
Save drenther/f08d5dea1aa5f3ea57adabcffce1b2fd to your computer and use it in GitHub Desktop.
name: PlanetScale Migration
on:
pull_request:
types: [opened, synchronize]
paths:
- packages/shared/db/schema.prisma
env:
PLANETSCALE_ORG: your-ps-org
PLANETSCALE_DB: your-db-name
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }}
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}
jobs:
branch_and_migration:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
# This is used to ensure the create branch & create deploy request actions only run once
- name: PS creation state
id: ps_created
uses: actions/cache@v2
with:
path: .planetscale-db-created
key: ${{ runner.os }}-ps-created-${{ github.event.pull_request.head.ref }}
- name: Create planetscale branch
uses: planetscale/create-branch-action@v1
id: create_branch
if: ${{ steps.ps_created.cache-hit != 'true' }}
with:
org_name: ${{ env.PLANETSCALE_ORG }}
database_name: ${{ env.PLANETSCALE_DB }}
branch_name: ${{ github.event.pull_request.head.ref }}
wait: true
- name: Create planetscale deploy request
uses: planetscale/create-deploy-request-action@v1
id: create_deploy_request
if: ${{ steps.ps_created.cache-hit != 'true' }}
with:
org_name: ${{ env.PLANETSCALE_ORG }}
database_name: ${{ env.PLANETSCALE_DB }}
branch_name: ${{ github.event.pull_request.head.ref }}
# This is used so migrations are only pushed to the branch if schema changes
- name: Save schema hash
id: schema_hash
uses: actions/cache@v2
with:
path: packages/shared/db/schema.prisma
key: ${{ runner.os }}-schema-${{ hashFiles('packages/shared/db/schema.prisma') }}
- name: Create planetscale password
uses: planetscale/create-branch-password-action@v1
id: password
if: ${{ steps.schema_hash.cache-hit != 'true' }}
with:
org_name: ${{ env.PLANETSCALE_ORG }}
database_name: ${{ env.PLANETSCALE_DB }}
branch_name: ${{ github.event.pull_request.head.ref }}
name: ${{ github.sha }}
env:
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }}
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }}
- run: touch .planetscale-db-created
if: ${{ steps.schema_hash.cache-hit != 'true' }}
- uses: pnpm/action-setup@v2
if: ${{ steps.schema_hash.cache-hit != 'true' }}
- name: Set up Node
if: ${{ steps.schema_hash.cache-hit != 'true' }}
env:
NODE_OPTIONS: "--max_old_space_size=5120"
uses: actions/setup-node@v3
with:
node-version: 16.x
cache: "pnpm"
- name: Get pnpm store directory
if: ${{ steps.schema_hash.cache-hit != 'true' }}
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- uses: actions/cache@v3
if: ${{ steps.schema_hash.cache-hit != 'true' }}
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-node-v3-${{ hashFiles('pnpm-lock.yaml') }}
restore-keys: ${{ runner.os }}-node-v3-
- run: pnpm install --no-optional
if: ${{ steps.schema_hash.cache-hit != 'true' }}
- name: prisma migrate
run: |
export DATABASE_URL=mysql://${{ steps.password.outputs.username }}:${{ steps.password.outputs.password }}@${{ steps.password.outputs.hostname }}/${{ env.PLANETSCALE_DB }}?sslaccept=strict
cd packages/flightdeck && pnpm blitz prisma db push
if: ${{ steps.schema_hash.cache-hit != 'true' }}
- name: Comment PR
uses: thollander/actions-comment-pull-request@v2
if: ${{ steps.ps_created.cache-hit != 'true' }}
with:
message: |
## PlanetScale
Branch: https://app.planetscale.com/${{ env.PLANETSCALE_ORG }}/${{ env.PLANETSCALE_DB }}/${{ github.event.pull_request.head.ref }}
Deploy request: https://app.planetscale.com/${{ env.PLANETSCALE_ORG }}/${{ env.PLANETSCALE_DB }}/deploy-requests/${{ steps.create_deploy_request.outputs.number }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment