Skip to content

Commit e430f50

Browse files
authored
Chore: fail if >1 migration scripts start with the same version (#5038)
1 parent 00a0c50 commit e430f50

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ repos:
2323
files: *files
2424
require_serial: true
2525
exclude: ^(tests/fixtures)
26+
- id: valid migrations
27+
name: valid migrations
28+
entry: tooling/validating_migration_numbers.sh
29+
language: system
30+
pass_filenames: false
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
# Navigate to the migrations directory (modify the path if necessary)
4+
cd "sqlmesh/migrations" || exit 1
5+
6+
7+
# Collect all migration files matching the pattern (e.g., v0001_initial.py)
8+
migration_files=(v*.py)
9+
10+
# Initialize an array to hold migration numbers
11+
numbers=()
12+
13+
# Extract migration numbers from filenames
14+
for file in "${migration_files[@]}"; do
15+
if [[ $file =~ ^v0*([0-9]+)_ ]]; then
16+
num=${BASH_REMATCH[1]}
17+
numbers+=("$num")
18+
fi
19+
done
20+
21+
# Check if any migration files were found
22+
if [[ ${#numbers[@]} -eq 0 ]]; then
23+
echo "No migration files found matching the pattern 'v<zero-padded number>_<description>.py'."
24+
exit 1
25+
fi
26+
27+
# Check for duplicate migration numbers
28+
duplicates=$(printf "%s\n" "${numbers[@]}" | sort | uniq -d)
29+
if [[ -n $duplicates ]]; then
30+
echo "Error: Duplicate migration numbers found: $duplicates"
31+
exit 1
32+
fi
33+
34+
# Sort the migration numbers
35+
sorted_numbers=($(printf "%s\n" "${numbers[@]}" | sort -n))
36+
37+
# Get the first and last migration numbers
38+
first_number="${sorted_numbers[0]}"
39+
last_index=$((${#sorted_numbers[@]} - 1))
40+
last_number="${sorted_numbers[$last_index]}"
41+
42+
# Check for gaps in the migration sequence
43+
expected_numbers=($(seq "$first_number" "$last_number"))
44+
45+
if [[ "${sorted_numbers[*]}" != "${expected_numbers[*]}" ]]; then
46+
echo "Error: Missing migration numbers in sequence."
47+
echo "Expected sequence: ${expected_numbers[*]}"
48+
echo "Found sequence: ${sorted_numbers[*]}"
49+
exit 1
50+
fi
51+
52+
echo "All migration numbers are sequential and without overlaps."
53+
exit 0

0 commit comments

Comments
 (0)