2018-12-18 02:08:53 +01:00
|
|
|
|
#!/usr/bin/env bash
|
2015-09-28 19:45:35 +02:00
|
|
|
|
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}"
|
|
|
|
|
|
2020-02-19 05:46:57 +01:00
|
|
|
|
# This psql command may fail because the zulip database doesn’t exist,
|
|
|
|
|
# hence the &&.
|
|
|
|
|
if records="$(
|
2020-10-15 04:55:57 +02:00
|
|
|
|
cd / # Make sure the current working directory is readable by postgres
|
2020-02-19 05:46:57 +01:00
|
|
|
|
su "$POSTGRES_USER" -c "psql -v ON_ERROR_STOP=1 -Atc 'SELECT COUNT(*) FROM zulip.zerver_message;' zulip"
|
|
|
|
|
)" && [ "$records" -gt 200 ]; then
|
2016-04-21 00:29:11 +02:00
|
|
|
|
set +x
|
|
|
|
|
echo "WARNING: This will delete your Zulip database which currently contains $records messages."
|
2019-09-13 00:14:32 +02:00
|
|
|
|
read -p "Do you want to proceed? [y/N] " -r
|
2016-04-21 00:29:11 +02:00
|
|
|
|
echo
|
2020-10-15 04:55:57 +02:00
|
|
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
2016-04-21 00:29:11 +02:00
|
|
|
|
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
|
|
|
|
|
2020-02-19 05:46:57 +01:00
|
|
|
|
# Drop any open connections to any old database.
|
2020-07-02 23:00:35 +02:00
|
|
|
|
# Send the script via stdin in case the postgres user lacks permission to read it.
|
2020-06-16 01:36:02 +02:00
|
|
|
|
su -s /usr/bin/env - -- "$POSTGRES_USER" \
|
2020-10-15 04:55:57 +02:00
|
|
|
|
bash -s - zulip zulip_base <"$(dirname "$0")/terminate-psql-sessions"
|
2015-09-28 19:45:35 +02:00
|
|
|
|
|
2015-12-22 19:43:20 +01:00
|
|
|
|
(
|
2020-10-15 04:55:57 +02:00
|
|
|
|
cd / # Make sure the current working directory is readable by postgres
|
2020-02-19 05:46:57 +01:00
|
|
|
|
su "$POSTGRES_USER" -c 'psql -v ON_ERROR_STOP=1 -e'
|
2020-10-15 04:55:57 +02:00
|
|
|
|
) <"$(dirname "$0")/create-db.sql"
|
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"
|