Skip to content

Commit 164ea2f

Browse files
authored
Merge pull request #17713 from dkijania/dkijania/archive_upgrade_script_test
[CI][HF] add check for change in archive between forks
2 parents 64824b4 + ea6ce70 commit 164ea2f

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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 "$@"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
let SelectFiles = ../../Lib/SelectFiles.dhall
2+
3+
let Pipeline = ../../Pipeline/Dsl.dhall
4+
5+
let PipelineTag = ../../Pipeline/Tag.dhall
6+
7+
let JobSpec = ../../Pipeline/JobSpec.dhall
8+
9+
let Cmd = ../../Lib/Cmds.dhall
10+
11+
let Command = ../../Command/Base.dhall
12+
13+
let Docker = ../../Command/Docker/Type.dhall
14+
15+
let Size = ../../Command/Size.dhall
16+
17+
in Pipeline.build
18+
Pipeline.Config::{
19+
, spec = JobSpec::{
20+
, dirtyWhen =
21+
[ SelectFiles.exactly "src/app/archive/create_schema" "sql"
22+
, SelectFiles.exactly "src/app/archive/drop_tables" "sql"
23+
]
24+
, path = "Lint"
25+
, name = "ArchiveUpgrade"
26+
, tags = [ PipelineTag.Type.Fast, PipelineTag.Type.Lint ]
27+
}
28+
, steps =
29+
[ Command.build
30+
Command.Config::{
31+
, commands =
32+
[ Cmd.run
33+
"buildkite/scripts/archive/upgrade-script-check.sh --mode assert --branch develop"
34+
]
35+
, label = "Archive: Check upgrade script need"
36+
, key = "archive-check-upgrade-script"
37+
, target = Size.Multi
38+
, docker = None Docker.Type
39+
}
40+
]
41+
}

0 commit comments

Comments
 (0)