latest: Remove redundant header
[ninja.git] / install_scripts / ninja_db_init.sh
blob7f2e2c963ccd17245ded4a3af9be2e3adf1307c0
1 #!/bin/bash
3 # setup the db tables required for Ninja
5 target_db_version=14
6 target_sched_version=10
8 db_user=merlin
9 db_pass=merlin
10 db_name=merlin
12 progname="$0"
14 show_usage()
16 cat << END_OF_HELP
18 usage: $progname [options]
20 Where options can be any combination of:
21 --help|-h Print this cruft and exit
22 --db-user=<username> User merlin should use with db
23 --db-pass=<password> Password for the db user
24 --db-name=<name> Database name
26 END_OF_HELP
27 exit 1
30 get_arg ()
32 expr "z$1" : 'z[^=]*=\(.*\)'
35 while test "$1"; do
36 case "$1" in
37 --db-user=*)
38 db_user=$(get_arg "$1")
40 --db-user)
41 shift
42 db_user="$1"
44 --db-pass=*)
45 db_pass=$(get_arg "$1")
47 --db-pass)
48 shift
49 db_pass="$1"
51 --db-name=*)
52 db_name=$(get_arg "$1")
54 --db-name)
55 shift
56 db_name="$1"
58 --help|-h)
59 show_usage
62 echo "Illegal argument. I have no idea what to make of '$1'"
63 exit 1
65 esac
66 shift
67 done
70 prefix=$(dirname $0)"/.."
72 run_sql_file () # (db_login_opts, sql_script_path)
74 db_login_opts=$1
75 sql_script_path=$2
77 mysql $db_login_opts < $sql_script_path >/dev/null 2>/dev/null
80 if [ "$db_pass" != "" ]
81 then
82 db_login_opts="-u$db_user -p$db_pass $db_name"
83 else
84 db_login_opts="-u$db_user $db_name"
87 db_ver=$(mysql $db_login_opts -Be "SELECT version FROM ninja_db_version" 2>/dev/null | sed -n \$p)
89 if [ "$db_ver" = '' ]
90 then
91 # nothing found, insert ninja.sql
92 echo "Installing database tables for Ninja GUI"
93 run_sql_file "$db_login_opts" "$prefix/sql/mysql/ninja.sql"
94 db_ver=$(mysql $db_login_opts -Be "SELECT version FROM ninja_db_version" 2>/dev/null | sed -n \$p)
97 if [[ "$db_ver" = '' ]]
98 then
99 echo "Cannot connect to mysql, cannot install/upgrade database."
100 exit 1
103 # deprecated and should be deleted if they exist
104 sla_ver=$(mysql $db_login_opts -Be "SELECT version FROM sla_db_version" 2>/dev/null | sed -n \$p)
105 avail_ver=$(mysql $db_login_opts -Be "SELECT version FROM avail_db_version" 2>/dev/null | sed -n \$p)
107 function all_versions()
109 db_ver="$1"
110 new_ver=`expr $db_ver + 1`
111 echo "Upgrading ninja db from v${db_ver} to v${new_ver}"
112 run_sql_file "$db_login_opts" "$prefix/sql/mysql/ninja_db_v${db_ver}_to_v${new_ver}.sql"
113 mysql $db_login_opts -Be "UPDATE ninja_db_version SET version=$new_ver" 2>/dev/null
114 db_ver=$new_ver
117 while [ "$db_ver" -lt "$target_db_version" ]; do
118 case "$db_ver" in
120 # add table for recurring_downtime
121 echo "Installing database table for Recurring Downtime"
122 run_sql_file "$db_login_opts" "$prefix/sql/mysql/recurring_downtime.sql"
123 mysql $db_login_opts -Be "UPDATE ninja_db_version SET version=2" 2>/dev/null
124 db_ver=2
127 php index.php cli/upgrade_auth
128 all_versions 5
131 all_versions 12
132 php index.php cli/upgrade_recurring_downtime
135 all_versions 13
136 # we intend to drop all the horrible mess that is the old reports
137 # however, we need to upgrade them first, so the migration will work
138 if [ "$sla_ver" != "" ]; then
139 target_sla_version=10
140 while [ "$sla_ver" -lt "$target_sla_version" ]
142 case "$sla_ver" in
143 [5-7])
144 new_ver='8'
145 upgrade_script="$prefix/sql/mysql/sla_v5_to_v8.sql"
148 new_ver=`expr $sla_ver + 1 `
149 upgrade_script="$prefix/sql/mysql/sla_v${sla_ver}_to_v${new_ver}.sql"
151 esac
153 echo -n "Upgrading SLA tables from v${sla_ver} to v${new_ver} ... "
154 if [ -r "$upgrade_script" ]
155 then
156 run_sql_file "$db_login_opts" $upgrade_script
157 mysql $db_login_opts -Be "UPDATE sla_db_version SET version = '$new_ver'" 2>/dev/null
158 echo "done."
159 else
160 echo "SCRIPT MISSING."
161 echo "Tried to use $upgrade_script"
164 sla_ver=$new_ver
165 done
167 if [ "$avail_ver" != "" ]; then
168 target_avail_version=15
169 while [ "$avail_ver" -lt $target_avail_version ]
171 case "$avail_ver" in
172 [2-4])
173 new_ver=5
174 upgrade_script="$prefix/sql/mysql/avail_v2_to_v5.sql"
176 [6-7])
177 new_ver=8
178 upgrade_script="$prefix/sql/mysql/avail_v6_to_v8.sql"
181 php $prefix/index.php cli/upgrade_excluded
182 new_ver=10
183 upgrade_script="$prefix/sql/mysql/avail_v${avail_ver}_to_v${new_ver}.sql"
186 new_ver=`expr $avail_ver + 1 `
187 upgrade_script="$prefix/sql/mysql/avail_v${avail_ver}_to_v${new_ver}.sql"
190 esac
192 echo -n "Upgrading AVAIL tables from v${avail_ver} to v${new_ver} ... "
193 if [ -r "$upgrade_script" ]
194 then
195 run_sql_file "$db_login_opts" $upgrade_script
196 mysql $db_login_opts -Be "UPDATE avail_db_version SET version = '$new_ver'" 2>/dev/null
197 echo "done."
198 else
199 echo "SCRIPT MISSING."
200 echo "Tried to use $upgrade_script"
203 avail_ver=$new_ver
204 done
206 php index.php db_migrations/v13_to_v14
210 all_versions "$db_ver"
212 esac
213 done
215 # check that we have the scheduled reports tables in merlin
216 sched_db_ver=$(mysql $db_login_opts -Be "SELECT version FROM scheduled_reports_db_version" 2>/dev/null | sed -n \$p)
218 if [ "$sched_db_ver" = "" ]
219 then
220 echo "Installing database tables for scheduled reports configuration"
221 upgrade_script="$prefix/sql/mysql/scheduled_reports.sql"
222 run_sql_file "$db_login_opts" $upgrade_script
223 sched_db_ver=$(mysql $db_login_opts -Be "SELECT version FROM scheduled_reports_db_version" 2>/dev/null | sed -n \$p)
226 sched_db_ver=$(echo $sched_db_ver | cut -d '.' -f1)
227 while [ "$sched_db_ver" -lt "$target_sched_version" ]; do
228 case "$sched_db_ver" in
229 [1-5])
230 sched_db_ver=5
231 new_ver=6
232 upgrade_script="$prefix/sql/mysql/scheduled_reports_v${sched_db_ver}_to_v${new_ver}.sql"
235 new_ver=`expr $sched_db_ver + 1`
236 upgrade_script="$prefix/sql/mysql/scheduled_reports_v${sched_db_ver}_to_v${new_ver}.sql"
238 esac
240 echo -n "Upgrading scheduled reports tables from v${sched_db_ver} to v${new_ver}.sql ... "
241 if [ -r "$upgrade_script" ]
242 then
243 run_sql_file "$db_login_opts" $upgrade_script
244 mysql $db_login_opts -Be "UPDATE scheduled_reports_db_version SET version = '${new_ver}'" 2>/dev/null
245 echo "done."
246 else
247 echo "SCRIPT MISSING."
248 echo "Tried to use $upgrade_script"
251 sched_db_ver=$new_ver
252 done;
254 echo "Database upgrade complete."