Add Flyway migration conflict detection in CI (#1888)

* Add Flyway migration conflict detection in CI

* Update flyway version

---------

Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
ACX
2025-12-14 12:16:16 -07:00
committed by GitHub
parent f602137bc7
commit 3f14376a3a

View File

@@ -10,62 +10,86 @@ on:
- '**' - '**'
jobs: jobs:
flyway-conflict-check: check-for-migrations:
name: Check for DB Migrations
if: github.event_name == 'pull_request' && ((github.base_ref == 'master' && github.head_ref == 'develop') || github.base_ref == 'develop')
runs-on: ubuntu-latest runs-on: ubuntu-latest
outputs:
has_migrations: ${{ steps.check_migrations.outputs.has_migrations }}
steps: steps:
- name: Checkout PR branch - name: Checkout Repository for Diff
uses: actions/checkout@v6 uses: actions/checkout@v6
with: with:
fetch-depth: 0 fetch-depth: 0
- name: Determine comparison reference - name: Detect Flyway Migration Changes
id: compare_ref
run: |
bash .github/scripts/determine-compare-ref.sh "${{ github.base_ref }}"
- name: Check for migration changes
id: check_migrations id: check_migrations
if: steps.compare_ref.outputs.has_ref == 'true'
run: | run: |
# Check for changes in the migration directory # Compare PR head with the target base branch
if git diff --name-only --diff-filter=AMRCD ${{ env.COMPARE_REF }}...HEAD | grep -q "booklore-api/src/main/resources/db/migration/"; then if git diff --name-only origin/${{ github.base_ref }}...HEAD | grep -q "booklore-api/src/main/resources/db/migration/V.*.sql"; then
echo "Migration file changes detected." echo "Migration file changes detected. Proceeding with migration preview."
echo "has_migrations=true" >> $GITHUB_OUTPUT echo "has_migrations=true" >> $GITHUB_OUTPUT
else else
echo "No migration file changes detected. Skipping conflict check." echo "No migration file changes detected. Skipping migration preview."
echo "has_migrations=false" >> $GITHUB_OUTPUT echo "has_migrations=false" >> $GITHUB_OUTPUT
fi fi
- name: Analyze migration changes flyway-migration-preview:
if: steps.check_migrations.outputs.has_migrations == 'true' name: Flyway DB Migration Preview
id: analyze_changes needs: [check-for-migrations]
if: needs.check-for-migrations.outputs.has_migrations == 'true'
runs-on: ubuntu-latest
services:
mariadb:
image: mariadb:10.6
env: env:
COMPARE_REF: ${{ env.COMPARE_REF }} MYSQL_ROOT_PASSWORD: root
run: | MYSQL_DATABASE: booklore_test
bash .github/scripts/analyze-changes.sh ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping --silent"
--health-interval=5s
--health-timeout=5s
--health-retries=10
- name: Extract and validate versions steps:
if: steps.check_migrations.outputs.has_migrations == 'true' - name: Checkout Base Branch
id: validate_versions uses: actions/checkout@v6
run: | with:
bash .github/scripts/validate-versions.sh ref: '${{ github.base_ref }}'
- name: Check for version conflicts with base - name: Apply Migrations from Base Branch
if: steps.validate_versions.outputs.has_versions == 'true'
env:
COMPARE_REF: ${{ env.COMPARE_REF }}
run: | run: |
bash .github/scripts/check-conflicts.sh echo "Applying migrations from '${{ github.base_ref }}' branch..."
docker run --network host \
-v ${{ github.workspace }}:/flyway/sql \
flyway/flyway:11.19.0-alpine \
-url=jdbc:mariadb://127.0.0.1:3306/booklore_test \
-user=root -password=root \
-locations=filesystem:/flyway/sql/booklore-api/src/main/resources/db/migration \
migrate
- name: Cleanup - name: Checkout Pull Request Branch
if: always() && steps.check_migrations.outputs.has_migrations == 'true' uses: actions/checkout@v6
- name: Apply Migrations from PR Branch
run: | run: |
rm -f /tmp/all_changes.txt /tmp/pr_files.txt /tmp/base_files.txt echo "Applying new migrations from PR branch..."
rm -f /tmp/versions_*.txt /tmp/duplicates_in_pr.txt docker run --network host \
-v ${{ github.workspace }}:/flyway/sql \
flyway/flyway:11.19.0-alpine \
-url=jdbc:mariadb://127.0.0.1:3306/booklore_test \
-user=root -password=root \
-locations=filesystem:/flyway/sql/booklore-api/src/main/resources/db/migration \
migrate
- name: Confirm Flyway Dry Run Success
run: echo "✅ Flyway migration preview successful. Migrations can be applied cleanly."
build-and-push: build-and-push:
needs: flyway-conflict-check needs: [check-for-migrations, flyway-migration-preview]
if: success() || (needs.flyway-conflict-check.result == 'skipped') if: always() && (needs.flyway-migration-preview.result == 'success' || needs.flyway-migration-preview.result == 'skipped')
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions: permissions:
@@ -81,33 +105,33 @@ jobs:
with: with:
fetch-depth: 0 fetch-depth: 0
- name: Log in to Docker Hub - name: Authenticate to Docker Hub
uses: docker/login-action@v2 uses: docker/login-action@v2
with: with:
username: ${{ secrets.DOCKER_USERNAME }} username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }} password: ${{ secrets.DOCKER_PASSWORD }}
- name: Log in to GitHub Container Registry - name: Authenticate to GitHub Container Registry
uses: docker/login-action@v2 uses: docker/login-action@v2
with: with:
registry: ghcr.io registry: ghcr.io
username: ${{ github.actor }} username: ${{ github.actor }}
password: ${{ github.token }} password: ${{ github.token }}
- name: Set up QEMU for multi-arch builds - name: Set Up QEMU for Multi-Architecture Builds
uses: docker/setup-qemu-action@v3 uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx - name: Set Up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
- name: Set up JDK 21 - name: Set Up JDK 21
uses: actions/setup-java@v4 uses: actions/setup-java@v4
with: with:
java-version: '21' java-version: '21'
distribution: 'temurin' distribution: 'temurin'
cache: 'gradle' cache: 'gradle'
- name: Run Backend Tests - name: Execute Backend Tests
id: backend_tests id: backend_tests
working-directory: ./booklore-api working-directory: ./booklore-api
run: | run: |
@@ -115,7 +139,7 @@ jobs:
./gradlew test --no-daemon --parallel --build-cache ./gradlew test --no-daemon --parallel --build-cache
continue-on-error: true continue-on-error: true
- name: Publish Test Results - name: Publish Backend Test Results
uses: EnricoMi/publish-unit-test-result-action@v2 uses: EnricoMi/publish-unit-test-result-action@v2
if: always() if: always()
with: with:
@@ -125,7 +149,7 @@ jobs:
report_individual_runs: true report_individual_runs: true
report_suite_logs: 'any' report_suite_logs: 'any'
- name: Upload Test Reports - name: Upload Backend Test Reports
uses: actions/upload-artifact@v4 uses: actions/upload-artifact@v4
if: always() if: always()
with: with:
@@ -135,13 +159,13 @@ jobs:
booklore-api/build/test-results/ booklore-api/build/test-results/
retention-days: 30 retention-days: 30
- name: Check Test Results - name: Validate Backend Test Results
if: steps.backend_tests.outcome == 'failure' if: steps.backend_tests.outcome == 'failure'
run: | run: |
echo "❌ Backend tests failed! Check the test results above." echo "❌ Backend tests failed! Check the test results above."
exit 1 exit 1
- name: Get Latest Master Version - name: Retrieve Latest Master Version Tag
id: get_version id: get_version
run: | run: |
latest_tag=$(git tag --list "v*" --sort=-v:refname | head -n 1) latest_tag=$(git tag --list "v*" --sort=-v:refname | head -n 1)
@@ -149,7 +173,7 @@ jobs:
echo "latest_tag=$latest_tag" >> $GITHUB_ENV echo "latest_tag=$latest_tag" >> $GITHUB_ENV
echo "Latest master tag: $latest_tag" echo "Latest master tag: $latest_tag"
- name: Determine Version Bump (Only for Master) - name: Determine Version Bump (Master Only)
if: github.ref == 'refs/heads/master' if: github.ref == 'refs/heads/master'
id: determine_bump id: determine_bump
env: env:
@@ -236,7 +260,7 @@ jobs:
type=gha,mode=max type=gha,mode=max
type=registry,ref=ghcr.io/booklore-app/booklore:buildcache,mode=max type=registry,ref=ghcr.io/booklore-app/booklore:buildcache,mode=max
- name: Update Release Draft (Only for Master) - name: Update GitHub Release Draft (Master Only)
if: github.ref == 'refs/heads/master' if: github.ref == 'refs/heads/master'
uses: release-drafter/release-drafter@v6 uses: release-drafter/release-drafter@v6
with: with:
@@ -245,7 +269,7 @@ jobs:
env: env:
GITHUB_TOKEN: ${{ github.token }} GITHUB_TOKEN: ${{ github.token }}
- name: Publish Draft Release (Only for Master) - name: Publish GitHub Draft Release (Master Only)
if: github.ref == 'refs/heads/master' if: github.ref == 'refs/heads/master'
env: env:
GITHUB_TOKEN: ${{ github.token }} GITHUB_TOKEN: ${{ github.token }}