2016-01-12 13:08:43 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -x
|
|
|
|
set -e
|
2013-11-13 02:32:52 +01:00
|
|
|
|
2016-01-12 13:08:43 +01:00
|
|
|
DEFAULT_USER="postgres"
|
|
|
|
if [ "$(uname)" = "OpenBSD" ]; then
|
|
|
|
DEFAULT_USER="_postgresql"
|
|
|
|
fi
|
|
|
|
|
|
|
|
ROOT_POSTGRES="sudo -u "$DEFAULT_USER" psql"
|
2013-11-13 02:32:52 +01:00
|
|
|
DEFAULT_DB=""
|
|
|
|
if [ "$(uname)" = "Darwin" ]; then
|
|
|
|
ROOT_POSTGRES="psql"
|
|
|
|
DEFAULT_DB="postgres"
|
|
|
|
fi
|
|
|
|
|
2016-01-12 13:08:43 +01:00
|
|
|
if [ "$(uname)" = "OpenBSD" ]; then
|
|
|
|
DEFAULT_DB="postgres"
|
|
|
|
fi
|
|
|
|
|
2015-08-19 06:08:22 +02:00
|
|
|
VAGRANTUSERNAME=$(whoami)
|
|
|
|
|
2013-11-13 02:32:52 +01:00
|
|
|
if [[ $# == 0 ]]; then
|
|
|
|
USERNAME=zulip
|
2016-05-26 12:44:30 +02:00
|
|
|
PASSWORD=$("$(dirname "$0")/../../scripts/get-django-setting" LOCAL_DATABASE_PASSWORD)
|
2013-11-13 02:32:52 +01:00
|
|
|
DBNAME=zulip
|
|
|
|
SEARCH_PATH="$USERNAME",public
|
|
|
|
elif [[ $# == 4 ]]; then
|
|
|
|
USERNAME=$1
|
|
|
|
PASSWORD=$2
|
|
|
|
DBNAME=$3
|
|
|
|
SEARCH_PATH=$4
|
|
|
|
else
|
|
|
|
echo "Usage Instructions"
|
|
|
|
echo "Run with either no arguments (sets up devel db for local deploy--zulip with user zulip)"
|
|
|
|
echo "or specify <db-username> <password> <db-name> <user-schema-search-path>"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2014-01-30 23:40:37 +01:00
|
|
|
DBNAME_BASE=${DBNAME}_base
|
2013-11-13 02:32:52 +01:00
|
|
|
|
2015-09-26 03:47:30 +02:00
|
|
|
$ROOT_POSTGRES "$DEFAULT_DB" << EOF
|
2015-08-21 00:29:48 +02:00
|
|
|
CREATE USER $USERNAME;
|
|
|
|
ALTER USER $USERNAME PASSWORD '$PASSWORD';
|
2013-11-13 02:32:52 +01:00
|
|
|
ALTER USER $USERNAME CREATEDB;
|
|
|
|
ALTER ROLE $USERNAME SET search_path TO $SEARCH_PATH;
|
2015-08-19 06:08:22 +02:00
|
|
|
|
2015-08-21 00:39:39 +02:00
|
|
|
CREATE USER $VAGRANTUSERNAME;
|
2015-08-19 06:08:22 +02:00
|
|
|
GRANT $USERNAME TO $VAGRANTUSERNAME;
|
|
|
|
ALTER ROLE $VAGRANTUSERNAME SET search_path TO $SEARCH_PATH;
|
2013-11-13 02:32:52 +01:00
|
|
|
EOF
|
|
|
|
|
|
|
|
umask go-rw
|
2015-08-20 08:15:21 +02:00
|
|
|
PGPASS_PREFIX="*:*:*:$USERNAME:"
|
|
|
|
PGPASS_ESCAPED_PREFIX="*:\*:\*:$USERNAME:"
|
2015-09-26 03:47:30 +02:00
|
|
|
if ! grep -q "$PGPASS_ESCAPED_PREFIX" ~/.pgpass; then
|
|
|
|
echo "$PGPASS_PREFIX$PASSWORD" >> ~/.pgpass
|
2015-08-20 08:15:21 +02:00
|
|
|
else
|
2015-08-21 00:29:48 +02:00
|
|
|
sed -i "s/$PGPASS_ESCAPED_PREFIX.*\$/$PGPASS_PREFIX$PASSWORD/" ~/.pgpass
|
2014-01-30 23:42:25 +01:00
|
|
|
fi
|
2013-11-13 02:32:52 +01:00
|
|
|
chmod go-rw ~/.pgpass
|
|
|
|
|
2016-05-26 12:44:30 +02:00
|
|
|
"$(dirname "$0")/../../scripts/setup/terminate-psql-sessions" "$USERNAME" "$DBNAME" "$DBNAME_BASE"
|
2015-09-28 19:45:35 +02:00
|
|
|
|
2015-09-26 03:47:30 +02:00
|
|
|
psql -h localhost postgres "$USERNAME" <<EOF
|
2013-11-13 02:32:52 +01:00
|
|
|
DROP DATABASE IF EXISTS $DBNAME;
|
2014-01-30 23:40:37 +01:00
|
|
|
DROP DATABASE IF EXISTS $DBNAME_BASE;
|
|
|
|
CREATE DATABASE $DBNAME_BASE
|
2013-11-13 02:32:52 +01:00
|
|
|
EOF
|
|
|
|
|
2015-09-26 03:47:30 +02:00
|
|
|
psql -h localhost "$DBNAME_BASE" "$USERNAME" <<EOF
|
2014-01-30 23:40:37 +01:00
|
|
|
CREATE SCHEMA zulip;
|
|
|
|
EOF
|
2013-11-13 02:32:52 +01:00
|
|
|
|
2015-09-26 03:47:30 +02:00
|
|
|
$ROOT_POSTGRES "$DBNAME_BASE" << EOF
|
2014-01-30 23:40:37 +01:00
|
|
|
CREATE EXTENSION tsearch_extras SCHEMA zulip;
|
2016-04-24 17:08:51 +02:00
|
|
|
CREATE EXTENSION pgroonga;
|
|
|
|
GRANT USAGE ON SCHEMA pgroonga TO $USERNAME;
|
2014-01-30 23:40:37 +01:00
|
|
|
EOF
|
|
|
|
|
2015-09-26 03:47:30 +02:00
|
|
|
psql -h localhost postgres "$USERNAME" <<EOF
|
2014-01-30 23:40:37 +01:00
|
|
|
CREATE DATABASE $DBNAME TEMPLATE $DBNAME_BASE;
|
2013-11-13 02:32:52 +01:00
|
|
|
EOF
|
|
|
|
|
2016-05-26 12:44:30 +02:00
|
|
|
sh "$(dirname "$0")/../../scripts/setup/flush-memcached"
|
2015-09-28 19:57:40 +02:00
|
|
|
|
2013-11-13 02:32:52 +01:00
|
|
|
echo "Database created"
|