postgres-init-db: Fix invocation from a relative path.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg 2020-02-18 20:46:57 -08:00 committed by Tim Abbott
parent cd43a33783
commit fd2d642f95
1 changed files with 11 additions and 18 deletions

View File

@ -10,14 +10,12 @@ set -x
# What user should we use for connecting to the database
POSTGRES_USER="${POSTGRES_USER:-postgres}"
# We pipe this output through cat to ensure we always get return code 0
# We have to do this because on production database zulip may not exist, so psql
# will fail with return code 2. Because set -e is on, this will cause the script
# to bail.
records=$(su "$POSTGRES_USER" -c "psql -v ON_ERROR_STOP=1 -Atc 'SELECT COUNT(*) FROM zulip.zerver_message;' zulip" | cat)
if [[ $records -gt 200 ]]
then
# This psql command may fail because the zulip database doesnt exist,
# hence the &&.
if records="$(
cd / # Make sure the current working directory is readable by postgres
su "$POSTGRES_USER" -c "psql -v ON_ERROR_STOP=1 -Atc 'SELECT COUNT(*) FROM zulip.zerver_message;' zulip"
)" && [ "$records" -gt 200 ]; then
set +x
echo "WARNING: This will delete your Zulip database which currently contains $records messages."
read -p "Do you want to proceed? [y/N] " -r
@ -34,18 +32,13 @@ if [ -e "/var/run/supervisor.sock" ]; then
supervisorctl stop all
fi
# Drop any open connections to any old database. Hackishly call using
# `source`, because postgres user may not be able to read this directory
# if unpacked by root.
# shellcheck source=terminate-psql-sessions
source "$(dirname "$0")/terminate-psql-sessions" postgres zulip zulip_base
# Drop any open connections to any old database.
"$(dirname "$0")/terminate-psql-sessions" postgres zulip zulip_base
(
# Make sure the current working directory is readable by postgres
cd /
su "$POSTGRES_USER" -c 'psql -v ON_ERROR_STOP=1 -e' < "$(dirname "$0")/create-db.sql"
)
cd / # Make sure the current working directory is readable by postgres
su "$POSTGRES_USER" -c 'psql -v ON_ERROR_STOP=1 -e'
) < "$(dirname "$0")/create-db.sql"
# Clear memcached to avoid contamination from previous database state
"$(dirname "$0")/flush-memcached"