Files
aevolution/tools/ci/postgres-check.sh
T

31 lines
1.8 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
root="$(cd "$(dirname "$0")/../.." && pwd)"
prefix="$root/.cache/postgres/install"
export PATH="$prefix/bin:$PATH"
export LD_LIBRARY_PATH="$prefix/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
test_dir="$(mktemp -d /tmp/evo-pg-test.XXXXXXXX)"
chmod 700 "$test_dir"
export PGHOST="$test_dir" PGPORT=55432 PGDATABASE=postgres PGUSER="$(id -un)"
unset PGHOSTADDR PGPASSWORD PGSERVICE PGSERVICEFILE
initdb -D "$test_dir/data" --no-locale --encoding=UTF8 --auth-local=trust --auth-host=reject > "$test_dir/init.log"
trap 'pg_ctl -D "$test_dir/data" -m immediate stop > /dev/null || true' EXIT
pg_ctl -D "$test_dir/data" -l "$test_dir/server.log" -o "-k $test_dir -p 55432 -c listen_addresses=''" -w start
psql -X -v ON_ERROR_STOP=1 -c 'CREATE ROLE evo_runtime_test NOLOGIN'
bash "$root/tools/migrate-postgres.sh"
bash "$root/tools/migrate-postgres.sh" # exact supported upgrade/restart is a no-op
"${1:?path to evo-postgres-tests}"
# Migration failure must not publish a partial schema.
createdb migration_rollback
if (export PGDATABASE=migration_rollback; psql -X -v ON_ERROR_STOP=1 -1 \
-v migration_sha=invalid -f "$root/migrations/0001_region_bootstrap.sql"); then
echo 'Expected failed migration' >&2; exit 1
fi
test "$(PGDATABASE=migration_rollback psql -XAtc "SELECT count(*) FROM pg_namespace WHERE nspname='evolution'")" = 0
# Unknown future versions and checksum drift must not be repaired silently.
psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=2'
if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi
psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=1,sha256=repeat('0',64)"
if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi
echo 'postgres migrations, rollback, constraints and permissions passed'