Added new workflow to check files in the migrations folder #14
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: 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 list of new migration files added in the current branch | |
NEW_MIGRATION_FILES=$(git diff --name-only origin/${{ github.base_ref }} -- $MIGRATION_DIR | grep .sql) | |
if [ -z "$NEW_MIGRATION_FILES" ]; then | |
echo "No new migration files to check." | |
exit 0 | |
fi | |
# 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 timestamps in new files before the latest in the base branch | |
for FILE in $NEW_MIGRATION_FILES; do | |
TIMESTAMP=$(echo $FILE | awk -F'_' '{print $1}') | |
# Ensure the timestamp is valid | |
if [[ -z "$TIMESTAMP" ]]; then | |
echo "File $FILE does not have a valid timestamp." | |
exit 1 | |
fi | |
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 new migration files are valid." |