-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_migration_timestamp.sh
More file actions
executable file
·71 lines (60 loc) · 1.86 KB
/
check_migration_timestamp.sh
File metadata and controls
executable file
·71 lines (60 loc) · 1.86 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env bash
set -euo pipefail
MODEL_DIRS=""
MIGRATION_DIR=""
EXCLUDES=()
for arg in "$@"; do
case $arg in
--model-dirs=*) MODEL_DIRS="${arg#*=}" ;;
--migration-dir=*) MIGRATION_DIR="${arg#*=}" ;;
--exclude=*) IFS=',' read -ra EXC <<< "${arg#*=}"; EXCLUDES+=("${EXC[@]}") ;;
--exclude-dir=*) ;; # ignored for now, kept for compatibility
*) ;;
esac
done
if [[ -z "$MODEL_DIRS" || -z "$MIGRATION_DIR" ]]; then
echo "❌ Error: --model-dirs and --migration-dir must be set."
exit 1
fi
# Get staged files
STAGED_FILES=$(git diff --cached --name-only)
# Build exclude pattern
EXCLUDE_PATTERN=""
for pattern in "${EXCLUDES[@]}"; do
if [ -n "$EXCLUDE_PATTERN" ]; then
EXCLUDE_PATTERN="$EXCLUDE_PATTERN|$pattern"
else
EXCLUDE_PATTERN="$pattern"
fi
done
# Check for changed model files in staging area
MODEL_CHANGES=""
IFS=',' read -ra DIRS <<< "$MODEL_DIRS"
for dir in "${DIRS[@]}"; do
dir_changes=$(echo "$STAGED_FILES" | grep "^$dir/" || true)
if [ -n "$dir_changes" ]; then
if [ -n "$EXCLUDE_PATTERN" ]; then
dir_changes=$(echo "$dir_changes" | grep -v -E "$EXCLUDE_PATTERN" || true)
fi
if [ -n "$dir_changes" ]; then
MODEL_CHANGES="$dir_changes"
break
fi
fi
done
# If no model changes, we're good
if [ -z "$MODEL_CHANGES" ]; then
exit 0
fi
# Check for migration changes in staging area
MIGRATION_CHANGES=$(echo "$STAGED_FILES" | grep "^$MIGRATION_DIR/" | grep "\.py$" || true)
if [ -z "$MIGRATION_CHANGES" ]; then
echo "❌ Detected changes in $(echo "$MODEL_DIRS" | tr ',' ' + ') without a new migration in $MIGRATION_DIR"
echo ""
echo "Changed model files:"
echo "$MODEL_CHANGES"
echo ""
echo "→ Please create a migration using: alembic revision --autogenerate -m '...'"
exit 1
fi
exit 0