|
| 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