feat(ci): dump current db schema automatically #5
Workflow file for this run
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
name: Generate and Validate Flyway Schema | |
on: | |
pull_request: | |
types: [opened, synchronize, labeled] | |
paths: | |
- 'backend/schema/**' | |
- '.github/workflows/schema-dump.yml' | |
workflow_dispatch: | |
jobs: | |
generate-schema: | |
runs-on: ubuntu-latest | |
# Explicitly set permissions needed for committing | |
permissions: | |
contents: write | |
pull-requests: read | |
services: | |
postgres: | |
image: postgres:16 | |
env: | |
POSTGRES_PASSWORD: postgres | |
POSTGRES_DB: postgres | |
POSTGRES_USER: postgres | |
options: >- | |
--health-cmd pg_isready | |
--health-interval 10s | |
--health-timeout 5s | |
--health-retries 5 | |
ports: | |
- 5432:5432 | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.head_ref }} | |
fetch-depth: 0 | |
- name: Run Flyway migrations | |
uses: docker://flyway/flyway:10-alpine | |
env: | |
FLYWAY_URL: jdbc:postgresql://postgres:5432/postgres | |
FLYWAY_USER: postgres | |
FLYWAY_PASSWORD: postgres | |
with: | |
args: -locations=filesystem:./backend/src/main/resources/db/migration/ migrate | |
- name: Generate new schema | |
uses: docker://postgres:16 | |
with: | |
args: > | |
bash -c "PGPASSWORD=postgres pg_dump | |
-h postgres | |
-U postgres | |
-d postgres | |
--schema-only | |
> /github/workspace/backend/docs/schema.sql" | |
- name: Ensure schema file is tracked | |
run: git add -f backend/docs/schema.sql | |
- name: Check for schema changes | |
id: check-changes | |
run: | | |
if git diff --quiet backend/docs/schema.sql; then | |
echo "Schema is unchanged" | |
echo "changed=false" >> $GITHUB_OUTPUT | |
else | |
echo "Schema has changes" | |
echo "changed=true" >> $GITHUB_OUTPUT | |
fi | |
- name: Handle schema changes | |
if: steps.check-changes.outputs.changed == 'true' | |
run: | | |
if [[ "${{ contains(github.event.pull_request.labels.*.name, 'update-schema') }}" == "true" ]]; then | |
# Update schema if label is present | |
git config --global user.name 'GitHub Action' | |
git config --global user.email '[email protected]' | |
git add backend/docs/schema.sql | |
git commit -m "Update schema documentation based on migration changes" | |
git push | |
else | |
echo "Error: Schema changes detected but 'update-schema' label is not present on the PR" | |
echo "Please add the 'update-schema' label if these changes are intentional" | |
exit 1 | |
fi |