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

View File

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