Skip to content

Commit b09d4e3

Browse files
committed
make+scripts+GitHub: prevent missing migration files
This commit adds a check script to our CI that makes sure there are no "holes" in the numbering of our migration version files. The script would also detect duplicate numbers in the migration prefixes. This is to prevent mistakes from cherry-picking commits onto branches that contain migration files.
1 parent 4ec0dd4 commit b09d4e3

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

.github/workflows/main.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ jobs:
8181
- name: Run test vector creation check
8282
run: make test-vector-check
8383

84-
migration-version-check:
84+
migration-check:
8585
name: migration version check
8686
runs-on: ubuntu-latest
8787
steps:
8888
- name: git checkout
8989
uses: actions/checkout@v3
9090

91-
- name: Run migration version check
92-
run: make migration-version-check
91+
- name: Run migration check
92+
run: make migration-check
9393

9494
########################
9595
# Compilation check.

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,10 @@ test-vector-check: gen-deterministic-test-vectors
331331
@$(call print, "Checking deterministic test vectors.")
332332
if test -n "$$(git status | grep -e ".json")"; then echo "Test vectors not updated"; git status; git diff; exit 1; fi
333333

334-
migration-version-check:
334+
migration-check:
335+
@$(call print, "Checking migration numbering.")
336+
./scripts/check-migration-numbering.sh
337+
335338
@$(call print, "Checking migration version.")
336339
./scripts/check-migration-latest-version.sh
337340

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
# The directory containing migration files.
4+
migrations_path="tapdb/sqlc/migrations"
5+
6+
# Check if the directory exists
7+
if [ ! -d "$migrations_path" ]; then
8+
echo "Directory $migrations_path does not exist."
9+
exit 1
10+
fi
11+
12+
# Get all unique prefixes (e.g., 000001, always 6 digits) from .up.sql files.
13+
prefixes=($(ls "$migrations_path"/*.up.sql 2>/dev/null | \
14+
sed -E 's/.*\/([0-9]{6})_.*\.up\.sql/\1/' | sort))
15+
16+
# Check if no prefixes were found.
17+
if [ ${#prefixes[@]} -eq 0 ]; then
18+
echo "No .up.sql migration files found in $migrations_path."
19+
exit 1
20+
fi
21+
22+
# Iterate over prefixes to ensure that there are no gaps and that each prefix
23+
# has a corresponding .down.sql file. Because the prefixes are sorted, and the
24+
# index starts at 0, we expect each prefix to be the index plus one.
25+
for i in "${!prefixes[@]}"; do
26+
expected_prefix=$(printf "%06d" $((i+1)))
27+
28+
if [ "${prefixes[$i]}" != "$expected_prefix" ]; then
29+
echo "Error: Missing migration with prefix $expected_prefix."
30+
exit 1
31+
fi
32+
33+
# Find the corresponding .down.sql file.
34+
base_filename=$(ls "$migrations_path/${prefixes[$i]}"_*.up.sql | \
35+
sed -E 's/\.up\.sql//')
36+
37+
if [ ! -f "$base_filename.down.sql" ]; then
38+
echo "Error: Missing .down.sql file for migration $expected_prefix."
39+
exit 1
40+
fi
41+
done
42+
43+
echo "All migration files are present and properly numbered."

0 commit comments

Comments
 (0)