name: Reusable - Flyway Migration Check on: workflow_call: inputs: base_ref: description: 'The base ref for comparison' required: true type: string head_ref: description: 'The head ref for comparison' required: true type: string outputs: has_migrations: description: "Boolean indicating if migration files were changed" value: ${{ jobs.check-for-migrations.outputs.has_migrations }} jobs: check-for-migrations: name: Check for DB Migrations runs-on: ubuntu-latest outputs: has_migrations: ${{ steps.check_migrations.outputs.has_migrations }} steps: - name: Checkout Repository uses: actions/checkout@v6 with: fetch-depth: 0 - name: Detect Flyway Migration Changes id: check_migrations run: | echo "Comparing ${{ inputs.base_ref }}...${{ inputs.head_ref }}" if git diff --name-only ${{ inputs.base_ref }}...${{ inputs.head_ref }} | grep -q "booklore-api/src/main/resources/db/migration/V.*.sql"; then echo "Migration file changes detected. Proceeding with migration preview." echo "has_migrations=true" >> $GITHUB_OUTPUT else echo "No migration file changes detected. Skipping migration preview." echo "has_migrations=false" >> $GITHUB_OUTPUT fi flyway-migration-preview: name: Flyway DB Migration Preview needs: [ check-for-migrations ] if: needs.check-for-migrations.outputs.has_migrations == 'true' runs-on: ubuntu-latest services: mariadb: image: mariadb:10.6 env: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: booklore_test ports: - 3306:3306 options: >- --health-cmd="mysqladmin ping --silent" --health-interval=5s --health-timeout=5s --health-retries=10 steps: - name: Checkout Base Branch uses: actions/checkout@v6 with: ref: ${{ inputs.base_ref }} fetch-depth: 0 - name: Apply Migrations from Base Branch run: | echo "Applying migrations from base ref (${{ inputs.base_ref }})..." 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: Checkout Head Branch uses: actions/checkout@v6 with: ref: ${{ inputs.head_ref }} fetch-depth: 0 - name: Apply Migrations from Head Branch run: | echo "Applying new migrations from head ref (${{ inputs.head_ref }})..." 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."