-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathvalidating_migration_numbers.sh
More file actions
executable file
·55 lines (43 loc) · 1.53 KB
/
validating_migration_numbers.sh
File metadata and controls
executable file
·55 lines (43 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
# Navigate to the migrations directory (modify the path if necessary)
cd "sqlmesh/migrations" || exit 1
# Collect all migration files matching the pattern (e.g., v0001_initial.py)
migration_files=(v*.py)
# Initialize an array to hold migration numbers
numbers=()
# Extract migration numbers from filenames
for file in "${migration_files[@]}"; do
if [[ $file =~ ^v0*([0-9]+)_ ]]; then
num=${BASH_REMATCH[1]}
if [[ "$num" -gt 0 ]]; then
numbers+=("$num")
fi
fi
done
# Check if any migration files were found
if [[ ${#numbers[@]} -eq 0 ]]; then
echo "No migration files found matching the pattern 'v<zero-padded number>_<description>.py'."
exit 1
fi
# Check for duplicate migration numbers
duplicates=$(printf "%s\n" "${numbers[@]}" | sort | uniq -d)
if [[ -n $duplicates ]]; then
echo "Error: Duplicate migration numbers found: $duplicates"
exit 1
fi
# Sort the migration numbers
sorted_numbers=($(printf "%s\n" "${numbers[@]}" | sort -n))
# Get the first and last migration numbers
first_number="${sorted_numbers[0]}"
last_index=$((${#sorted_numbers[@]} - 1))
last_number="${sorted_numbers[$last_index]}"
# Check for gaps in the migration sequence
expected_numbers=($(seq "$first_number" "$last_number"))
if [[ "${sorted_numbers[*]}" != "${expected_numbers[*]}" ]]; then
echo "Error: Missing migration numbers in sequence."
echo "Expected sequence: ${expected_numbers[*]}"
echo "Found sequence: ${sorted_numbers[*]}"
exit 1
fi
echo "All migration numbers are sequential and without overlaps."
exit 0