-
Notifications
You must be signed in to change notification settings - Fork 0
/
koha-upgrade-from-v22.05
executable file
·248 lines (207 loc) · 11.8 KB
/
koha-upgrade-from-v22.05
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
# ------------------------------------------------------------------
# Author: Lennon Mazonde
# GitHub: @grandmaestr
# Title: koha-erm-upgrade-fix
# Description:
# This script attempts to upgrade your Koha installation from version 22.05 to the current version while automatically resolving some of the errors that may come up during the process, specifically the following:
##################
# Upgrade to 22.06.00.084 [08:52:30]: Bug 31162 - Add primary key to erm_eholdings_packages_agreements
# ERROR - {UNKNOWN}: DBI Exception: DBD::mysql::db do failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CONSTRAINT erm_eholdings_packages_agreements_uniq,
# ADD PRIMARY K' at line 3 at /usr/share/koha/lib/C4/Installer.pm line 739
# and
##################
# Upgrading database schema for kohadev
# Upgrade to 22.06.00.048 [02:11:58]: Bug 30472 - borrower_relationships.guarantor_id NOT NULL
# ERROR - {UNKNOWN}: DBI Exception: DBD::mysql::db do failed: Cannot change column 'guarantor_id': used in a foreign key constraint 'r_guarantor' at /usr/share/koha/lib/C4/Installer.pm line 739
#################
# This script attempts to fix these errors by dropping and recreating the erm_eholdings_packages_agreements table, and by dropping and recreating the foreign key constraint table. Dropping and recreating the erm_eholdings_packages_agreements will likely result in data loss since the table is destroyed in the process. Please make sure you understand what you are doing and make a backup of your database and have a recovery procedure in place before running this script.
# See:
# https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=31673
# https://www.mail-archive.com/koha@lists.katipo.co.nz/msg29255.html
# https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=31086
# https://github.com/Koha-Community/Koha/blob/master/installer/data/mysql/kohastructure.sql
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------
# Script version
VERSION=0.1.0
# Set the name of the script to variable
SCRIPT_NAME="$(basename ${0})"
SUBJECT=koha-upgrade-from-v22.05-with-fixes
# ------Help--------------------------------------------------------
Help(){
# Display Help
cat <<EOF
Usage: $SCRIPT_NAME -ihv args
Options:
i - instanceid. The name of the koha instance. One instance id per option.
h - help. Print this help.
v - version. Print the script version.
Example:
To run the script on a single instance called "library", run:
$SCRIPT_NAME -i library
For multiple instances, run
$SCRIPT_NAME -i library1 -i library2 ...
EOF
}
# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
Help
exit 1;
fi
while getopts ":i:vh" optname; do
case "$optname" in
v)
echo "Version $VERSION"
exit 0;
;;
i)
instanceid+=("$OPTARG")
;;
h)
Help
exit 0;
;;
\?)
echo "Unknown option $OPTARG"
Help
exit 0;
;;
:)
echo "Error: you must provide at least one instance name for option -$OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
Help
exit 0;
;;
esac
done
shift $(($OPTIND - 1))
param1=$1
param2=$2
# --- Backup Original Config -------------------------------------
Backup(){
# Backup database
while true; do
read -p "Do you want to backup all your Koha instances? This step is strongly recommended." response
case $response in
[yY][eE][sS]|[yY] )
printf "Running backup. Please be patient. \n";
# Backup databases
sudo koha-run-backups 2>/dev/null;
printf "Backup complete \n";
break;;
[nN][oO]|[nN] )
printf "Skipping backup. \n";
break;;
[qQ][uU][iI][tT] | [qQ] )
exit;;
* ) echo Invalid response.Try again;;
esac
done
}
# --- Drop the ERM table -------------------------------------
DropErm(){
sudo koha-mysql $val -Nse "DROP TABLE IF EXISTS erm_eholdings_packages_agreements;"
sudo koha-mysql $val -Nse "CREATE TABLE erm_eholdings_packages_agreements ( package_id int(11) NOT NULL COMMENT 'link to the package', agreement_id int(11) NOT NULL COMMENT 'link to the agreement', PRIMARY KEY (package_id,agreement_id), KEY erm_eholdings_packages_agreements_ibfk_2 (agreement_id), CONSTRAINT erm_eholdings_packages_agreements_ibfk_1 FOREIGN KEY (package_id) REFERENCES erm_eholdings_packages (package_id) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT erm_eholdings_packages_agreements_ibfk_2 FOREIGN KEY (agreement_id) REFERENCES erm_agreements (agreement_id) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;"
printf "The drop and recreate operation for the erm_eholdings_packages_agreements table for $val is complete. \n"
sleep 1s
}
# --- Drop and restore the foreign key -------------------------------------
DropForeignKey(){
while true; do
read -p "Drop and recreate the foreign key 'r_guarantor' for $val?: " response
case $response in
[yY][eE][sS]|[yY] )
# Drop the foreign key
echo print
sudo koha-mysql $val -Nse "ALTER TABLE borrower_relationships DROP FOREIGN KEY r_guarantor;ALTER TABLE borrower_relationships MODIFY COLUMN guarantor_id INT(11) COLLATE utf8mb4_unicode_ci NOT NULL;"
# Upgrade the database schema
sudo koha-upgrade-schema $val
# Add the foreign key
sudo koha-mysql $val -Nse "ALTER TABLE borrower_relationships ADD CONSTRAINT r_guarantor FOREIGN KEY (guarantor_id) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE;"
printf "The foreign key drop and recreate operation for $val is complete. \n If the koha-upgrade-schema commmand above produces errors, you will need to resolve those errors before you can successfully complete the upgrade process. \n"
sleep 1s
break;;
[nN][oO]|[nN] ) echo No changes have been made;
break;;
[qQ][uU][iI][tT] | [qQ] )
exit;;
* ) echo Invalid response.Try again;;
esac
done
}
# --- Restore Original Config -------------------------------------
Restore(){
# Stop and remove Koha instance
sudo koha-worker --stop $val
sudo koha-plack --stop $val
sudo koha-zebra --stop $val
sudo koha-remove $val
sudo deluser ${val}-koha
sudo rm /etc/apache2/sites-enabled/$val.conf /etc/apache2/sites-enabled/$val-le-ssl.conf
sudo rm -rf /etc/koha/sites/$val
sudo rm -rf /var/lib/koha/$val
sudo systemctl restart apache2.service $val
# Set path to backup files
sqldump=$(sudo ls /var/spool/koha/$val/*.sql.gz -t | head -n1)
configdump=$(sudo ls /var/spool/koha/$val/*.tar.gz -t | head -n1)
sudo koha-restore $sqldump $configdump
sudo koha-worker --start $val
sudo koha-plack --enable $val
sudo koha-plack --start $val
sudo koha-zebra --start $val
}
# --- Locks -------------------------------------------------------
LOCK_FILE=/tmp/$SUBJECT.lock
if [ -f "$LOCK_FILE" ]; then
echo "Script is already running"
exit
fi
trap "rm -f $LOCK_FILE" EXIT
touch $LOCK_FILE
# --- Body --------------------------------------------------------
set -x
# Set a time variable to be used in naming of backup and tmp files created during the process.
time=$(date +"%d_%m_%Y_%T")
# Backup databases
Backup
# Enable and start Koha instances
for val in "${instanceid[@]}"; do
# Upgrade the database schema of your Koha instances.
# If the schema upgrade fails with erm table and foreign key check errors, run fixes:
if sudo koha-upgrade-schema $val | grep -E 'ERROR(.*)CONSTRAINT erm_eholdings_packages_agreements_uniq(.*)|ERROR(.*)Cannot change column(.*)guarantor_id(.*)r_guarantor(.*)';
then
if sudo koha-upgrade-schema $val | grep -E "ERROR(.*)CONSTRAINT erm_eholdings_packages_agreements_uniq(.*)";
then
printf "The koha-schema-upgrade process produced an error message related to the erm_eholdings_packages_agreements. \nThe suggested fix is to drop and recreate the table. This will destroy any data that is on the table."
while true; do
read -p "Drop and recreate the erm_eholdings_packages_agreements table for $val?: " response
case $response in
[yY][eE][sS]|[yY] )
# Drop and recreate the table
DropErm 2>/tmp/$SUBJECT.$time.error.log
if cat /tmp/$SUBJECT.$time.error.log | grep "ERROR - {UNKNOWN}: DBI Exception: DBD::mysql::db do failed: Cannot change column 'guarantor_id': used in a foreign key constraint 'r_guarantor'";
then
printf "The koha-schema-upgrade process produced an error message related to the borrower_relationships.guarantor_id table. \n The suggested fix is to drop the r_guarantor foreign key, run the schema upgrade, and then recreate the foreign key."
DropForeignKey
fi
break;;
[nN][oO]|[nN] ) echo No changes have been made;
break;;
[qQ][uU][iI][tT] | [qQ] )
exit;;
* ) echo Invalid response.Try again;;
esac
done
fi
# Fix foreign key error
if sudo koha-upgrade-schema $val | grep -E "ERROR(.*)Cannot change column 'guarantor_id': used in a foreign key constraint 'r_guarantor'(.*)";
then
printf "The koha-schema-upgrade process produced an error message related to the borrower_relationships.guarantor_id table. \n The suggested fix is to drop the r_guarantor foreign key, run the schema upgrade, and then recreate the foreign key."
DropForeignKey
fi
fi
done