Skip to content

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

Added new workflow to check files in the migrations folder

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

name: Database 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 Migration Issues
run: |
echo "Checking for migration issues..."
# Set the directory for migrations
MIGRATION_DIR=src/main/resources/db/migration
# Get latest timestamp from the base branch
BASE_MIGRATION_FILES=$(git ls-tree -r --name-only origin/${{ github.base_ref }} -- $MIGRATION_DIR)
LATEST_BASE_TIMESTAMP=$(echo "$BASE_MIGRATION_FILES" | awk -F'_' '{print $1}' | sort -n | tail -n 1)
echo "Latest timestamp in base branch: $LATEST_BASE_TIMESTAMP"
# List current migration files
CURRENT_MIGRATION_FILES=$(ls $MIGRATION_DIR)
echo "Current migration files: $CURRENT_MIGRATION_FILES"
# Check for duplicates
DUPLICATE_FILES=$(echo "$CURRENT_MIGRATION_FILES" | awk -F'_' '{print $1}' | sort | uniq -d)
if [ ! -z "$DUPLICATE_FILES" ]; then
echo "Duplicate timestamps found: $DUPLICATE_FILES"
exit 1
fi
# Check for timestamps before the latest in the base branch
for FILE in $CURRENT_MIGRATION_FILES; do
TIMESTAMP=$(echo $FILE | awk -F'_' '{print $1}')
if [[ "$TIMESTAMP" < "$LATEST_BASE_TIMESTAMP" ]]; then
echo "Timestamp $TIMESTAMP in $FILE is before the latest timestamp in the base branch."
exit 1
fi
done
echo "All migration files are valid."