Drop database users prior to DROP/CREATE database.

This fixes an annoying issue where one tries to rebuild the database,
and it fails due to there being existing connections.

The one thing that is potentially scary about this implementation is
that it means it's now a lot easier to accidentally drop your
production database by running the wrong script; might be worth adding
a "--force" flag controlling this behavior or something.

Thanks to Nemanja Stanarevic and Neeraj Wahi for prototypes of this
implementation!  They did most of the work and testing for this.
This commit is contained in:
Tim Abbott 2015-09-28 10:45:35 -07:00
parent b7cd000af6
commit 3c31f9a2e3
5 changed files with 47 additions and 2 deletions

View File

@ -1,8 +1,14 @@
#!/bin/sh -xe
#!/bin/bash
set -e
set -x
# Make sure the current working directory is readable
cd /
# Drop any open connections to any old database. Hackishly call using
# source because postgres user can't read /root/zulip/scripts/setup.
source "$(dirname "$0")/terminate-psql-sessions" postgres zulip zulip_base
su postgres -c psql <<EOF
CREATE USER zulip;
ALTER ROLE zulip SET search_path TO zulip,public;
@ -15,6 +21,7 @@ CREATE SCHEMA zulip AUTHORIZATION zulip;
CREATE EXTENSION tsearch_extras SCHEMA zulip;
EOF
# Clear memcached to avoid contamination from previous database state
sh "$(dirname "$0")/flush-memcached"
echo "Database created"

View File

@ -0,0 +1,31 @@
#!/bin/bash
set -e
set -x
cd /
if [ "$EUID" -eq 0 ]; then
version=$(su postgres -c 'psql -A -t -d postgres -c "show server_version"')
else
version=$(psql -A -t -d postgres -c "show server_version")
fi
major=$(echo $version | cut -d. -f1,2)
pid_style=$(echo "$major > 9.1" | bc -l)
username=$1
shift
tables=$(echo "'$@'" | sed "s/ /','/g")
if [ "$pid_style" = "1" ]; then
pidname="pid"
else
pidname="procpid"
fi
if [ "$EUID" -eq 0 ]; then
su postgres -c psql postgres postgres <<EOF
SELECT pg_terminate_backend($pidname) FROM pg_stat_activity WHERE datname IN ($tables);
EOF
else
psql -h localhost postgres "$username" <<EOF
SELECT pg_terminate_backend($pidname) FROM pg_stat_activity WHERE datname IN ($tables);
EOF
fi

View File

@ -1,5 +1,7 @@
#!/bin/sh -xe
"$(dirname "$0")/../scripts/setup/terminate-psql-sessions" zulip zulip zulip_base
psql -h localhost postgres zulip <<EOF
DROP DATABASE IF EXISTS zulip;
CREATE DATABASE zulip TEMPLATE zulip_base;

View File

@ -11,6 +11,7 @@ if [ "$template_grep_error_code" == "0" ]; then
if [ -e zerver/fixtures/migration-status ] &&
cmp -s zerver/fixtures/available-migrations zerver/fixtures/migration-status &&
[ "$1" != "--force" ]; then
"$(dirname "$0")/../scripts/setup/terminate-psql-sessions" zulip zulip_test zulip_test_base zulip_test_template
psql -h localhost postgres zulip_test << EOF
DROP DATABASE IF EXISTS zulip_test;
CREATE DATABASE zulip_test TEMPLATE zulip_test_template;
@ -22,6 +23,8 @@ fi
mkdir -p zerver/fixtures
"$(dirname "$0")/../scripts/setup/terminate-psql-sessions" zulip zulip_test zulip_test_base zulip_test_template
psql -h localhost postgres zulip_test <<EOF
DROP DATABASE IF EXISTS zulip_test;
CREATE DATABASE zulip_test TEMPLATE zulip_test_base;

View File

@ -49,6 +49,8 @@ else
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;