Skip to content

Commit

Permalink
Enable auto db provision for both docker compose and k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
xcompass committed Jun 19, 2024
1 parent 7ea57f9 commit 882f4db
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
22 changes: 20 additions & 2 deletions batch/createdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function __construct($arg, $interactive) {

$this->batch = !$interactive || isset($arg["batch"]);
$this->replace = isset($arg["replace"]);
$this->no_replace = isset($arg["no-replace"]);
$this->replace_user = isset($arg["replace-user"]);
$this->quiet = isset($arg["quiet"]);
$this->verbose = isset($arg["verbose"]);
Expand Down Expand Up @@ -338,6 +339,17 @@ function database_exists() {
return $found;
}

/** @return bool */
function table_exists() {
$result = Dbl::qe($this->hcrp_dblink(), "DESCRIBE `ActionLog`");
$found = false;
while (($x = $result->fetch_row())) {
$found = $found || $x[0] === "logId";
}
$result->close();
return $found;
}

function read_dbuser() {
$defuser = $this->configopt["dbUser"] ?? substr($this->name, 0, 16);
while (true) {
Expand Down Expand Up @@ -433,7 +445,11 @@ function check_schema() {
if ($this->replace) {
return true;
} else if ($this->batch) {
throw new CommandLineException("Not populating existing database in batch mode");
if (!$this->table_exists()) {
return true;
} else {
throw new CommandLineException("Not populating existing database in batch mode");
}
} else {
return $this->confirm("Replace contents of existing database? [Y/n] ");
}
Expand Down Expand Up @@ -586,7 +602,7 @@ function run() {
$this->had_dbuser = $this->grant && $this->dbuser_exists();
$this->check_dbpass();
}
if ($this->had_db) {
if ($this->had_db && !$this->no_replace) {
$this->check_replace();
}

Expand Down Expand Up @@ -631,7 +647,9 @@ static function make_args($argv, $interactive) {
"minimal Output minimal configuration file",
"batch Batch installation: never stop for input",
"replace Replace existing HotCRP database if present",
"no-replace Do not replace database",
"replace-user Replace existing HotCRP database user if present",
"no-config Do not replace database",
"no-grant Do not create user or grant privileges for HotCRP database access",
"dbuser: =USER,PASS Specify database USER and PASS for HotCRP database access",
"host: =HOST Specify database host [localhost]",
Expand Down
9 changes: 3 additions & 6 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3'

services:
smtp:
image: mailhog/mailhog:v1.0.1
Expand Down Expand Up @@ -64,10 +62,9 @@ services:
expose:
- 3306
environment:
# HOTCRP user and db are created with batch/createdb.php script
# MYSQL_USER: hotcrp
# MYSQL_PASSWORD: hotcrppwd
# MYSQL_DATABASE: hotcrp
MYSQL_USER: hotcrp
MYSQL_PASSWORD: hotcrppwd
MYSQL_DATABASE: hotcrp
MYSQL_ROOT_PASSWORD: root

keycloak:
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ wait_for_mysql

# provision db, db user and schema
if [ -n "$MYSQL_ROOT_PASSWORD" ]; then
if php batch/createdb.php -u root --password=$MYSQL_ROOT_PASSWORD -n $MYSQL_DATABASE --batch --dbuser $MYSQL_USER,$MYSQL_PASSWORD --host mysql --grant-host '%'; then
if php batch/createdb.php -u root --password=$MYSQL_ROOT_PASSWORD -n $MYSQL_DATABASE --batch --dbuser $MYSQL_USER,$MYSQL_PASSWORD --host $MYSQL_HOST --no-grant --no-replace --no-config; then
echo "Database has been initialized."
fi
fi
Expand Down

0 comments on commit 882f4db

Please sign in to comment.