-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-patch.sh
More file actions
executable file
·122 lines (76 loc) · 2.38 KB
/
create-patch.sh
File metadata and controls
executable file
·122 lines (76 loc) · 2.38 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
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
#!/bin/bash
# input patch name
# checks patch does not exist automatically adds newest number
# file name patch ${num}-${name}-patch.sql
# file name patch ${num}-${name}-rollback.sql
# find last content changed file (mtime) ls -1rt
declare -r PATCH_DIR_NAME='patches'
declare -r ROLLBACK_DIR_NAME='rollbacks'
function check() {
if [[ ! -d "${PATCH_DIR_NAME}" ]]; then
echo "directory ${PATCH_DIR_NAME} does not exist" || exit
fi
if [[ ! -d "${ROLLBACK_DIR_NAME}" ]]; then
echo "directory ${ROLLBACK_DIR_NAME} does not exist" || exit
fi
}
function make_patch_name() {
local -r old_name=${1}
read -p 'patch name:' name
## todo check not empty
if [[ -z "${name}" ]]; then
echo "Please provide a patch name"
exit 1
fi
num=${old_name/-*/}
## next only works with extended globbing on
shopt -s extglob;
num=${num##+(0)}
(( num += 1 ))
printf -v t "%05d" ${num}
echo "${t}-${name}"
}
function create_patch_file() {
local PATCH_NAME="${1}"
local latest_dep="${2}"
local output="${PATCH_DIR_NAME}/${PATCH_NAME}-patch.sql"
cat > "${output}" <<EOF
BEGIN;
SELECT _v.register_patch('${PATCH_NAME}',
array['${latest_dep}'] );
-- section of creation best as user role megdb_admin
SET ROLE megdb_admin;
-- for some test queries as user megxuser
-- SET ROLE megxuser;
rollback;
EOF
}
function create_rollback_file() {
local PATCH_NAME="${1}"
local output="${ROLLBACK_DIR_NAME}/${PATCH_NAME}-rollback.sql"
cat > ${output} <<EOF
BEGIN;
SELECT _v.unregister_patch( '${PATCH_NAME}');
ROLLBACK;
EOF
}
function main() {
svn update ${PATCH_DIR_NAME} || exit "Could not update patch dir from svn"
local patch_name
local latest_patch_file
## todo make patch name a cmd line input
check || exit "not all required stuff here"
## this ls is mision critical (use version sort with -v whch is numerical sort)
latest_patch_file=$(ls -1v ${PATCH_DIR_NAME}/*.sql | tail -n1)
latest_patch_name=$(basename "${latest_patch_file}" '.sql')
echo "Latest patch=${latest_patch_file}"
latest_patch_name=${latest_patch_name%-patch}
echo "Latest patch name=${latest_patch_name}"
patch_name=$(make_patch_name ${latest_patch_name})
create_rollback_file ${patch_name}
create_patch_file ${patch_name} ${latest_patch_name}
echo "Succesfuly created patch file: ${patch_name}.sql"
return 0
}
main "$@"
exit