mirror of
https://github.com/gadgethd/ukmesh.git
synced 2026-12-06 11:14:26 +00:00
32 lines
1.5 KiB
Bash
32 lines
1.5 KiB
Bash
#!/usr/bin/env bash
|
|
# Exporter stays in its read-only transaction until the dump reader finishes.
|
|
# A subshell owns both pipe descriptors, including on SQL or pg_dump failure.
|
|
dump_postgres_snapshot() (
|
|
set -euo pipefail
|
|
local container=$1 db_user=$2 database=$3 sql_file=$4 dump_file=$5 baseline_file=$6
|
|
local validator=$7 snapshot baseline
|
|
coproc EXPORTER { docker exec -i "$container" psql -XqAt -v ON_ERROR_STOP=1 -U "$db_user" -d "$database"; }
|
|
local exporter_pid=$EXPORTER_PID input=${EXPORTER[1]} output=${EXPORTER[0]}
|
|
printf '%s\n' 'BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY;' \
|
|
"SET LOCAL timezone = 'UTC'; SET LOCAL datestyle = 'ISO, YMD'; SET LOCAL search_path = pg_catalog, mesh, core, public;" \
|
|
'SELECT pg_export_snapshot();' >&"$input"
|
|
cat "$sql_file" >&"$input"
|
|
IFS= read -r snapshot <&"$output"
|
|
IFS= read -r baseline <&"$output"
|
|
printf '%s\n' "$baseline" | jq -ce "$validator" > "$baseline_file"
|
|
docker exec "$container" pg_dump -U "$db_user" -d "$database" --format=custom \
|
|
--compress=6 --no-owner --snapshot="$snapshot" > "$dump_file"
|
|
printf '%s\n' 'ROLLBACK;' '\q' >&"$input"
|
|
wait "$exporter_pid"
|
|
)
|
|
|
|
dump_analytics_snapshot() {
|
|
dump_postgres_snapshot "$@" 'select(.packets >= 0 and .nodes >= 0)'
|
|
}
|
|
|
|
read_analytics_baseline() {
|
|
local container=$1 db_user=$2 database=$3 sql_file=$4
|
|
docker exec -i -e "PGOPTIONS=-c timezone=UTC -c datestyle=ISO,YMD -c search_path=pg_catalog,mesh,core,public" \
|
|
"$container" psql -XqAt -v ON_ERROR_STOP=1 -U "$db_user" -d "$database" < "$sql_file"
|
|
}
|