1+ #! /bin/bash
2+
3+ # Archive Schema Upgrade Check Script
4+ #
5+ # This script verifies that when archive schema files are modified, the corresponding
6+ # upgrade script exists. This ensures database schema changes are properly handled
7+ # in production deployments.
8+ #
9+ # USAGE:
10+ # upgrade-script-check.sh [OPTIONS]
11+ #
12+ # OPTIONS:
13+ # -m, --mode MODE Execution mode: 'default' or 'verbose' (default: default)
14+ # default: Returns exit code 0/1 without error messages
15+ # verbose: Prints error messages and fails with descriptive output
16+ # -b, --branch BRANCH Target branch for comparison (default: develop)
17+ # -h, --help Show this help message
18+ #
19+ # EXIT CODES:
20+ # 0: Success (no schema changes or upgrade script exists)
21+ # 1: Failure (schema changes detected but upgrade script missing)
22+ #
23+ # FILES MONITORED:
24+ # - src/app/archive/create_schema.sql
25+ # - src/app/archive/drop_table.sql
26+ # - src/app/archive/upgrade_to_mesa.sql (required when above files change)
27+
28+ set -euo pipefail
29+
30+ MODE=" default"
31+ BRANCH=" develop"
32+ REPO_ROOT=" $( git rev-parse --show-toplevel) "
33+
34+ # Parse command line arguments
35+ parse_args () {
36+ while [[ $# -gt 0 ]]; do
37+ case $1 in
38+ -m|--mode)
39+ MODE=" $2 "
40+ if [[ " $MODE " != " default" && " $MODE " != " verbose" ]]; then
41+ echo " Error: Mode must be 'default' or 'verbose'" >&2
42+ exit 1
43+ fi
44+ shift 2
45+ ;;
46+ -b|--branch)
47+ BRANCH=" $2 "
48+ shift 2
49+ ;;
50+ -h|--help)
51+ grep ' ^#' " $0 " | sed ' s/^# //' | sed ' s/^#//'
52+ exit 0
53+ ;;
54+ * )
55+ echo " Error: Unknown option $1 " >&2
56+ echo " Use --help for usage information" >&2
57+ exit 1
58+ ;;
59+ esac
60+ done
61+ }
62+
63+ # Check if required files exist in the repository
64+ check_file_exists () {
65+ local file=" $1 "
66+ if [[ ! -f " $REPO_ROOT /$file " ]]; then
67+ if [[ " $MODE " == " assert" ]]; then
68+ echo " Error: Required file does not exist: $file " >&2
69+ fi
70+ return 1
71+ fi
72+ return 0
73+ }
74+
75+ # Check if files have differences against specified branch
76+ has_changes () {
77+ local file=" $1 "
78+ if ! check_file_exists " $file " ; then
79+ return 1
80+ fi
81+
82+ # Fetch latest branch to ensure accurate comparison
83+ git fetch origin " $BRANCH " > /dev/null 2>&1 || {
84+ if [[ " $MODE " == " verbose" ]]; then
85+ echo " Error: Failed to fetch origin/$BRANCH " >&2
86+ fi
87+ return 1
88+ }
89+
90+ # Check if file has differences
91+ git diff --quiet " origin/$BRANCH " -- " $REPO_ROOT /$file " 2> /dev/null
92+ return $?
93+ }
94+
95+ # Main execution
96+ main () {
97+ parse_args " $@ "
98+
99+ local monitored_scripts=(
100+ " src/app/archive/create_schema.sql"
101+ " src/app/archive/drop_table.sql"
102+ )
103+ local upgrade_script=" src/app/archive/upgrade_to_mesa.sql"
104+
105+ # Check if either monitored file has changes
106+ local schema_changed=false
107+
108+ for script in " ${monitored_scripts[@]} " ; do
109+ if has_changes " $script " ; then
110+ schema_changed=true
111+ if [[ " $MODE " == " verbose" ]]; then
112+ echo " Detected changes in: $script "
113+ fi
114+ fi
115+ done
116+
117+ # If schema files changed, verify upgrade script exists
118+ if [[ " $schema_changed " == " true" ]]; then
119+ if ! check_file_exists " $upgrade_script " ; then
120+ if [[ " $MODE " == " verbose" ]]; then
121+ echo " Error: Archive schema files have been modified but upgrade script is missing!"
122+ echo " Please create: $upgrade_script "
123+ echo " This script should contain the necessary database migration steps."
124+ fi
125+ exit 1
126+ fi
127+
128+ if [[ " $MODE " == " verbose" ]]; then
129+ echo " ✓ Archive schema changes detected and upgrade script exists"
130+ fi
131+ else
132+ if [[ " $MODE " == " verbose" ]]; then
133+ echo " ✓ No archive schema changes detected"
134+ fi
135+ fi
136+
137+ exit 0
138+ }
139+
140+ main " $@ "
0 commit comments