2014-01-30 23:06:31 +01:00
|
|
|
#!/bin/bash -xe
|
2013-11-13 02:32:52 +01:00
|
|
|
|
|
|
|
ROOT_POSTGRES="sudo -u postgres psql"
|
|
|
|
DEFAULT_DB=""
|
|
|
|
if [ "$(uname)" = "Darwin" ]; then
|
|
|
|
ROOT_POSTGRES="psql"
|
|
|
|
DEFAULT_DB="postgres"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $# == 0 ]]; then
|
|
|
|
USERNAME=zulip
|
2014-01-06 21:56:46 +01:00
|
|
|
PASSWORD=$($(dirname $0)/../bin/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
|
|
|
|
|
|
|
$ROOT_POSTGRES $DEFAULT_DB << EOF
|
|
|
|
CREATE USER $USERNAME WITH PASSWORD '$PASSWORD';
|
|
|
|
ALTER USER $USERNAME CREATEDB;
|
|
|
|
ALTER ROLE $USERNAME SET search_path TO $SEARCH_PATH;
|
|
|
|
EOF
|
|
|
|
|
|
|
|
umask go-rw
|
|
|
|
echo "*:*:*:$USERNAME:$PASSWORD" >> ~/.pgpass
|
|
|
|
chmod go-rw ~/.pgpass
|
|
|
|
|
|
|
|
psql -h localhost postgres $USERNAME <<EOF
|
|
|
|
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
|
|
|
|
|
2014-01-30 23:40:37 +01:00
|
|
|
psql -h localhost $DBNAME_BASE $USERNAME <<EOF
|
|
|
|
CREATE SCHEMA zulip;
|
|
|
|
EOF
|
2013-11-13 02:32:52 +01:00
|
|
|
|
2014-01-30 23:40:37 +01:00
|
|
|
$ROOT_POSTGRES $DBNAME_BASE << EOF
|
2013-11-13 02:32:52 +01:00
|
|
|
DROP SCHEMA public CASCADE;
|
2014-01-30 23:40:37 +01:00
|
|
|
CREATE EXTENSION tsearch_extras SCHEMA zulip;
|
|
|
|
EOF
|
|
|
|
|
|
|
|
psql -h localhost postgres $USERNAME <<EOF
|
|
|
|
CREATE DATABASE $DBNAME TEMPLATE $DBNAME_BASE;
|
2013-11-13 02:32:52 +01:00
|
|
|
EOF
|
|
|
|
|
|
|
|
echo "Database created"
|
|
|
|
|