Skip to content

Commit

Permalink
Load staging baseline even if db in use
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Oct 29, 2024
1 parent 8a0d9d9 commit 3f35369
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
25 changes: 23 additions & 2 deletions script/ci/restore-staging-baseline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

set -e

# Load default values if not already set.
: ${DB_USER='ofn_user'}
: ${DB_DATABASE='openfoodnetwork'}

Expand All @@ -18,8 +19,28 @@ else
exit 1
fi

dropdb -h localhost -U "$DB_USER" "$DB_DATABASE" --if-exists
createdb -h localhost -U "$DB_USER" "$DB_DATABASE"
# We want to re-create the database but it's still in use.
# The SQL query below is a workaround suppoting old postgresql versions.
#
# Once we have at least Postgresql 13, we can replace these SQL commands with:
#
# DROP DATABASE IF EXISTS $DB_DATABASE WITH FORCE
# CREATE DATABASE $DB_DATABASE
#
# Versions:
# - Ubuntu 16: psql 9.5
# - Ubuntu 18: psql 10
# - Ubuntu 20: psql 15 <-- switch here
psql -h localhost -U "$DB_USER" postgres <<EOF
REVOKE CONNECT ON DATABASE $DB_DATABASE FROM public;
ALTER DATABASE $DB_DATABASE CONNECTION LIMIT 0;
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE pid <> pg_backend_pid()
AND datname='$DB_DATABASE';
DROP DATABASE $DB_DATABASE;
CREATE DATABASE $DB_DATABASE;
EOF

gunzip -c "$srcfile" | psql -h localhost -U "$DB_USER" "$DB_DATABASE"
echo "Staging baseline data restored."
1 change: 1 addition & 0 deletions script/ci/save-staging-baseline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

set -e

# Load default values if not already set.
: ${DB_USER='ofn_user'}
: ${DB_DATABASE='openfoodnetwork'}

Expand Down

0 comments on commit 3f35369

Please sign in to comment.