-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch-cr
executable file
·148 lines (123 loc) · 3.4 KB
/
patch-cr
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
#
# Patches/applies one or more change requests (or any SQL file) to a database.
set -e
if [ -f .env ]; then
set -o allexport
source .env
set +o allexport
fi
. $(dirname "$0")/utility.sh
g_db=
g_host=$(dnsdomainname -A)
g_port=5433
g_user=
g_log_level=WARNING
if [ -f ~/vault/.default.sead.server ]; then
g_host=`cat ~/vault/.default.sead.server`
fi
if [ -f ~/vault/.default.sead.username ]; then
g_user=`cat ~/vault/.default.sead.username`
fi
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--database|-d)
g_db="$2"; shift 2;
;;
--port|-p)
g_port="$2"; shift 2;
;;
--user|-U)
g_user="$2"; shift 2;
;;
--host|-h)
g_host="$2"; shift 2;
;;
--log-level|-l)
g_log_level="$2"; shift 2;
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
g_crs=$@
function usage()
{
local error_message=$1
if [ "$error_message" != "" ]; then
echo "$error_message"
fi
echo "usage: patch-cr --database DBNAME [--host HOST] [--port PORT] [--user USER] [--log-level LEVEL] CHANGE_REQUEST_NAME..."
echo "Creates a new database using source as template."
echo ""
echo " --database target database name"
echo " --host host name"
echo " --user user name"
echo " --port host port"
echo " --log-level DEBUG | INFO | NOTICE | WARNING | ERROR"
echo ""
echo "Example:"
echo " bin/patch-cr --port 5433 --database sead_staging 20240924_DDL_MEASURED_VALUES_REFACTOR"
exit 64
}
if [[ ! "$g_log_level" =~ ^(DEBUG|INFO|NOTICE|WARNING|ERROR)$ ]]; then
usage "Invalid log level: $LOG_LEVEL"
fi
function execute_sql() {
local database="$1"
local sql="$2"
psql -bqw -h $g_host -p "$g_port" -U $g_user -c "$sql" $database > /dev/null
if [ $? -ne 0 ]; then
echo "fatal: sql failed!"
ecit 64
fi
}
function kickout() {
local database="$1"
local sql="select pg_terminate_backend(pid) from pg_stat_activity where datname = '$database' and pid <> pg_backend_pid();"
execute_sql "postgres" "$sql" &> /dev/null
}
if [ "$g_db" == "" ]; then
usage 'fatal: target database not specified!'
fi
if [ "$g_crs" == "" ]; then
usage 'fatal: you need to specify CRs!'
fi
for g_cr in $g_crs; do
if [ -f "$g_cr" ]; then
continue
fi
g_project=$(find_project "$g_cr")
if [ "$g_project" == "" ]; then
usage "fatal: patch ${g_cr} not found!"
fi
g_cr_file="$g_project/deploy/${g_cr}.sql"
if [ ! -f "$g_cr_file" ]; then
usage "fatal: cr file $g_cr_file not found!"
fi
done
kickout $g_db
for g_cr in $g_crs; do
if [ -f "$g_cr" ]; then
g_patch_file="$g_cr"
g_temp_file="/tmp/cr-patch-file.sql"
else
g_project=$(find_project "$g_cr")
g_patch_file="$g_project/deploy/$g_cr.sql"
g_temp_file="/tmp/$g_cr.sql"
fi
if grep -q "\/repo" "$g_patch_file"; then
sed 's/\/repo/./g' "$g_patch_file" > "$g_temp_file"
g_patch_file="$g_temp_file"
fi
psql -h $g_host -p $g_port -U $g_user -w --dbname=$g_db --file $g_patch_file -q -v ON_ERROR_STOP=1 -c "set client_min_messages to $g_log_level;"
if [ -f "$g_temp_file" ]; then
rm -f "$g_temp_file"
fi
done