From dfec3e1577171a8a0c27afab628d35721b05345b Mon Sep 17 00:00:00 2001 From: Eric Cabrel TIOGO Date: Sat, 22 Jun 2024 16:56:09 +0200 Subject: [PATCH] build: ensure the migration diff is displayed --- .github/workflows/deploy-migration.yml | 204 ---------------------- .github/workflows/migration-db-check.yml | 98 +++++++++++ .github/workflows/migration-db-deploy.yml | 98 +++++++++++ 3 files changed, 196 insertions(+), 204 deletions(-) delete mode 100644 .github/workflows/deploy-migration.yml create mode 100644 .github/workflows/migration-db-check.yml create mode 100644 .github/workflows/migration-db-deploy.yml diff --git a/.github/workflows/deploy-migration.yml b/.github/workflows/deploy-migration.yml deleted file mode 100644 index ea39588b..00000000 --- a/.github/workflows/deploy-migration.yml +++ /dev/null @@ -1,204 +0,0 @@ -name: Deploy database migration -on: - pull_request: # TODO to be removed - branches: - - 'main' - paths: - - 'packages/domain/prisma/migrations/**' - push: - branches: - - 'main' - - 'dev' - paths: - - 'packages/domain/prisma/migrations/**' - -jobs: - - migration-detail: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Setup pscale - uses: planetscale/setup-pscale-action@v1 - - - name: Set database branch name - run: echo "PSCALE_BRANCH_NAME=$(echo ${{ github.head_ref }} | tr -cd '[:alnum:]-'| tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV - -# - name: Create deploy request on development branch -# env: -# PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} -# PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} -# run: | -# pscale deploy-request create ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} \ -# --org ${{ secrets.PLANETSCALE_ORG_NAME }} \ -# --into dev \ -# --notes "Apply changes to the dev branch" - - - - name: Get deploy request number - env: - PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} - PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} - run: | - DEPLOY_REQUEST_NUMBER=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} -f json | jq -r '.number') - echo "DEPLOY_REQUEST_NUMBER=$DEPLOY_REQUEST_NUMBER" >> $GITHUB_ENV - - - name: Check deployment state - continue-on-error: false - env: - PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} - PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} - run: | - for i in {1..10}; do - DEPLOYMENT_STATE=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.DEPLOY_REQUEST_NUMBER }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} --format json | jq -r '.deployment_state') - echo "Deployment State: $DEPLOYMENT_STATE" - - if [ "$DEPLOYMENT_STATE" = "ready" ]; then - echo "Deployment state is ready. Continuing." - echo "DEPLOY_REQUEST_OPENED=true" >> $GITHUB_ENV - break - fi - - echo "Deployment state is not ready. Waiting 2 seconds before checking again." - sleep 2 - done - - - - name: Collect the migration diff - continue-on-error: false - if: ${{ env.DEPLOY_REQUEST_OPENED }} - env: - PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} - PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} - run: | - deploy_data=$(pscale api organizations/${{ secrets.PLANETSCALE_ORG_NAME }}/databases/${{ secrets.PLANETSCALE_DATABASE_NAME }}/deploy-requests/${{ env.DEPLOY_REQUEST_NUMBER }}/deployment --org planetscale) - can_drop_data=$(echo "$deploy_data" | jq -r '.deploy_operations[] | select(.can_drop_data == true) | .can_drop_data') - - - echo "Deploy request opened: https://app.planetscale.com/${{ secrets.PLANETSCALE_ORG_NAME }}/${{ secrets.PLANETSCALE_DATABASE_NAME }}/deploy-requests/${{ env.DEPLOY_REQUEST_NUMBER }}" >> migration-message.txt - echo "" >> migration-message.txt - - - if [ "$can_drop_data" = "true" ]; then - echo ":rotating_light: You are dropping a column. Before running the migration make sure to do the following:" >> migration-message.txt - echo "" >> migration-message.txt - - - echo "1. [ ] Deploy app changes to ensure the column is no longer being used." >> migration-message.txt - echo "2. [ ] Once you've verified it's no used, run the deploy request." >> migration-message.txt - echo "" >> migration-message.txt - else - echo "When adding to the schema, the Deploy Request must be run **before** the code is deployed." >> migration-message.txt - echo "Please ensure your schema changes are compatible with the application code currently running in production." >> migration-message.txt - echo "" >> migration-message.txt - - - echo "1. [ ] Successfully run the Deploy Request" >> migration-message.txt - echo "2. [ ] Deploy this PR" >> migration-message.txt - echo "" >> migration-message.txt - fi - - - echo "\`\`\`diff" >> migration-message.txt - pscale deploy-request diff ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.DEPLOY_REQUEST_NUMBER }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} -f json | jq -r '.[].raw' >> migration-message.txt - echo "\`\`\`" >> migration-message.txt - - - name: Comment pull request with the migration diff - uses: thollander/actions-comment-pull-request@v2 - if: ${{ env.DEPLOY_REQUEST_OPENED }} - with: - filePath: migration-message.txt - - apply-migration: - if: github.ref == 'refs/heads/main' - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Setup pscale - uses: planetscale/setup-pscale-action@v1 - - - name: Set database branch name - run: echo "PSCALE_BRANCH_NAME=$(echo ${{ github.head_ref }} | tr -cd '[:alnum:]-'| tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV - - - name: Apply migration in Production - env: - PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} - PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} - run: | - pscale deploy-request create ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} \ - --into main \ - --notes "Deploy migration to production branch" - - - name: Get the deploy request number - env: - PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} - PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} - run: | - DEPLOY_REQUEST_NUMBER=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} -f json | jq -r '.number') - echo "DEPLOY_REQUEST_NUMBER=$DEPLOY_REQUEST_NUMBER" >> $GITHUB_ENV - - - name: Check deployment state - id: check-state - env: - PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} - PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} - run: | - for i in {1..10}; do - DEPLOYMENT_STATE=$(pscale deploy-request show ${{ secrets.PLANETSCALE_ORG_NAME }} ${{ env.DEPLOY_REQUEST_NUMBER }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} --format json | jq -r '.deployment_state') - echo "Deployment State: $DEPLOYMENT_STATE" - - if [ "$DEPLOYMENT_STATE" = "ready" ]; then - echo "Deployment state is ready. Continuing." - echo "DEPLOY_REQUEST_OPENED=true" >> $GITHUB_ENV - break - fi - - echo "Deployment state is not ready. Waiting 2 seconds before checking again." - sleep 2 - done - - - name: Deploy schema migrations - continue-on-error: false - env: - PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} - PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} - run: | - pscale deploy-request deploy ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.DEPLOY_REQUEST_NUMBER }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} --wait - - - name: Apply migration in Development - continue-on-error: false - env: - PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} - PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} - run: | - pscale deploy-request create ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} \ - --into dev \ - --notes "Deploy migration to development branch" - - DEV_DEPLOY_REQUEST_NUMBER=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} -f json | jq -r '.number') - - for i in {1..10}; do - DEPLOYMENT_STATE=$(pscale deploy-request show ${{ secrets.PLANETSCALE_ORG_NAME }} $DEV_DEPLOY_REQUEST_NUMBER --org ${{ secrets.PLANETSCALE_ORG_NAME }} --format json | jq -r '.deployment_state') - echo "Deployment State: $DEPLOYMENT_STATE" - - if [ "$DEPLOYMENT_STATE" = "ready" ]; then - echo "Deployment state is ready. Continuing." - - break - fi - - echo "Deployment state is not ready. Waiting 2 seconds before checking again." - sleep 2 - done - - pscale deploy-request deploy ${{ secrets.PLANETSCALE_DATABASE_NAME }} $DEV_DEPLOY_REQUEST_NUMBER --org ${{ secrets.PLANETSCALE_ORG_NAME }} --wait - - - name: Delete the database branch - env: - PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} - PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} - run: | - pscale branch delete ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} --force - echo "The branch \"${{ env.PSCALE_BRANCH_NAME }}\" has been successfully." diff --git a/.github/workflows/migration-db-check.yml b/.github/workflows/migration-db-check.yml new file mode 100644 index 00000000..45fbf965 --- /dev/null +++ b/.github/workflows/migration-db-check.yml @@ -0,0 +1,98 @@ +name: Check database migration +on: + pull_request: + branches: + - 'main' + paths: + - 'packages/domain/prisma/migrations/**' + +jobs: + migration-detail: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Setup pscale + uses: planetscale/setup-pscale-action@v1 + + - name: Set database branch name + run: echo "PSCALE_BRANCH_NAME=$(echo ${{ github.head_ref }} | tr -cd '[:alnum:]-'| tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV + + - name: Create deploy request on development branch + env: + PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} + PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} + run: | + pscale deploy-request create ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} \ + --org ${{ secrets.PLANETSCALE_ORG_NAME }} \ + --into dev \ + --notes "Apply changes to the dev branch" + + - name: Get deploy request number + env: + PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} + PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} + run: | + DEPLOY_REQUEST_NUMBER=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} -f json | jq -r '.number') + echo "DEPLOY_REQUEST_NUMBER=$DEPLOY_REQUEST_NUMBER" >> $GITHUB_ENV + + - name: Check deployment state + continue-on-error: false + env: + PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} + PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} + run: | + for i in {1..10}; do + DEPLOYMENT_STATE=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.DEPLOY_REQUEST_NUMBER }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} --format json | jq -r '.deployment_state') + echo "Deployment State: $DEPLOYMENT_STATE" + + if [ "$DEPLOYMENT_STATE" = "ready" ]; then + echo "Deployment state is ready. Continuing." + echo "DEPLOY_REQUEST_OPENED=true" >> $GITHUB_ENV + break + fi + + echo "Deployment state is not ready. Waiting 2 seconds before checking again." + sleep 2 + done + + - name: Collect the migration diff + continue-on-error: false + if: ${{ env.DEPLOY_REQUEST_OPENED }} + env: + PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} + PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} + run: | + deploy_data=$(pscale api organizations/${{ secrets.PLANETSCALE_ORG_NAME }}/databases/${{ secrets.PLANETSCALE_DATABASE_NAME }}/deploy-requests/${{ env.DEPLOY_REQUEST_NUMBER }}/deployment --org planetscale) + can_drop_data=$(echo "$deploy_data" | jq -r '.deploy_operations[] | select(.can_drop_data == true) | .can_drop_data') + + echo "Deploy request opened: https://app.planetscale.com/${{ secrets.PLANETSCALE_ORG_NAME }}/${{ secrets.PLANETSCALE_DATABASE_NAME }}/deploy-requests/${{ env.DEPLOY_REQUEST_NUMBER }}" >> migration-message.txt + echo "" >> migration-message.txt + + if [ "$can_drop_data" = "true" ]; then + echo ":rotating_light: You are dropping a column. Before running the migration make sure to do the following:" >> migration-message.txt + echo "" >> migration-message.txt + + echo "1. [ ] Deploy app changes to ensure the column is no longer being used." >> migration-message.txt + echo "2. [ ] Once you've verified it's no used, run the deploy request." >> migration-message.txt + echo "" >> migration-message.txt + else + echo "When adding to the schema, the Deploy Request must be run **before** the code is deployed." >> migration-message.txt + echo "Please ensure your schema changes are compatible with the application code currently running in production." >> migration-message.txt + echo "" >> migration-message.txt + + echo "1. [ ] Successfully run the Deploy Request" >> migration-message.txt + echo "2. [ ] Deploy this PR" >> migration-message.txt + echo "" >> migration-message.txt + fi + + echo "\`\`\`diff" >> migration-message.txt + pscale deploy-request diff ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.DEPLOY_REQUEST_NUMBER }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} -f json | jq -r '.[].raw' >> migration-message.txt + echo "\`\`\`" >> migration-message.txt + + + - name: Comment pull request with the migration diff + uses: thollander/actions-comment-pull-request@v2 + if: ${{ env.DEPLOY_REQUEST_OPENED }} + with: + filePath: migration-message.txt diff --git a/.github/workflows/migration-db-deploy.yml b/.github/workflows/migration-db-deploy.yml new file mode 100644 index 00000000..c78b3890 --- /dev/null +++ b/.github/workflows/migration-db-deploy.yml @@ -0,0 +1,98 @@ +name: Deploy database migration +on: +# pull_request: # TODO to be removed +# branches: +# - 'main' +# paths: +# - 'packages/domain/prisma/migrations/**' + push: + branches: + - 'main' + - 'dev' + paths: + - 'packages/domain/prisma/migrations/**' + +jobs: + apply-migration: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Setup pscale + uses: planetscale/setup-pscale-action@v1 + + - name: Set database branch name + run: echo "PSCALE_BRANCH_NAME=$(echo ${{ github.head_ref }} | tr -cd '[:alnum:]-'| tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV + + - name: Get the deploy request number in development branch + env: + PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} + PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} + run: | + DEV_DEPLOY_REQUEST_NUMBER=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} -f json | jq -r '.number') + echo "DEV_DEPLOY_REQUEST_NUMBER=$DEV_DEPLOY_REQUEST_NUMBER" >> $GITHUB_ENV + + - name: Check deployment state + continue-on-error: false + env: + PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} + PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} + run: | + for i in {1..10}; do + DEPLOYMENT_STATE=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.DEV_DEPLOY_REQUEST_NUMBER }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} --format json | jq -r '.deployment_state') + echo "Deployment State: $DEPLOYMENT_STATE" + + if [ "$DEPLOYMENT_STATE" = "ready" ]; then + echo "Deployment state is ready. Continuing." + echo "DEPLOY_REQUEST_OPENED=true" >> $GITHUB_ENV + break + fi + + echo "Deployment state is not ready. Waiting 2 seconds before checking again." + sleep 2 + done + + - name: Deploy schema migration in the development branch + continue-on-error: false + env: + PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} + PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} + run: | + pscale deploy-request deploy ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.DEV_DEPLOY_REQUEST_NUMBER }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} --wait + + - name: Deploy schema migration in the production branch + if: ${{ success() }} + continue-on-error: false + env: + PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} + PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} + run: | + pscale deploy-request create ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} \ + --into main \ + --notes "Apply changes to the production branch" + + PROD_DEPLOY_REQUEST_NUMBER=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} -f json | jq -r '.number') + + for i in {1..10}; do + DEPLOYMENT_STATE=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} $PROD_DEPLOY_REQUEST_NUMBER --org ${{ secrets.PLANETSCALE_ORG_NAME }} --format json | jq -r '.deployment_state') + echo "Deployment State: $DEPLOYMENT_STATE" + + if [ "$DEPLOYMENT_STATE" = "ready" ]; then + echo "Deployment state is ready. Continuing." + break + fi + + echo "Deployment state is not ready. Waiting 2 seconds before checking again." + sleep 2 + done + + pscale deploy-request deploy ${{ secrets.PLANETSCALE_DATABASE_NAME }} $PROD_DEPLOY_REQUEST_NUMBER --org ${{ secrets.PLANETSCALE_ORG_NAME }} --wait + + - name: Delete the database branch + if: ${{ success() }} + env: + PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} + PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} + run: | + pscale branch delete ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} --force + echo "The branch \"${{ env.PSCALE_BRANCH_NAME }}\" has been successfully."