Skip to content

Standardize table names #46

Standardize table names

Standardize table names #46

Workflow file for this run

name: Standup Database and Test Migration
on:
pull_request:
branches: [ main ]
paths:
- 'users-api/src/migrations/**'
jobs:
check-migrations:
runs-on: ubuntu-latest
env:
GCP_PROEJCT_ID: "database-migration-anonymized"
DB_HOST: "localhost"
POSTGRES_PASSWORD: '${{ secrets.DB_PASSWORD }}'
DB_USERNAME: "postgres"
POSTGRES_DB: "postgres"
DB_BACKUP_URL: "https://storage.googleapis.com/anonymize-db-backups/us-west1/backup.sql"
DB_PASSWORD: '${{ secrets.DB_PASSWORD }}'
PGPASSWORD: '${{ secrets.DB_PASSWORD }}'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Build PostgreSQL container
run: |
docker build -t postgres-migration:latest ./db
- name: Start PostgreSQL container
run: |
docker run -d --name postgres-migration \
-e POSTGRES_PASSWORD="${POSTGRES_PASSWORD}" \
-e POSTGRES_USER="${DB_USERNAME}" \
-e POSTGRES_DB="${POSTGRES_DB}" \
-e RESTORE_FROM_BACKUP=True \
-e DB_BACKUP_URL="${DB_BACKUP_URL}" \
-p 5432:5432 \
postgres-migration:latest
- name: Install Sequelize CLI and dependencies
run: |
cd ./users-api/src
npm install
- name: Wait for Postgres to be ready
run: |
for i in {1..10}; do
nc -z localhost 5432 && echo "Postgres is up" && break
echo "Waiting for Postgres..."
sleep 10
done
- name: Run Migrations
run: |
cd ./users-api/src
if npx sequelize-cli db:migrate; then
echo "PR_COMMENT=:white_check_mark: Database migration succeeded!" >> $GITHUB_ENV
echo "MIGRATION_FAILED=false" >> $GITHUB_ENV
else
echo "PR_COMMENT=:x: Database migration failed." >> $GITHUB_ENV
echo "MIGRATION_FAILED=true" >> $GITHUB_ENV
fi
continue-on-error: true
- name: Cleanup
run: docker stop postgres-migration
- name: Check for existing comment
id: check_comment
run: |
comment_id=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments \
| jq '.[] | select(.user.login == "github-actions[bot]" and .body | test("Database migration")) | .id')
echo "::set-output name=comment_id::$comment_id"
continue-on-error: true
- name: Post or update PR comment based on migration result
run: |
if [[ -z "${{ steps.check_comment.outputs.comment_id }}" ]]; then
# Create a new comment
curl -X POST \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments \
-d "{\"body\":\"${{ env.PR_COMMENT }}\"}"
else
# Update the existing comment
curl -X PATCH \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/comments/${{ steps.check_comment.outputs.comment_id }} \
-d "{\"body\":\"${{ env.PR_COMMENT }}\"}"
fi
- name: Exit with appropriate status
if: always()
run: |
if [[ "${{ env.MIGRATION_FAILED }}" == "true" ]]; then
exit 1
fi