Skip to content

Added new workflow to check files in the migrations folder #21

Added new workflow to check files in the migrations folder

Added new workflow to check files in the migrations folder #21

name: Flyway Migration Files
on:
pull_request:
branches:
- '**'
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout current branch
uses: actions/checkout@v3
- name: Fetch base branch
run: |
git fetch origin +refs/heads/${{ github.base_ref }}:refs/remotes/origin/${{ github.base_ref }}
- name: Check for new migration files
run: |
echo "Checking for new migration files..."
# Set the directory for migrations
MIGRATION_DIR=src/main/resources/db/migration
# Get existing migration files from the base branch
BASE_MIGRATION_FILES=$(git ls-tree -r --name-only origin/${{ github.base_ref }} -- $MIGRATION_DIR)
# Extract all timestamps from base migration files
BASE_TIMESTAMPS=$(echo "$BASE_MIGRATION_FILES" | awk -F'_' '{print $1}' | sort -n | uniq)
# List current migration files
CURRENT_MIGRATION_FILES=$(ls $MIGRATION_DIR)
echo "Current migration files: $CURRENT_MIGRATION_FILES"
# Get the new migration files added in the current branch
NEW_MIGRATION_FILES=$(git diff --name-only origin/${{ github.base_ref }} -- $MIGRATION_DIR)
# Output existing files in the base branch
echo "Existing migration files in the base branch:"
echo "$BASE_MIGRATION_FILES"
# Output new migration files found in the current branch
if [[ -z "$NEW_MIGRATION_FILES" ]]; then
echo "No new migration files found."
exit 0
else
echo "New migration files in the current branch:"
echo "$NEW_MIGRATION_FILES"
fi
# Check for new migration files and their timestamps
for FILE in $NEW_MIGRATION_FILES; do
TIMESTAMP=$(echo $FILE | awk -F'_' '{print $1}')
# Ensure the timestamp is valid
if [[ -z "$TIMESTAMP" ]]; then
echo "Error: File $FILE does not have a valid timestamp."
exit 1
fi
# Check if the timestamp is earlier than any base timestamps
for BASE_TIMESTAMP in $BASE_TIMESTAMPS; do
if [[ "$TIMESTAMP" < "$BASE_TIMESTAMP" ]]; then
echo "Error: Timestamp $TIMESTAMP in $FILE is earlier than base migration timestamp $BASE_TIMESTAMP."
exit 1