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