Skip to content

Commit b8eb6ee

Browse files
committed
Refactor script to make it more comprehensive, move under tooling/
1 parent 74fb1a0 commit b8eb6ee

File tree

4 files changed

+58
-24
lines changed

4 files changed

+58
-24
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ repos:
2323
files: *files
2424
require_serial: true
2525
exclude: ^(tests/fixtures)
26-
- id: check-migration-duplicates
27-
name: Check migration duplicates
28-
language: script
29-
entry: ./scripts/check_migration_duplicates.sh
30-
files: ^sqlmesh/migrations/v\d{4}_.*\.py$
26+
- id: valid migrations
27+
name: valid migrations
28+
entry: tooling/validating_migration_numbers.sh
29+
language: system
3130
pass_filenames: false

scripts/check_migration_duplicates.sh

Lines changed: 0 additions & 18 deletions
This file was deleted.

tooling/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ The `vscode` directory contains sample configs for VSCode that can be copied int
88

99
```bash
1010
make vscode_settings
11-
```
11+
```
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)