2015-09-28 19:45:35 +02:00
|
|
|
#!/bin/bash
|
|
|
|
set -e
|
2018-08-21 20:36:50 +02:00
|
|
|
|
|
|
|
if [ "$EUID" -ne 0 ]; then
|
|
|
|
echo "Error: This script must be run as root" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2015-09-28 19:45:35 +02:00
|
|
|
set -x
|
2013-02-25 21:46:36 +01:00
|
|
|
|
2016-04-27 00:23:37 +02:00
|
|
|
# What user should we use for connecting to the database
|
|
|
|
POSTGRES_USER="${POSTGRES_USER:-postgres}"
|
|
|
|
|
2016-04-21 00:29:11 +02:00
|
|
|
# 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.
|
2018-08-03 02:14:47 +02:00
|
|
|
records=$(su "$POSTGRES_USER" -c "psql -Atc 'SELECT COUNT(*) FROM zulip.zerver_message;' zulip" | cat)
|
2016-04-21 00:29:11 +02:00
|
|
|
|
|
|
|
if [[ $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? " -n 1 -r
|
|
|
|
echo
|
|
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]
|
|
|
|
then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
2015-10-15 04:47:35 +02:00
|
|
|
# Shut down all services to ensure a quiescent state.
|
2016-04-27 00:20:06 +02:00
|
|
|
if [ -e "/var/run/supervisor.sock" ]; then
|
|
|
|
supervisorctl stop all
|
|
|
|
fi
|
2015-10-15 04:47:35 +02:00
|
|
|
|
2015-09-28 19:45:35 +02:00
|
|
|
# Drop any open connections to any old database. Hackishly call using
|
2017-08-10 02:30:47 +02:00
|
|
|
# `source`, because postgres user may not be able to read this directory
|
|
|
|
# if unpacked by root.
|
2018-08-03 02:14:47 +02:00
|
|
|
# shellcheck source=/dev/null
|
2015-09-28 19:45:35 +02:00
|
|
|
source "$(dirname "$0")/terminate-psql-sessions" postgres zulip zulip_base
|
|
|
|
|
2015-12-22 19:43:20 +01:00
|
|
|
(
|
|
|
|
# Make sure the current working directory is readable by postgres
|
|
|
|
cd /
|
|
|
|
|
2016-04-27 00:23:37 +02:00
|
|
|
su "$POSTGRES_USER" -c psql <<EOF
|
2013-11-13 02:32:52 +01:00
|
|
|
DROP DATABASE IF EXISTS zulip;
|
2013-02-25 21:46:36 +01:00
|
|
|
EOF
|
|
|
|
|
2018-05-16 01:04:50 +02:00
|
|
|
"$(dirname "$0")/postgres-create-db"
|
2015-12-22 19:43:20 +01:00
|
|
|
)
|
2015-08-21 07:17:25 +02:00
|
|
|
|
2015-09-28 19:45:35 +02:00
|
|
|
# Clear memcached to avoid contamination from previous database state
|
2016-09-29 05:46:34 +02:00
|
|
|
"$(dirname "$0")/flush-memcached"
|
2015-09-28 19:57:40 +02:00
|
|
|
|
2013-02-25 21:46:36 +01:00
|
|
|
echo "Database created"
|