From 6123222064a956f466284f8b6d56e1d074570491 Mon Sep 17 00:00:00 2001 From: Tom Levy Date: Tue, 2 Jan 2024 02:52:19 +1300 Subject: [PATCH 1/6] Remove unused file config/database.yml.default (#237) --- config/database.yml.default | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 config/database.yml.default diff --git a/config/database.yml.default b/config/database.yml.default deleted file mode 100644 index c3a2fa2c..00000000 --- a/config/database.yml.default +++ /dev/null @@ -1,27 +0,0 @@ -# SQLite version 3.x -# gem install sqlite3 -development: - adapter: postgresql - encoding: unicode - database: nztrain - pool: 5 - username: www-data - password: - -# Warning: The database defined as "test" will be erased and -# re-generated from your development database when you run "rake". -# Do not set this db to the same as development or production. -test: - adapter: postgresql - encoding: unicode - database: nztraintest - pool: 5 - username: postgres - -production: - adapter: postgresql - encoding: unicode - database: nztrain - pool: 5 - username: www-data - password: From 0f9c913a9c66d46d397a8f63dd42969f63d52e10 Mon Sep 17 00:00:00 2001 From: Tom Levy Date: Tue, 2 Jan 2024 02:52:35 +1300 Subject: [PATCH 2/6] Remove obsolete comment about SQLite from PostgreSQL config (#237) --- config/database.yml.template | 2 -- 1 file changed, 2 deletions(-) diff --git a/config/database.yml.template b/config/database.yml.template index 252b43f7..79d8d6e1 100644 --- a/config/database.yml.template +++ b/config/database.yml.template @@ -1,5 +1,3 @@ -# SQLite version 3.x -# gem install sqlite3 development: adapter: postgresql encoding: unicode From c30add928cd6d96d94599d8fa168306096799155 Mon Sep 17 00:00:00 2001 From: Tom Levy Date: Tue, 2 Jan 2024 02:53:04 +1300 Subject: [PATCH 3/6] Fix quoting issue in PostgreSQL install script (#237) The previous version breaks if DATABASE_USERNAME is unset/empty. --- script/install/postgresql.bash | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/script/install/postgresql.bash b/script/install/postgresql.bash index e3358b5a..f84d23ac 100644 --- a/script/install/postgresql.bash +++ b/script/install/postgresql.bash @@ -22,7 +22,7 @@ echo "$ $cmd" $cmd || exit 1 # setup user if required -psql -U$DATABASE_USERNAME postgres -c '' &> /dev/null || { +psql -U "$DATABASE_USERNAME" postgres -c '' &> /dev/null || { bash script/confirm.bash "Create new PostgreSQL user $DATABASE_USERNAME" && { cmd="sudo -u postgres createuser --superuser $DATABASE_USERNAME" echo "$ $cmd" @@ -32,7 +32,7 @@ psql -U$DATABASE_USERNAME postgres -c '' &> /dev/null || { # setup database if required if [[ $DATABASE ]] ; then - psql -U$DATABASE_USERNAME $DATABASE -c '' &> /dev/null || { + psql -U "$DATABASE_USERNAME" "$DATABASE" -c '' &> /dev/null || { bash script/confirm.bash "Create new PostgreSQL database $DATABASE" && { cmd="sudo -u postgres createdb $DATABASE" echo "$ $cmd" @@ -41,9 +41,9 @@ if [[ $DATABASE ]] ; then } fi -# setup database if required +# setup test database if required if [[ $TEST_DATABASE ]] ; then - psql -U$DATABASE_USERNAME $TEST_DATABASE -c '' &> /dev/null || { + psql -U "$DATABASE_USERNAME" "$TEST_DATABASE" -c '' &> /dev/null || { bash script/confirm.bash "Create new PostgreSQL database $TEST_DATABASE" && { cmd="sudo -u postgres createdb $TEST_DATABASE" echo "$ $cmd" From bba0a5043559e11dc5edcac22c6a8d028184d2eb Mon Sep 17 00:00:00 2001 From: Tom Levy Date: Tue, 2 Jan 2024 02:53:21 +1300 Subject: [PATCH 4/6] Change how we check if PostgreSQL server is installed (#237) Previously we used `psql --version`, which actually checks the client utility rather than the server. Switch to checking whether the `postgres` user exists, since that's a simple and fairly reliable test. This commit drops the version check, because it's fairly tricky to check the server version (and we don't have much control over the version anyway because we are installing the package from the archives). Some ideas that were discarded: - `postgres --version`: Fails because postgres is not in $PATH. - `pg_config --version`: This is supposed to print the server version, but it succeeds even if the server is not installed (e.g. when the libpq-dev package is installed). - `sudo -u postgres psql -t postgres -c 'SELECT version();'`: If the server is installed but not running, this fails. But in that case we probably shouldn't try to install the server. --- script/install/postgresql.bash | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/script/install/postgresql.bash b/script/install/postgresql.bash index f84d23ac..aa235ce4 100644 --- a/script/install/postgresql.bash +++ b/script/install/postgresql.bash @@ -4,16 +4,13 @@ cd `dirname $0`/../.. source script/install.cfg -min_version=8 - -psql --version 2>/dev/null | bash script/extract_version.bash | bash script/check_version.bash $min_version || { - echo PostgreSQL $min_version+ required! +id -u postgres &> /dev/null || { + echo "PostgreSQL required!" bash script/confirm.bash 'Install PostgreSQL' && { cmd="sudo apt-get install postgresql" echo "$ $cmd" $cmd } || exit 1 - } # required by pg gem From 20e9938de9a14b99b2119b7fdcc6deda8e54ced2 Mon Sep 17 00:00:00 2001 From: Tom Levy Date: Tue, 2 Jan 2024 02:53:38 +1300 Subject: [PATCH 5/6] Add env var DATABASE_INSTALL to skip PostgreSQL install (#237) If the database is running on another host/container, then we don't need to install the PostgreSQL server on the web server. This is useful for CI, and mirrors the existing REDIS_INSTALL environment variable. We always need the PostgreSQL client utilities, so install them separately. If we skip installation of the database then we don't try to create the database user, because that's unlikely to work with remote databases. --- script/install/postgresql.bash | 44 +++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/script/install/postgresql.bash b/script/install/postgresql.bash index aa235ce4..1256956a 100644 --- a/script/install/postgresql.bash +++ b/script/install/postgresql.bash @@ -4,29 +4,39 @@ cd `dirname $0`/../.. source script/install.cfg -id -u postgres &> /dev/null || { - echo "PostgreSQL required!" - bash script/confirm.bash 'Install PostgreSQL' && { - cmd="sudo apt-get install postgresql" - echo "$ $cmd" - $cmd - } || exit 1 -} - -# required by pg gem +# header files (required by pg gem) cmd="sudo apt-get install libpq-dev" echo "$ $cmd" $cmd || exit 1 -# setup user if required -psql -U "$DATABASE_USERNAME" postgres -c '' &> /dev/null || { - bash script/confirm.bash "Create new PostgreSQL user $DATABASE_USERNAME" && { - cmd="sudo -u postgres createuser --superuser $DATABASE_USERNAME" - echo "$ $cmd" - $cmd - } || exit 1 +# client utilities (psql, createdb, pg_dump, etc.) +command -v psql >/dev/null || { + cmd="sudo apt-get install postgresql-client" + echo "$ $cmd" + $cmd || exit 1 } +if [ "${DATABASE_INSTALL:-true}" = "true" ]; then + # install server (not required if the server is running on another host/container) + id -u postgres &> /dev/null || { + echo "PostgreSQL server required!" + bash script/confirm.bash 'Install PostgreSQL server' && { + cmd="sudo apt-get install postgresql" + echo "$ $cmd" + $cmd + } || exit 1 + } + + # setup user if required + psql -U "$DATABASE_USERNAME" postgres -c '' &> /dev/null || { + bash script/confirm.bash "Create new PostgreSQL user $DATABASE_USERNAME" && { + cmd="sudo -u postgres createuser --superuser $DATABASE_USERNAME" + echo "$ $cmd" + $cmd + } || exit 1 + } +fi + # setup database if required if [[ $DATABASE ]] ; then psql -U "$DATABASE_USERNAME" "$DATABASE" -c '' &> /dev/null || { From 7556bc860fd3985c1ccd49fa0321238e57c215ea Mon Sep 17 00:00:00 2001 From: Tom Levy Date: Tue, 2 Jan 2024 02:54:27 +1300 Subject: [PATCH 6/6] Don't use sudo for createdb (#237) We should have a working database user, so we shouldn't need sudo (and sudo wouldn't help if the database is remote). Aside: The syntax `cmd=(...)` creates a bash array, it is required to properly handle empty strings and avoid other quoting issues. --- script/install/postgresql.bash | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/script/install/postgresql.bash b/script/install/postgresql.bash index 1256956a..27f65195 100644 --- a/script/install/postgresql.bash +++ b/script/install/postgresql.bash @@ -41,9 +41,9 @@ fi if [[ $DATABASE ]] ; then psql -U "$DATABASE_USERNAME" "$DATABASE" -c '' &> /dev/null || { bash script/confirm.bash "Create new PostgreSQL database $DATABASE" && { - cmd="sudo -u postgres createdb $DATABASE" - echo "$ $cmd" - $cmd + cmd=(createdb -U "$DATABASE_USERNAME" "$DATABASE") + echo "$ ${cmd[@]}" + "${cmd[@]}" } || exit 1 } fi @@ -52,10 +52,10 @@ fi if [[ $TEST_DATABASE ]] ; then psql -U "$DATABASE_USERNAME" "$TEST_DATABASE" -c '' &> /dev/null || { bash script/confirm.bash "Create new PostgreSQL database $TEST_DATABASE" && { - cmd="sudo -u postgres createdb $TEST_DATABASE" - echo "$ $cmd" - $cmd - } || exit + cmd=(createdb -U "$DATABASE_USERNAME" "$TEST_DATABASE") + echo "$ ${cmd[@]}" + "${cmd[@]}" + } || exit 1 } fi