From decf1e6f03bb074eca2c989733406d94ed4208a9 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 25 Oct 2024 14:23:33 +1100 Subject: [PATCH 1/3] Move old Buildkite scripts to archive folder We could delete them all but I want use some of their wisdom for new CI scripts. --- script/{ci => buildkite-archive}/check_github_status.sh | 0 script/{ci => buildkite-archive}/includes.sh | 0 script/{ci => buildkite-archive}/merge_branch_to_master.sh | 0 script/{ci => buildkite-archive}/merge_master_into_branch.sh | 0 script/{ci => buildkite-archive}/push_to_production.sh | 0 script/{ci => buildkite-archive}/push_to_staging.sh | 0 script/{ci => buildkite-archive}/save_staging_baseline.sh | 0 7 files changed, 0 insertions(+), 0 deletions(-) rename script/{ci => buildkite-archive}/check_github_status.sh (100%) rename script/{ci => buildkite-archive}/includes.sh (100%) rename script/{ci => buildkite-archive}/merge_branch_to_master.sh (100%) rename script/{ci => buildkite-archive}/merge_master_into_branch.sh (100%) rename script/{ci => buildkite-archive}/push_to_production.sh (100%) rename script/{ci => buildkite-archive}/push_to_staging.sh (100%) rename script/{ci => buildkite-archive}/save_staging_baseline.sh (100%) 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 From 8a0d9d99e570a225e774a3beef1b8cdf98a7b22f Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 25 Oct 2024 15:07:39 +1100 Subject: [PATCH 2/3] Add scripts to save and restore baseline data --- script/ci/restore-staging-baseline.sh | 25 +++++++++++++++++++++++++ script/ci/save-staging-baseline.sh | 16 ++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100755 script/ci/restore-staging-baseline.sh create mode 100755 script/ci/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..c2945b86cf --- /dev/null +++ b/script/ci/restore-staging-baseline.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# Restore baseline data for staging in case a pull request messed with +# the database. + +set -e + +: ${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 + +dropdb -h localhost -U "$DB_USER" "$DB_DATABASE" --if-exists +createdb -h localhost -U "$DB_USER" "$DB_DATABASE" + +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..89225eb7fd --- /dev/null +++ b/script/ci/save-staging-baseline.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Save baseline data for staging which can be restored in case a pull request +# messes with the database. + +set -e + +: ${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." From 3f353690c789cc386f49f48d87750200135a638f Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 29 Oct 2024 12:44:46 +1100 Subject: [PATCH 3/3] Load staging baseline even if db in use --- script/ci/restore-staging-baseline.sh | 25 +++++++++++++++++++++++-- script/ci/save-staging-baseline.sh | 1 + 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/script/ci/restore-staging-baseline.sh b/script/ci/restore-staging-baseline.sh index c2945b86cf..71e43ae445 100755 --- a/script/ci/restore-staging-baseline.sh +++ b/script/ci/restore-staging-baseline.sh @@ -5,6 +5,7 @@ set -e +# Load default values if not already set. : ${DB_USER='ofn_user'} : ${DB_DATABASE='openfoodnetwork'} @@ -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 < 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 index 89225eb7fd..076970d499 100755 --- a/script/ci/save-staging-baseline.sh +++ b/script/ci/save-staging-baseline.sh @@ -5,6 +5,7 @@ set -e +# Load default values if not already set. : ${DB_USER='ofn_user'} : ${DB_DATABASE='openfoodnetwork'}