-
Notifications
You must be signed in to change notification settings - Fork 35
chore: adds db migrations to ci/cd #5563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+153
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3c615e0
chore: adds db migrations to ci/cd
ravenac95 f20e87e
Potential fix for code scanning alert no. 21: Workflow does not conta…
ravenac95 c3eca5e
fix: fix usage of cli
ravenac95 2bcaf7b
more fixes
ravenac95 7895fa2
fix: ensure proper conditions for running on merge_group only
ravenac95 fb3f4ab
fix: add automatic backup before migrations run
ravenac95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #!/bin/bash | ||
| set -euxo pipefail | ||
|
|
||
| # Arguments | ||
| WORKDIR=$1 | ||
| SUPABASE_DB_URL=$2 | ||
|
|
||
| if [ -z "$WORKDIR" ]; then | ||
| echo "Error: WORKDIR is required." | ||
| exit 1 | ||
| fi | ||
|
|
||
| pushd "$WORKDIR" | ||
|
|
||
| if [ -z "$SUPABASE_DB_URL" ]; then | ||
| echo "Error: SUPABASE_DB_URL is required." | ||
| exit 1 | ||
| fi | ||
|
|
||
| set +u | ||
| if [ -z "$GITHUB_OUTPUT" ]; then | ||
| echo "GITHUB_OUTPUT is not set. Using /dev/null for local testing." | ||
| GITHUB_OUTPUT="/dev/null" | ||
| fi | ||
| set -u | ||
|
|
||
| # Allow dry run mode for testing | ||
| DRY_RUN=${DRY_RUN:-false} | ||
|
|
||
| echo "Checking for pending migrations..." | ||
|
|
||
| # Capture output from dry-run | ||
| # pnpm supabase needs to be run where supabase config/migrations are (apps/frontend) | ||
| # usage of 2>&1 to capture stderr as well | ||
| OUTPUT=$(pnpm supabase db push --dry-run --db-url "$SUPABASE_DB_URL" 2>&1) || true | ||
| echo "$OUTPUT" | ||
|
|
||
| if echo "$OUTPUT" | grep -q "Remote database is up to date"; then | ||
| echo "No migrations needed." | ||
| echo "needs_migration=false" >> $GITHUB_OUTPUT | ||
| elif echo "$OUTPUT" | grep -q "Would push these migrations"; then | ||
| echo "Migrations needed. Proceeding with backup." | ||
| echo "needs_migration=true" >> $GITHUB_OUTPUT | ||
|
|
||
| # Perform backup | ||
| TIMESTAMP=$(date -u +"%Y%m%d%H%M%S") | ||
| BACKUP_FILE="${TIMESTAMP}Z_backup.sql.gz" | ||
|
|
||
| if [[ "$DRY_RUN" == "true" ]]; then | ||
| echo "Dry run enabled, skipping actual backup creation." | ||
| echo "backup_file=$BACKUP_FILE" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "Running pg_dump..." | ||
| pg_dump "$SUPABASE_DB_URL" | gzip > "$BACKUP_FILE" | ||
| echo "Creating backup file: $BACKUP_FILE" | ||
| fi | ||
|
|
||
| echo "Backup created at $BACKUP_FILE" | ||
| echo "backup_file=$BACKUP_FILE" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "Error: Unexpected output from dry run." | ||
| echo "$OUTPUT" | ||
| exit 1 | ||
| fi | ||
|
|
||
| popd | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| name: Supabase DB Migration | ||
| permissions: | ||
| contents: read | ||
| id-token: write | ||
|
|
||
| # We need to match both pull_request and merge_group events to ensure that | ||
| # we stop broken migrations from being merged into main. This ensures that | ||
| # we can add this workflow as a required check. Without the pull_request event, | ||
| # we, oddly, would not be able to add this as a required check on and the | ||
| # merge_group. | ||
| on: | ||
| pull_request: | ||
| branches: | ||
| - main | ||
| merge_group: | ||
| branches: | ||
| - main | ||
|
|
||
| jobs: | ||
| migrate-db: | ||
| runs-on: ubuntu-latest | ||
| environment: deploy | ||
| steps: | ||
| - uses: actions/checkout@v3 | ||
| if: ${{ github.event_name == 'merge_group' }} | ||
|
|
||
| - name: Authenticate to Google Cloud | ||
| uses: google-github-actions/auth@v2 | ||
| with: | ||
| credentials_json: '${{ secrets.GOOGLE_CREDENTIALS_JSON }}' | ||
| if: ${{ github.event_name == 'merge_group' }} | ||
|
|
||
| - name: Set up Cloud SDK | ||
| uses: google-github-actions/setup-gcloud@v2 | ||
| with: | ||
| version: '>= 363.0.0' | ||
| if: ${{ github.event_name == 'merge_group' }} | ||
|
|
||
| - name: Setup pnpm | ||
| uses: pnpm/action-setup@v4 | ||
| with: | ||
| version: 10 | ||
| run_install: | | ||
| - recursive: true | ||
| args: [--frozen-lockfile, --strict-peer-dependencies] | ||
| if: ${{ github.event_name == 'merge_group' }} | ||
|
|
||
| - name: Set up Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: "22.x" | ||
| cache: "pnpm" | ||
| if: ${{ github.event_name == 'merge_group' }} | ||
|
|
||
| - name: Install postgresql-client | ||
| run: | | ||
| sudo apt-get update | ||
| sudo apt-get install -y postgresql-client | ||
| if: ${{ github.event_name == 'merge_group' }} | ||
|
|
||
| - name: Check migrations and backup | ||
| id: check_migrations | ||
| run: | | ||
| bash .github/scripts/check-migrations-and-backup.sh "apps/frontend" "$SUPABASE_DB_URL" | ||
| env: | ||
| SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }} | ||
| SUPABASE_DB_URL: ${{ secrets.SUPABASE_PRODUCTION_DB_URL }} | ||
| if: ${{ github.event_name == 'merge_group' }} | ||
|
|
||
| - name: Upload backup to GCS | ||
| working-directory: apps/frontend | ||
| run: | | ||
| BACKUP_FILE="${{ steps.check_migrations.outputs.backup_file }}" | ||
| echo "Uploading backup ${BACKUP_FILE} to gs://${DB_BACKUP_BUCKET}/${BACKUP_FILE}" | ||
| gcloud storage cp "${BACKUP_FILE}" "gs://${DB_BACKUP_BUCKET}/${BACKUP_FILE}" | ||
| env: | ||
| DB_BACKUP_BUCKET: ${{ secrets.DB_BACKUP_BUCKET }} | ||
| if: ${{ github.event_name == 'merge_group' && steps.check_migrations.outputs.needs_migration == 'true' }} | ||
|
|
||
| - name: Push Supabase migrations | ||
| working-directory: apps/frontend | ||
| run: | | ||
| pnpm supabase db push --db-url "$SUPABASE_DB_URL" | ||
| env: | ||
| SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }} | ||
| SUPABASE_DB_URL: ${{ secrets.SUPABASE_PRODUCTION_DB_URL }} | ||
| if: ${{ github.event_name == 'merge_group' && steps.check_migrations.outputs.needs_migration == 'true' }} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At some point we might need to remove this as it could take a long time. Not for the near future tho.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya haha. We are at 1G for now. Also, this is conditional atm so I'm hoping this isn't too frustrating.