|
1 | | -#!/bin/sh |
| 1 | +#!/bin/bash |
2 | 2 | # @configure_input@ |
3 | 3 |
|
4 | 4 | # This script allows one to perform DOMjudge database setup actions. |
@@ -52,30 +52,48 @@ not have to pass any of the options above. |
52 | 52 | EOF |
53 | 53 | } |
54 | 54 |
|
| 55 | +urlencode() |
| 56 | +{ |
| 57 | + # Redirect via stdin to prevent ugly escaping hacks. |
| 58 | + echo -n "$1" | php -r "echo rawurlencode(file_get_contents('php://stdin'));" |
| 59 | +} |
| 60 | + |
| 61 | +# This is global variable to be able to return the output from |
| 62 | +# mysql_options() below as an array, which is not possible otherwise. |
| 63 | +declare -a _mysql_options |
| 64 | + |
55 | 65 | mysql_options() |
56 | 66 | { |
| 67 | + local user pass |
| 68 | + _mysql_options=() |
| 69 | + |
57 | 70 | # shellcheck disable=SC2153 |
58 | 71 | if [ -n "$DBUSER" ]; then |
59 | | - _user="-u $DBUSER" |
60 | | - else |
61 | | - _user="${DBA_USER:+-u ${DBA_USER}}" |
| 72 | + _mysql_options+=('-u' "$DBUSER") |
| 73 | + elif [ -n "$DBA_USER" ]; then |
| 74 | + _mysql_options+=('-u' "$DBA_USER") |
62 | 75 | fi |
63 | 76 | # shellcheck disable=SC2153 |
64 | 77 | if [ -n "$PASSWD" ]; then |
65 | | - _pass="-p$PASSWD" |
66 | | - else |
67 | | - [ -n "$PROMPT_PASSWD" ] && _pass="-p" |
68 | | - [ -n "$DBA_PASSWD" ] && _pass="-p$DBA_PASSWD" |
| 78 | + _mysql_options+=("-p$PASSWD") |
| 79 | + elif [ -n "$DBA_PASSWD" ]; then |
| 80 | + _mysql_options+=("-p$DBA_PASSWD") |
| 81 | + elif [ -n "$PROMPT_PASSWD" ]; then |
| 82 | + _mysql_options+=('-p') |
69 | 83 | fi |
70 | 84 |
|
71 | | - [ -z "$USE_SOCKET" ] && port="-P$DBPORT" |
72 | | - echo $_user ${_pass:+"$_pass"} -h "$DBHOST" ${port:+"$port"} |
| 85 | + _mysql_options+=('-h' "$DBHOST") |
| 86 | + |
| 87 | + if [ -z "$USE_SOCKET" ]; then |
| 88 | + _mysql_options+=("-P$DBPORT") |
| 89 | + fi |
73 | 90 | } |
74 | 91 |
|
75 | 92 | # Wrapper around mysql command to allow setting options, user, etc. |
76 | 93 | mysql() |
77 | 94 | { |
78 | | - command mysql $(mysql_options) --silent --skip-column-names "$@" |
| 95 | + mysql_options |
| 96 | + command mysql "${_mysql_options[@]}" --silent --skip-column-names "$@" |
79 | 97 | } |
80 | 98 |
|
81 | 99 | # Quick shell hack to get a key from an INI file. |
@@ -126,10 +144,13 @@ symfony_console() |
126 | 144 | fi |
127 | 145 |
|
128 | 146 | if [ -n "$DBA_USER" ]; then |
| 147 | + user=$(urlencode "${DBA_USER}") |
| 148 | + host=$(urlencode "${domjudge_DBHOST}") |
| 149 | + db=$(urlencode "${domjudge_DBNAME}") |
129 | 150 | if [ -n "$DBA_PASSWD" ]; then |
130 | | - DATABASE_URL=mysql://${DBA_USER}:${DBA_PASSWD}@${domjudge_DBHOST}:${domjudge_DBPORT}/${domjudge_DBNAME} |
| 151 | + DATABASE_URL="mysql://$user:$(urlencode "${DBA_PASSWD}")@$host:${domjudge_DBPORT}/$db" |
131 | 152 | else |
132 | | - DATABASE_URL=mysql://${DBA_USER}@${domjudge_DBHOST}:${domjudge_DBPORT}/${domjudge_DBNAME} |
| 153 | + DATABASE_URL="mysql://$user@$host:${domjudge_DBPORT}/$db" |
133 | 154 | fi |
134 | 155 | fi |
135 | 156 | fi |
@@ -356,7 +377,7 @@ upgrade) |
356 | 377 | # shellcheck disable=SC2016,SC2028 |
357 | 378 | echo 'INSERT INTO `doctrine_migration_versions` |
358 | 379 | (version, executed_at, execution_time) |
359 | | - SELECT concat("DoctrineMigrations\\\\Version", version), executed_at, 1 |
| 380 | + SELECT concat("DoctrineMigrations\\Version", version), executed_at, 1 |
360 | 381 | FROM migration_versions;' | mysql "$DBNAME" |
361 | 382 | echo "DROP TABLE \`migration_versions\`" | mysql "$DBNAME" |
362 | 383 | fi |
|
0 commit comments