mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
If we run provision.py a second time, there will already be zulip/zulip_test users, so the CREATE USER will fail and the password won't get updated to the newly generated value. By creating the user and setting the password in two commands, we allow the creation to fail without affecting whether the password is set. Also the quoting for updating .pgpass was wrong. (imported from commit 5e249813c17cb4829e4e4958e92aaa30563c5f96)
81 lines
1.9 KiB
Bash
Executable File
81 lines
1.9 KiB
Bash
Executable File
#!/bin/bash -xe
|
|
|
|
ROOT_POSTGRES="sudo -u postgres psql"
|
|
DEFAULT_DB=""
|
|
if [ "$(uname)" = "Darwin" ]; then
|
|
ROOT_POSTGRES="psql"
|
|
DEFAULT_DB="postgres"
|
|
fi
|
|
|
|
VAGRANTUSERNAME=$(whoami)
|
|
|
|
if [[ $# == 0 ]]; then
|
|
USERNAME=zulip
|
|
PASSWORD=$($(dirname $0)/../bin/get-django-setting LOCAL_DATABASE_PASSWORD)
|
|
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
|
|
|
|
DBNAME_BASE=${DBNAME}_base
|
|
|
|
$ROOT_POSTGRES $DEFAULT_DB << EOF
|
|
CREATE USER $USERNAME;
|
|
ALTER USER $USERNAME PASSWORD '$PASSWORD';
|
|
ALTER USER $USERNAME CREATEDB;
|
|
ALTER ROLE $USERNAME SET search_path TO $SEARCH_PATH;
|
|
|
|
DO \$\$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT * FROM pg_catalog.pg_user WHERE usename -- [sic]
|
|
= '$VAGRANTUSERNAME') THEN
|
|
|
|
CREATE USER $VAGRANTUSERNAME;
|
|
END IF;
|
|
END
|
|
\$\$ ;
|
|
GRANT $USERNAME TO $VAGRANTUSERNAME;
|
|
ALTER ROLE $VAGRANTUSERNAME SET search_path TO $SEARCH_PATH;
|
|
EOF
|
|
|
|
umask go-rw
|
|
PGPASS_PREFIX="*:*:*:$USERNAME:"
|
|
PGPASS_ESCAPED_PREFIX="*:\*:\*:$USERNAME:"
|
|
if ! $(grep -q "$PGPASS_ESCAPED_PREFIX" ~/.pgpass); then
|
|
echo $PGPASS_PREFIX$PASSWORD >> ~/.pgpass
|
|
else
|
|
sed -i "s/$PGPASS_ESCAPED_PREFIX.*\$/$PGPASS_PREFIX$PASSWORD/" ~/.pgpass
|
|
fi
|
|
chmod go-rw ~/.pgpass
|
|
|
|
psql -h localhost postgres $USERNAME <<EOF
|
|
DROP DATABASE IF EXISTS $DBNAME;
|
|
DROP DATABASE IF EXISTS $DBNAME_BASE;
|
|
CREATE DATABASE $DBNAME_BASE
|
|
EOF
|
|
|
|
psql -h localhost $DBNAME_BASE $USERNAME <<EOF
|
|
CREATE SCHEMA zulip;
|
|
EOF
|
|
|
|
$ROOT_POSTGRES $DBNAME_BASE << EOF
|
|
CREATE EXTENSION tsearch_extras SCHEMA zulip;
|
|
EOF
|
|
|
|
psql -h localhost postgres $USERNAME <<EOF
|
|
CREATE DATABASE $DBNAME TEMPLATE $DBNAME_BASE;
|
|
EOF
|
|
|
|
echo "Database created"
|
|
|