2015-09-28 19:45:35 +02:00
|
|
|
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
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.
|
|
|
|
records=`su "$POSTGRES_USER" -c "psql -Atc 'SELECT COUNT(*) FROM zulip.zerver_message;' zulip" | cat`
|
|
|
|
|
|
|
|
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.
|
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
|
|
|
CREATE USER zulip;
|
|
|
|
ALTER ROLE zulip SET search_path TO zulip,public;
|
|
|
|
DROP DATABASE IF EXISTS zulip;
|
2013-11-13 04:29:49 +01:00
|
|
|
CREATE DATABASE zulip OWNER=zulip;
|
2013-02-25 21:46:36 +01:00
|
|
|
EOF
|
|
|
|
|
2016-04-27 00:23:37 +02:00
|
|
|
su "$POSTGRES_USER" -c 'psql zulip' <<EOF
|
2015-08-28 18:37:46 +02:00
|
|
|
CREATE SCHEMA zulip AUTHORIZATION zulip;
|
2015-08-21 07:17:25 +02:00
|
|
|
CREATE EXTENSION tsearch_extras SCHEMA zulip;
|
|
|
|
EOF
|
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"
|