Kill any postgres connections before dropping and recreating database

This commit is contained in:
Rohan Mitchell
2015-04-24 12:17:39 +10:00
parent 92e03e208f
commit 9149276048
2 changed files with 16 additions and 2 deletions

View File

@@ -11,3 +11,17 @@ function exit_unless_master_merged {
exit 1
fi
}
function drop_and_recreate_database {
# Adapted from: http://stackoverflow.com/questions/12924466/capistrano-with-postgresql-error-database-is-being-accessed-by-other-users
psql -U openfoodweb postgres <<EOF
REVOKE CONNECT ON DATABASE $1 FROM public;
ALTER DATABASE $1 CONNECTION LIMIT 0;
SELECT pg_terminate_backend(procpid)
FROM pg_stat_activity
WHERE procpid <> pg_backend_pid()
AND datname='$1';
DROP DATABASE $1;
CREATE DATABASE $1;
EOF
}

View File

@@ -5,6 +5,7 @@
# current database.
set -e
source ./script/ci/includes.sh
cd /home/openfoodweb/apps/openfoodweb/current
@@ -16,8 +17,7 @@ mkdir -p db/backup
pg_dump -h localhost -U openfoodweb openfoodweb_production |gzip > db/backup/staging-`date +%Y%m%d%H%M%S`.sql.gz
echo "Loading baseline data..."
dropdb -U openfoodweb -w openfoodweb_production
createdb -U openfoodweb -w openfoodweb_production
drop_and_recreate_database "openfoodweb_production"
gunzip -c db/backup/staging-baseline.sql.gz |psql -h localhost -U openfoodweb openfoodweb_production
echo "Restarting unicorn..."