2016-01-12 13:08:43 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
2013-11-13 02:32:52 +01:00
|
|
|
|
2020-04-29 17:36:14 +02:00
|
|
|
if [[ $# == 0 ]]; then
|
|
|
|
MODE=dev
|
|
|
|
elif [[ $# == 1 ]]; then
|
|
|
|
MODE=$1
|
|
|
|
else
|
|
|
|
echo "Too many arguments"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$MODE" == "dev" ]]; then
|
|
|
|
USERNAME=zulip
|
|
|
|
DBNAME=zulip
|
|
|
|
STATUS_FILE_NAME=migration_status_dev
|
|
|
|
elif [[ "$MODE" == "test" ]]; then
|
|
|
|
USERNAME=zulip_test
|
|
|
|
DBNAME=zulip_test
|
|
|
|
STATUS_FILE_NAME=migration_status_test
|
|
|
|
else
|
2021-05-10 07:02:14 +02:00
|
|
|
echo "Usage instructions"
|
2020-04-29 17:36:14 +02:00
|
|
|
echo "Run with either no arguments (sets up devel db for local deploy--zulip with user zulip)"
|
|
|
|
echo "or specify 'test'"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
set -x
|
|
|
|
|
2019-04-13 01:30:49 +02:00
|
|
|
POSTGRES_USER="postgres"
|
2016-01-12 13:08:43 +01:00
|
|
|
if [ "$(uname)" = "OpenBSD" ]; then
|
2020-10-15 04:55:57 +02:00
|
|
|
POSTGRES_USER="_postgresql"
|
2016-01-12 13:08:43 +01:00
|
|
|
fi
|
|
|
|
|
2019-04-13 01:30:49 +02:00
|
|
|
ROOT_POSTGRES=(sudo -i -u "$POSTGRES_USER" psql)
|
2013-11-13 02:32:52 +01:00
|
|
|
DEFAULT_DB=""
|
|
|
|
if [ "$(uname)" = "Darwin" ]; then
|
2020-10-15 04:55:57 +02:00
|
|
|
ROOT_POSTGRES=(psql)
|
|
|
|
DEFAULT_DB="postgres"
|
2013-11-13 02:32:52 +01:00
|
|
|
fi
|
|
|
|
|
2016-01-12 13:08:43 +01:00
|
|
|
if [ "$(uname)" = "OpenBSD" ]; then
|
2020-10-15 04:55:57 +02:00
|
|
|
DEFAULT_DB="postgres"
|
2016-01-12 13:08:43 +01:00
|
|
|
fi
|
|
|
|
|
2015-08-19 06:08:22 +02:00
|
|
|
VAGRANTUSERNAME=$(whoami)
|
|
|
|
|
2020-04-29 17:36:14 +02:00
|
|
|
SEARCH_PATH=zulip,public
|
|
|
|
PASSWORD=$("$(dirname "$0")/../../scripts/get-django-setting" LOCAL_DATABASE_PASSWORD)
|
2014-01-30 23:40:37 +01:00
|
|
|
DBNAME_BASE=${DBNAME}_base
|
2013-11-13 02:32:52 +01:00
|
|
|
|
2019-04-13 01:30:49 +02:00
|
|
|
if ! pg_isready -U "$POSTGRES_USER" -q; then
|
2016-12-19 20:45:02 +01:00
|
|
|
set +x
|
|
|
|
echo
|
2021-05-10 07:02:14 +02:00
|
|
|
echo 'ERROR: PostgreSQL server is not running! Ensure the service is enabled.'
|
2018-08-03 02:14:50 +02:00
|
|
|
# shellcheck disable=SC2016
|
2016-12-19 20:45:02 +01:00
|
|
|
echo 'ERROR: Try `sudo service postgresql start`?'
|
2019-04-13 01:30:49 +02:00
|
|
|
echo "ERROR: You can easily test if you fixed it using: pg_isready -U \$POSTGRES_USER"
|
2016-12-19 20:45:02 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-06-18 01:09:07 +02:00
|
|
|
# We need to remove the stamp file indicating that the database
|
|
|
|
# is properly provisioned with migrations.
|
|
|
|
uuid_var_path=$($(readlink -f "$(dirname "$0")/../../scripts/lib/zulip_tools.py") get_dev_uuid)
|
|
|
|
rm -f "$uuid_var_path/$STATUS_FILE_NAME"
|
|
|
|
|
2020-10-15 04:55:57 +02:00
|
|
|
"${ROOT_POSTGRES[@]}" -v ON_ERROR_STOP=1 -e "$DEFAULT_DB" <<EOF
|
2019-04-19 23:30:28 +02:00
|
|
|
DO \$\$BEGIN
|
|
|
|
CREATE USER $USERNAME;
|
|
|
|
EXCEPTION WHEN duplicate_object THEN
|
|
|
|
RAISE NOTICE '$USERNAME user already exists';
|
|
|
|
END\$\$;
|
2015-08-21 00:29:48 +02:00
|
|
|
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
|
|
|
|
2019-04-19 23:30:28 +02:00
|
|
|
DO \$\$BEGIN
|
2024-01-19 18:32:14 +01:00
|
|
|
CREATE USER "$VAGRANTUSERNAME";
|
2019-04-19 23:30:28 +02:00
|
|
|
EXCEPTION WHEN duplicate_object THEN
|
|
|
|
RAISE NOTICE '$VAGRANTUSERNAME user already exists';
|
|
|
|
END\$\$;
|
2024-01-19 18:32:14 +01: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:"
|
2018-08-03 02:14:50 +02:00
|
|
|
PGPASS_ESCAPED_PREFIX="*:\\*:\\*:$USERNAME:"
|
2015-09-26 03:47:30 +02:00
|
|
|
if ! grep -q "$PGPASS_ESCAPED_PREFIX" ~/.pgpass; then
|
2020-10-15 04:55:57 +02:00
|
|
|
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
|
|
|
|
|
2020-06-16 01:36:02 +02:00
|
|
|
PGHOST=localhost PGUSER="$USERNAME" \
|
|
|
|
"$(dirname "$0")/../../scripts/setup/terminate-psql-sessions" "$DBNAME" "$DBNAME_BASE"
|
2015-09-28 19:45:35 +02:00
|
|
|
|
2019-04-02 01:00:57 +02:00
|
|
|
psql -v ON_ERROR_STOP=1 -e -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
|
|
|
|
|
2019-04-02 01:00:57 +02:00
|
|
|
psql -v ON_ERROR_STOP=1 -e -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
|
|
|
|
2020-10-15 04:55:57 +02:00
|
|
|
"${ROOT_POSTGRES[@]}" -v ON_ERROR_STOP=1 -e "$DBNAME_BASE" <<EOF
|
2016-04-24 17:08:51 +02:00
|
|
|
CREATE EXTENSION pgroonga;
|
2023-05-16 06:13:42 +02:00
|
|
|
|
|
|
|
-- Work around https://github.com/pgroonga/pgroonga/issues/335
|
|
|
|
SELECT unnest(
|
|
|
|
CASE WHEN current_setting('server_version_num')::integer >= 150000
|
|
|
|
AND current_setting('pgroonga.enable_wal', TRUE) = 'on' IS NOT TRUE THEN
|
|
|
|
ARRAY['ALTER SYSTEM SET pgroonga.enable_wal = ''on''', 'SELECT pg_reload_conf()']
|
|
|
|
END
|
|
|
|
) \gexec
|
2014-01-30 23:40:37 +01:00
|
|
|
EOF
|
|
|
|
|
2019-04-02 01:00:57 +02:00
|
|
|
psql -v ON_ERROR_STOP=1 -e -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
|
|
|
|
|
2019-10-01 23:42:35 +02:00
|
|
|
"$(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"
|