-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(database): move from planetsacle to rds sniff (#105)
* chore(database): move from planetsacle to rds sniff * fix: doppler install * fix: migration diff step * fix: migration diff step n tests * fix: migration diff step n tests * fix: migration diff step n tests * fix: migration diff step n tests * fix: migration diff step n tests * fix: migration diff step n tests * fix: migration diff step n tests * fix: migration diff step n tests * fix: migration diff step n tests * fix: migration diff step n tests * fix: migration diff step n tests * fix: migration diff step n tests * fix: migration diff step n tests
- Loading branch information
1 parent
2d2d8c3
commit be32440
Showing
17 changed files
with
168 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,93 +6,75 @@ on: | |
paths: | ||
- 'packages/domain/prisma/migrations/**' | ||
|
||
env: | ||
PLANETSCALE_SERVICE_TOKEN_ID: ${{ secrets.PLANETSCALE_SERVICE_TOKEN_ID }} | ||
PLANETSCALE_SERVICE_TOKEN: ${{ secrets.PLANETSCALE_SERVICE_TOKEN }} | ||
|
||
jobs: | ||
migration-detail: | ||
runs-on: ubuntu-latest | ||
env: | ||
MYSQL_ROOT_PASSWORD: root | ||
MYSQL_DATABASE: db | ||
MYSQL_PORT: 3306 | ||
DATABASE_URL: mysql://root:[email protected]:3306/db | ||
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 | ||
run: | | ||
DEPLOY_REQUEST_STATE=$(pscale deploy-request show ${{ secrets.PLANETSCALE_DATABASE_NAME }} ${{ env.PSCALE_BRANCH_NAME }} --org ${{ secrets.PLANETSCALE_ORG_NAME }} -f json | jq -r '.state') | ||
echo "Deploy request State: $DEPLOY_REQUEST_STATE" | ||
if [ "$DEPLOY_REQUEST_STATE" = "open" ]; then | ||
echo "Deploy request exists: skipping creation" | ||
else | ||
echo "Deploy request does not exist : creating..." | ||
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" | ||
fi | ||
- name: Start MySQL server | ||
run: sudo systemctl start mysql.service | ||
|
||
- name: Get deploy request number | ||
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: Install dependencies | ||
uses: ./.github/actions/install-dependencies | ||
|
||
- name: Check deployment state | ||
continue-on-error: false | ||
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" | ||
- name: Run migrations | ||
run: yarn workspace @snipcode/domain db:migrate | ||
|
||
if [ "$DEPLOYMENT_STATE" = "ready" ]; then | ||
echo "Deployment state is ready. Continuing." | ||
echo "DEPLOY_REQUEST_OPENED=true" >> $GITHUB_ENV | ||
break | ||
fi | ||
- name: Install Doppler CLI | ||
uses: dopplerhq/cli-action@v3 | ||
|
||
echo "Deployment state is not ready. Waiting 2 seconds before checking again." | ||
sleep 2 | ||
done | ||
- name: Retrieve and export the production database URL | ||
run: echo "PROD_DATABASE_URL=$(doppler secrets get CONNECTION_STRING --plain)" >> $GITHUB_ENV | ||
env: | ||
DOPPLER_TOKEN: ${{ secrets.DOPPLER_DATABASE_TOKEN }} | ||
|
||
- name: Collect the migration diff | ||
continue-on-error: false | ||
if: ${{ env.DEPLOY_REQUEST_OPENED }} | ||
working-directory: packages/domain | ||
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 | ||
set -e | ||
echo "### Database Migration Detected" > migration-message.txt | ||
echo "The following migration will be applied to the production database:" >> 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 "\`\`\`sql" >> migration-message.txt | ||
yarn prisma migrate diff --from-url "$PROD_DATABASE_URL" --to-url "$DATABASE_URL" --script >> migration-message.txt | ||
echo "\`\`\`" >> migration-message.txt | ||
echo "" >> migration-message.txt | ||
echo "" >> migration-message.txt | ||
echo "Please ensure your schema changes are compatible with the application code currently running in production." >> migration-message.txt | ||
- name: Comment pull request with the migration diff | ||
uses: thollander/actions-comment-pull-request@v2 | ||
if: ${{ env.DEPLOY_REQUEST_OPENED }} | ||
- name: Create or update the pull request with the migration diff | ||
uses: actions/github-script@v6 | ||
id: plan-comment | ||
with: | ||
filePath: migration-message.txt | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const fs = require('fs'); | ||
const { data: comments } = await github.rest.issues.listComments({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: context.issue.number, | ||
}); | ||
const botComment = comments.find(comment => { | ||
return comment.user.type === 'Bot' && comment.body.includes('Database Migration Detected') | ||
}); | ||
const output = fs.readFileSync('packages/domain/migration-message.txt', 'utf8'); | ||
if (botComment) { | ||
github.rest.issues.deleteComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
comment_id: botComment.id, | ||
}); | ||
} | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: output | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
DATABASE_URL=mysql://root:@127.0.0.1:3311/core-db | ||
DATABASE_URL=mysql://root:secret@127.0.0.1:3311/snipcode | ||
SHADOW_DATABASE_URL=mysql://root:[email protected]:3312/snipcode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.