Skip to content

Commit cbc4424

Browse files
committed
Add GitHub workflow to validate new migrations are ordered correctly
1 parent b9c50f7 commit cbc4424

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

.github/workflows/database.yaml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,88 @@ jobs:
6363
- name: Load fixtures
6464
run: bin/console doctrine:fixtures:load -vvv --ansi --no-interaction
6565

66+
migration-integrity:
67+
name: Migration integrity
68+
69+
# Verifies the integrity of migration files:
70+
# 1. Existing migrations from the base branch have not been modified or deleted
71+
# 2. New migrations have a higher version number than existing migrations
72+
73+
runs-on: ubuntu-latest
74+
75+
steps:
76+
- name: Checkout code
77+
uses: actions/checkout@v4
78+
with:
79+
fetch-depth: 0
80+
81+
- name: Determine base branch
82+
id: base
83+
run: |
84+
if [ -n "${{ github.base_ref }}" ]; then
85+
BASE_REF="${{ github.base_ref }}"
86+
else
87+
git fetch --all --quiet
88+
BEST_BRANCH="main"
89+
BEST_DISTANCE=$(git rev-list --count origin/main..HEAD 2>/dev/null || echo 999999)
90+
for branch in $(git branch -r | grep -oE 'origin/[0-9]+\.x' | sed 's|origin/||'); do
91+
DISTANCE=$(git rev-list --count origin/$branch..HEAD 2>/dev/null || echo 999999)
92+
if [ "$DISTANCE" -lt "$BEST_DISTANCE" ]; then
93+
BEST_DISTANCE=$DISTANCE
94+
BEST_BRANCH=$branch
95+
fi
96+
done
97+
BASE_REF=$BEST_BRANCH
98+
fi
99+
echo "base_ref=$BASE_REF" >> $GITHUB_OUTPUT
100+
git fetch origin $BASE_REF
101+
102+
- name: Verify existing migrations are not modified or deleted
103+
run: |
104+
BASE_REF="${{ steps.base.outputs.base_ref }}"
105+
CHANGED=$(git diff --name-only --diff-filter=MD origin/$BASE_REF...HEAD -- migrations/)
106+
if [ -n "$CHANGED" ]; then
107+
echo "Error: The following existing migrations were modified or deleted:"
108+
echo "$CHANGED"
109+
exit 1
110+
fi
111+
echo "No existing migrations were modified or deleted."
112+
113+
- name: Verify new migrations have higher version numbers than existing migrations
114+
run: |
115+
BASE_REF="${{ steps.base.outputs.base_ref }}"
116+
BASE_MAX=$(git ls-tree -r --name-only origin/$BASE_REF -- migrations/ | grep -oE '[0-9]{14}' | sort | tail -1)
117+
if [ -z "$BASE_MAX" ]; then
118+
echo "No existing migrations found in base branch, skipping version check."
119+
exit 0
120+
fi
121+
echo "Highest existing migration version: $BASE_MAX"
122+
123+
NEW_MIGRATIONS=$(git diff --name-only --diff-filter=A origin/$BASE_REF...HEAD -- migrations/)
124+
if [ -z "$NEW_MIGRATIONS" ]; then
125+
echo "No new migrations added."
126+
exit 0
127+
fi
128+
129+
FAILED=false
130+
for file in $NEW_MIGRATIONS; do
131+
VERSION=$(echo "$file" | grep -oE '[0-9]{14}')
132+
if [ -z "$VERSION" ]; then
133+
echo "Warning: Could not extract version number from $file"
134+
continue
135+
fi
136+
if [ "$VERSION" -le "$BASE_MAX" ]; then
137+
echo "Error: $file has version $VERSION which is not higher than the existing maximum $BASE_MAX"
138+
FAILED=true
139+
else
140+
echo "OK: $file (version $VERSION > $BASE_MAX)"
141+
fi
142+
done
143+
144+
if [ "$FAILED" = true ]; then
145+
exit 1
146+
fi
147+
66148
migrations:
67149
name: Migrations
68150

0 commit comments

Comments
 (0)