Files
zulip/tools/setup/postgres-init-dev-db
Kouhei Sutou 683f49aa99 Support full text search for all languages using pgroonga.
This adds support for using PGroonga to back the Zulip full-text
search feature.  Because built-in PostgreSQL full text search doesn't
support languages that don't put space between terms such as Japanese,
Chinese and so on. PGroonga supports all languages including Japanese
and Chinese.

Developers will need to re-provision when rebasing past this patch for
the tests to pass, since provision is what installs the PGroonga
package and extension.

PGroonga is enabled by default in development but not in production;
the hope is that after the PGroonga support is tested further, we can
enable it by default.

Fixes #615.

[docs and tests tweaked by tabbott]
2016-08-26 21:04:03 -07:00

89 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -x
set -e
DEFAULT_USER="postgres"
if [ "$(uname)" = "OpenBSD" ]; then
DEFAULT_USER="_postgresql"
fi
ROOT_POSTGRES="sudo -u "$DEFAULT_USER" psql"
DEFAULT_DB=""
if [ "$(uname)" = "Darwin" ]; then
ROOT_POSTGRES="psql"
DEFAULT_DB="postgres"
fi
if [ "$(uname)" = "OpenBSD" ]; then
DEFAULT_DB="postgres"
fi
VAGRANTUSERNAME=$(whoami)
if [[ $# == 0 ]]; then
USERNAME=zulip
PASSWORD=$("$(dirname "$0")/../../scripts/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;
CREATE USER $VAGRANTUSERNAME;
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
"$(dirname "$0")/../../scripts/setup/terminate-psql-sessions" "$USERNAME" "$DBNAME" "$DBNAME_BASE"
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;
CREATE EXTENSION pgroonga;
GRANT USAGE ON SCHEMA pgroonga TO $USERNAME;
EOF
psql -h localhost postgres "$USERNAME" <<EOF
CREATE DATABASE $DBNAME TEMPLATE $DBNAME_BASE;
EOF
sh "$(dirname "$0")/../../scripts/setup/flush-memcached"
echo "Database created"