Skip to content

Commit

Permalink
Added new workflow to check files in the migrations folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Gareth40342 committed Oct 13, 2024
1 parent 2a9a783 commit 0a9f0d7
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/check-database-migration-files.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Validate Migration Files

on:
pull_request:
branches:
- '**' # This triggers on PRs to any branch

jobs:
validate-migrations:
runs-on: ubuntu-latest
steps:
- name: Checkout current branch
uses: actions/checkout@v3
with:
fetch-depth: 0 # Fetch all branches to get the base branch

- name: Set up environment
run: |
# Create a directory for the migration files
mkdir -p current_migrations
mkdir -p base_migrations
- name: Fetch base branch
run: |
# Use the base branch name from the pr in github
BASE_BRANCH=${{ github.event.pull_request.base.ref }}
echo "Base branch is: $BASE_BRANCH"
# Fetch the base branch
git fetch origin $BASE_BRANCH:refs/remotes/origin/$BASE_BRANCH
git checkout $BASE_BRANCH
cp -R db/migration/* base_migrations/
- name: Check current migration timestamps
run: |
# Switch back to the current branch
git checkout ${{ github.head_ref }}
cp -R db/migration/* current_migrations/
# Validate timestamps
echo "Validating migration timestamps..."
# Extract and sort timestamps
current_timestamps=$(ls current_migrations | sed 's/^\([0-9]*\).*/\1/' | sort -n)
base_timestamps=$(ls base_migrations | sed 's/^\([0-9]*\).*/\1/' | sort -n)
# Check for duplicate timestamps first
if echo "$current_timestamps" | uniq -d | grep -q .; then
echo "Error: Duplicate timestamps found in the current branch migration files!"
exit 1
fi
# Check for timestamps in the current branch that are before the latest base branch timestamp
latest_base_timestamp=$(echo "$base_timestamps" | tail -n 1)
echo "Latest timestamp in base branch: $latest_base_timestamp"
for timestamp in $current_timestamps; do
if [ "$timestamp" -lt "$latest_base_timestamp" ]; then
echo "Error: Migration timestamp $timestamp is earlier than the latest base timestamp $latest_base_timestamp!"
exit 1
fi
done
echo "All migration timestamps are valid."

0 comments on commit 0a9f0d7

Please sign in to comment.