Skip to content

Commit

Permalink
feat(ci): Throw error on sync script failure, wrap in single transact…
Browse files Browse the repository at this point in the history
…ion (#2177)
  • Loading branch information
DafyddLlyr authored Aug 30, 2023
1 parent 0ffb3d6 commit 7eb67bf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
19 changes: 18 additions & 1 deletion .github/workflows/sync-staging-db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ jobs:
with:
files: ".env, .env.staging, api.planx.uk/.env.test, hasura.planx.uk/.env.test"
fail: true
- run: docker run --rm -v "$(pwd)/scripts/seed-database:/app" --workdir="/app" postgis/postgis:12-3.0-alpine "./container.sh" "${STAGING_PG_URL}" "${PRODUCTION_PG_URL_FOR_USER_GITHUB_ACTIONS}" "reset_flows" "include_published_flows"
- name: Run sync script
run: |
{
docker run --rm -v "$(pwd)/scripts/seed-database:/app" --workdir="/app" postgis/postgis:12-3.0-alpine "./container.sh" "${STAGING_PG_URL}" "${PRODUCTION_PG_URL_FOR_USER_GITHUB_ACTIONS}" "reset_flows"
} || {
echo "Database sync failed!"
exit 1
}
env:
PRODUCTION_PG_URL_FOR_USER_GITHUB_ACTIONS: ${{ secrets.PRODUCTION_PG_URL_FOR_USER_GITHUB_ACTIONS }}
- name: Slack Notification
uses: rtCamp/action-slack-notify@v2
if: failure()
env:
SLACK_CHANNEL: planx-notifications-internal
SLACK_USERNAME: GitHub Actions
SLACK_COLOR: "#FF5733"
SLACK_TITLE: Staging sync failure
SLACK_MESSAGE: Failed to sync the staging database with production
SLACK_WEBHOOK: ${{ secrets.SLACK_DEPLOYMENT_WEBHOOK }}
2 changes: 1 addition & 1 deletion docker-compose.seed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
- "./scripts/seed-database:/app"
working_dir: "/app"
entrypoint: "./container.sh postgres://${PG_USERNAME}:${PG_PASSWORD}@postgres/${PG_DATABASE} ${PRODUCTION_PG_URL_FOR_USER_GITHUB_ACTIONS}"
command: "no_reset include_published_flows"
command: "no_reset"
restart: "no"
depends_on:
hasura-proxy:
Expand Down
27 changes: 12 additions & 15 deletions scripts/seed-database/container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ REMOTE_PG=${2}
# RESET set to "reset_all" will truncate all synced tables
RESET=${3}

# INCLUDE_PUBLISHED_FLOWS set to "include_published_flows" will sync published flows
INCLUDE_PUBLISHED_FLOWS=${4}

echo downloading data from production

# set-up tmp dir for remote data
Expand All @@ -26,27 +23,27 @@ for table in "${tables[@]}"; do
read_cmd="\\copy (SELECT * FROM ${table}) TO ${target} WITH (FORMAT csv, DELIMITER ';');"
psql --quiet ${REMOTE_PG} --command="${read_cmd}"
echo ${table} downloaded

if [[ ${RESET} == "reset_all" ]]; then
reset_cmd="TRUNCATE TABLE ${table} CASCADE;"
psql ${LOCAL_PG} --command="${reset_cmd}"

# Start building single sync.sql file which will be executed in a single transaction
cat $reset_cmd > sync.sql
fi
done

if [[ ${RESET} == "reset_flows" ]]; then
psql ${LOCAL_PG} --command="TRUNCATE TABLE flows CASCADE;"
fi
psql --quiet ${REMOTE_PG} --command="\\copy (SELECT DISTINCT ON (flow_id) id, data, flow_id, summary, publisher_id, created_at FROM published_flows ORDER BY flow_id, created_at DESC) TO '/tmp/published_flows.csv' (FORMAT csv, DELIMITER ';');"
echo published_flows downloaded

if [[ ${INCLUDE_PUBLISHED_FLOWS} == "include_published_flows" ]]; then
psql --quiet ${REMOTE_PG} --command="\\copy (SELECT DISTINCT ON (flow_id) id, data, flow_id, summary, publisher_id, created_at FROM published_flows ORDER BY flow_id, created_at DESC) TO '/tmp/published_flows.csv' (FORMAT csv, DELIMITER ';');"
echo published_flows downloaded
if [[ ${RESET} == "reset_flows" ]]; then
cat write/truncate_flows.sql > sync.sql
fi

echo beginning write transaction
psql --quiet ${LOCAL_PG} -f write/main.sql
# Add main operations
cat write/main.sql > sync.sql

if [[ ${INCLUDE_PUBLISHED_FLOWS} == "include_published_flows" ]]; then
psql --quiet ${LOCAL_PG} -f write/published_flows.sql
fi
echo "Beginning write transaction..."
psql --quiet ${LOCAL_PG} -f sync.sql --single-transaction -v ON_ERROR_STOP=on

# clean-up tmp dir
rm -rf /tmp
Expand Down
3 changes: 1 addition & 2 deletions scripts/seed-database/write/main.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
BEGIN;
\include write/users.sql
\include write/teams.sql
\include write/flows.sql
\include write/flow_document_templates.sql
COMMIT;
\include write/published_flows.sql
1 change: 1 addition & 0 deletions scripts/seed-database/write/truncate_flows.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TRUNCATE TABLE flows CASCADE;

0 comments on commit 7eb67bf

Please sign in to comment.