Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scripts to save and restore baseline data #12947

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
46 changes: 46 additions & 0 deletions script/ci/restore-staging-baseline.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# Restore baseline data for staging in case a pull request messed with
# the database.

set -e

# Load default values if not already set.
: ${DB_USER='ofn_user'}
dacook marked this conversation as resolved.
Show resolved Hide resolved
: ${DB_DATABASE='openfoodnetwork'}

srcdir="$HOME/apps/openfoodnetwork/shared"
srcfile="$srcdir/staging-baseline.sql.gz"

if [ -f "$srcfile" ]; then
echo "Restoring data from: $srcfile"
else
>&2 echo "[Error] No baseline data available at: $srcfile"
exit 1
fi

# 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."
17 changes: 17 additions & 0 deletions script/ci/save-staging-baseline.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

# Save baseline data for staging which can be restored in case a pull request
# messes with the database.

set -e

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

dstdir="$HOME/apps/openfoodnetwork/shared"
dstfile="$dstdir/staging-baseline.sql.gz"

mkdir -p "$dstdir"
pg_dump -h localhost -U "$DB_USER" "$DB_DATABASE" | gzip > "$dstfile"
echo "Staging baseline data saved."
Loading