diff --git a/script/ci/check_github_status.sh b/script/buildkite-archive/check_github_status.sh similarity index 100% rename from script/ci/check_github_status.sh rename to script/buildkite-archive/check_github_status.sh diff --git a/script/ci/includes.sh b/script/buildkite-archive/includes.sh similarity index 100% rename from script/ci/includes.sh rename to script/buildkite-archive/includes.sh diff --git a/script/ci/merge_branch_to_master.sh b/script/buildkite-archive/merge_branch_to_master.sh similarity index 100% rename from script/ci/merge_branch_to_master.sh rename to script/buildkite-archive/merge_branch_to_master.sh diff --git a/script/ci/merge_master_into_branch.sh b/script/buildkite-archive/merge_master_into_branch.sh similarity index 100% rename from script/ci/merge_master_into_branch.sh rename to script/buildkite-archive/merge_master_into_branch.sh diff --git a/script/ci/push_to_production.sh b/script/buildkite-archive/push_to_production.sh similarity index 100% rename from script/ci/push_to_production.sh rename to script/buildkite-archive/push_to_production.sh diff --git a/script/ci/push_to_staging.sh b/script/buildkite-archive/push_to_staging.sh similarity index 100% rename from script/ci/push_to_staging.sh rename to script/buildkite-archive/push_to_staging.sh diff --git a/script/ci/save_staging_baseline.sh b/script/buildkite-archive/save_staging_baseline.sh similarity index 100% rename from script/ci/save_staging_baseline.sh rename to script/buildkite-archive/save_staging_baseline.sh diff --git a/script/ci/restore-staging-baseline.sh b/script/ci/restore-staging-baseline.sh new file mode 100755 index 0000000000..71e43ae445 --- /dev/null +++ b/script/ci/restore-staging-baseline.sh @@ -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'} +: ${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 < 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." diff --git a/script/ci/save-staging-baseline.sh b/script/ci/save-staging-baseline.sh new file mode 100755 index 0000000000..076970d499 --- /dev/null +++ b/script/ci/save-staging-baseline.sh @@ -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."