forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-migration-safety.sh
More file actions
executable file
·87 lines (71 loc) · 2.13 KB
/
Copy pathcheck-migration-safety.sh
File metadata and controls
executable file
·87 lines (71 loc) · 2.13 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env bash
set -euo pipefail
MIGRATION_DIR="Backend/src/database/migrations"
ROLLBACK_MODE=false
log() { echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] $*"; }
fail() { log "FAIL: $*"; exit_code=1; }
exit_code=0
while [[ $# -gt 0 ]]; do
case "$1" in
--files)
shift
MIGRATION_FILES=($@)
break
;;
--rollback)
ROLLBACK_MODE=true
shift
;;
*)
echo "Usage: $0 [--files <file1 file2 ...>] [--rollback]"
exit 1
;;
esac
done
if [[ "$ROLLBACK_MODE" == true ]]; then
log "Running rollback validation..."
if [[ -d "$MIGRATION_DIR" ]]; then
for f in "$MIGRATION_DIR"/*.ts; do
if grep -qE 'down\s*:\s*Promise\.resolve' "$f"; then
log "Rollback exists for $(basename "$f")"
elif grep -qE '##down' "$f" 2>/dev/null; then
log "Rollback exists for $(basename "$f")"
else
fail "Migration $(basename "$f") has no rollback implementation"
fi
done
fi
if [[ $exit_code -eq 0 ]]; then
log "Rollback validation passed."
fi
exit $exit_code
fi
log "Checking migration files for breaking changes..."
for file in "${MIGRATION_FILES[@]}"; do
log "Checking $file..."
if grep -qiE 'DROP\s+COLUMN' "$file"; then
fail "$file contains DROP COLUMN — breaking change"
fi
if grep -qiE 'ALTER\s+.*\s+TYPE\s+' "$file"; then
fail "$file contains ALTER COLUMN TYPE — potential breaking change"
fi
if grep -qiE 'ALTER\s+.*\s+SET\s+NOT\s+NULL' "$file" && ! grep -qiE 'DEFAULT' "$file"; then
fail "$file adds NOT NULL without DEFAULT — breaking change"
fi
if grep -qiE 'DROP\s+TABLE' "$file"; then
fail "$file contains DROP TABLE — breaking change"
fi
if grep -qiE 'RENAME\s+(COLUMN|TABLE)' "$file"; then
fail "$file contains RENAME — breaking change for running applications"
fi
if grep -qiE 'ALTER\s+COLUMN.*DROP\s+DEFAULT' "$file"; then
fail "$file drops column DEFAULT — may cause application errors"
fi
log "$file — no breaking changes detected"
done
if [[ $exit_code -eq 0 ]]; then
log "All migration safety checks passed."
else
log "Some migration safety checks failed."
fi
exit $exit_code