1010#    upgrade-script-check.sh [OPTIONS]
1111# 
1212#  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
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, --comparison- branch BRANCH   Target branch for comparison (default: develop)
17+ #    -h, --help                       Show this help message
1818# 
1919#  EXIT CODES:
2020#    0: Success (no schema changes or upgrade script exists)
2828set  -euo pipefail
2929
3030MODE=" default" 
31- BRANCH =" develop" 
31+ COMPARISION_BRANCH =" develop" 
3232REPO_ROOT=" $( git rev-parse --show-toplevel) " 
3333
3434#  Parse command line arguments
@@ -43,8 +43,8 @@ parse_args() {
4343                fi 
4444                shift  2
4545                ;;
46-             -b|--branch)
47-                 BRANCH =" $2 " 
46+             -b|--comparison- branch)
47+                 COMPARISION_BRANCH =" $2 " 
4848                shift  2
4949                ;;
5050            -h|--help)
@@ -80,18 +80,53 @@ has_changes() {
8080    fi 
8181
8282    #  Fetch latest branch to ensure accurate comparison
83-     git fetch origin " $BRANCH "   > /dev/null 2>&1  ||  {
83+     git fetch origin " $COMPARISION_BRANCH "   > /dev/null 2>&1  ||  {
84+         echo  " Error: Failed to fetch origin/$COMPARISION_BRANCH "   >&2 
85+         exit  1
86+     }
87+ 
88+     #  Check if file has differences
89+     if  !  git diff --quiet " origin/$COMPARISION_BRANCH "   -- " $REPO_ROOT /$file "   2> /dev/null;  then 
90+         return  0
91+     else 
92+         return  1
93+     fi 
94+ }
95+ 
96+ has_changes_in_git () {
97+     local  file=" $1 " 
98+     if  !  check_file_exists " $file " ;  then 
99+         return  1
100+     fi 
101+ 
102+     #  Fetch latest branch to ensure accurate comparison
103+     git fetch origin " $COMPARISION_BRANCH "   > /dev/null 2>&1  ||  {
84104        if  [[ " $MODE "   ==  " verbose"   ]];  then 
85-             echo  " Error: Failed to fetch origin/$BRANCH  "   >&2 
105+             echo  " Error: Failed to fetch origin/$COMPARISION_BRANCH  "   >&2 
86106        fi 
87107        return  1
88108    }
89109
90-     #  Check if file has differences
91-     git diff --quiet " origin/$BRANCH "   -- " $REPO_ROOT /$file "   2> /dev/null
92-     return  $? 
110+     #  Check if file was modified in last commit, staged, or has unstaged changes
111+     local  file_path=" $REPO_ROOT /$file " 
112+ 
113+     #  Check if file was modified in last commit
114+     if  git diff --quiet HEAD~1 HEAD -- " $file_path "   2> /dev/null;  then 
115+         #  No changes in last commit, check staged changes
116+         if  git diff --quiet --cached -- " $file_path "   2> /dev/null;  then 
117+             #  No staged changes, check unstaged changes
118+             if  git diff --quiet -- " $file_path "   2> /dev/null;  then 
119+                 #  No changes found
120+                 return  1
121+             fi 
122+         fi 
123+     fi 
124+ 
125+     #  File has changes in one of the three states
126+     return  0
93127}
94128
129+ 
95130#  Main execution
96131main () {
97132    parse_args " $@ " 
@@ -120,6 +155,12 @@ main() {
120155
121156    #  If schema files changed, verify upgrade script exists
122157    if  [[ " $schema_changed "   ==  " true"   ]];  then 
158+ 
159+         if  ./buildkite/scripts/git/check-bypass.sh " !ci-bypass-upgrade-script-check" ;  then 
160+             echo  " ⏭️  Skipping upgrade script check as PR is bypassed" 
161+             exit  0
162+         fi 
163+ 
123164        #  Check that all required scripts exist
124165        for  script_path  in  " ${scripts[@]} " ;  do 
125166            if  !  check_file_exists " $script_path " ;  then 
@@ -130,6 +171,23 @@ main() {
130171            fi 
131172            exit  1
132173            fi 
174+ 
175+             #  Check if the upgrade script itself has changes
176+             if  has_changes_in_git " $script_path " ;  then 
177+                 if  [[ " $MODE "   ==  " verbose"   ]];  then 
178+                     echo  " ✓ Upgrade script has been modified: $script_path " 
179+                 fi 
180+             else 
181+                 if  [[ " $MODE "   ==  " verbose"   ]];  then 
182+                     echo  " Error: Schema changed but upgrade script not updated: $script_path " 
183+                     echo  " Please update the upgrade script to reflect schema changes." 
184+                     echo  " This is critical to ensure smooth database migrations in production." 
185+                     echo  " Upgrade/Rollback scripts must be updated together with schema changes, in the same commit." 
186+                     echo  " For local testing, scripts can be modified in staged/unstaged git states." 
187+                     exit  1
188+                 fi 
189+             fi 
190+ 
133191        done 
134192
135193        if  [[ " $MODE "   ==  " verbose"   ]];  then 
0 commit comments