Merge remote-tracking branch 'origin/bug-1239' into release
[cacert-devel.git] / scripts / db_migrations / version4.sh
1 #!/bin/sh
2 # LibreSSL - CAcert web application
3 # Copyright (C) 2004-2011 CAcert Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18
19
20 # script to do database migrations
21
22 # This particular version migrates from the preversioned state to version 1
23 # If you want to reuse it for further migrations you probably should pay special
24 # attention because you have to adjust it a bit
25
26 set -e # script fails if any command fails
27
28 STDIN=0
29 STDOUT=1
30 STDERR=2
31
32 if [ "$1" = "--help" ]; then
33 cat >&$STDERR <<- USAGE
34 Usage: $0 [MYSQL_OPTIONS]
35 You have to specify all options needed by "mysql" as if you had started
36 the MySQL command line client directly (including the name of the
37 database to operate on). The MySQL user used has to have enough
38 privileges to do all necessary operations (among others CREATE, ALTER,
39 DROP, UPDATE, INSERT, DELETE).
40 You might need to enter the mysql password multiple times if you
41 specify the -p option.
42 USAGE
43 exit 1
44 fi
45
46 mysql_opt=" --batch --skip-column-names $@"
47
48 schema_version=$( mysql $mysql_opt <<- 'SQL'
49
50 SELECT MAX(`version`) FROM `schema_version`;
51 SQL
52 )
53 if [ $schema_version != 3 ]; then
54 cat >&$STDERR <<- ERROR
55 Error: database schema is not in the right version to do the migration!
56 Expected version: 3
57 ERROR
58 exit 2
59 fi
60
61 mysql $mysql_opt <<- 'SQL'
62
63 -- dump table AdminLog
64 SELECT *
65 INTO OUTFILE "adminlog_table_backup_1135"
66 FIELDS TERMINATED BY ','
67 OPTIONALLY ENCLOSED BY '"'
68 LINES TERMINATED BY "\n"
69 FROM `adminlog`;
70 SQL
71
72
73 echo "Dump table create in adminlog_table_backup_1135"
74
75
76 mysql $mysql_opt <<- 'SQL'
77 -- update table admin log
78
79 UPDATE `adminlog` SET `type` = 'old name or dob change',
80 `information` = 'see adminlog_table_backup_1135';
81
82 -- alter table admin log
83
84 ALTER TABLE `adminlog`
85 DROP `old-lname`,
86 DROP `old-dob`,
87 DROP `new-lname`,
88 DROP `new-dob`;
89
90
91 -- Update schema version number
92 INSERT INTO `schema_version`
93 (`version`, `when`) VALUES
94 ('4' , NOW() );
95 SQL
96
97
98 echo "Database successfully migrated to version 4"
99 exit 0
100