test(postgres-hooks): retarget hectic bundle tests to migrator init + db-tool hydrate
Move postgres-init-hectic-inheritance test (13 cases) to migrator/init-hectic-bundle since the bundle is now applied by `migrator init` instead of `postgres-init`. Drop init-migrator-with-inherits since `--inherits` is now a deprecation warning, not an error. Add db-tool hydrate-hook test (5 cases) covering --no-hook skip, default apply, idempotency, and HECTIC_DOTENV_FILE. Augment init-migrator with hectic.version and hectic.secret table assertions.
This commit is contained in:
72
test/package/db-tool/test/postgresql/hydrate-hook/run.sh
Normal file
72
test/package/db-tool/test/postgresql/hydrate-hook/run.sh
Normal file
@@ -0,0 +1,72 @@
|
||||
# shellcheck shell=dash
|
||||
|
||||
HECTIC_NAMESPACE=test-db-tool-hydrate-hook
|
||||
|
||||
PG_WORKING_DIR=$(mktemp -d)
|
||||
export PG_WORKING_DIR PG_DATABASE=testdb PG_PORT=5432 PG_SHARED_PRELOAD_LIBRARIES=''
|
||||
|
||||
cleanup() {
|
||||
postgres-cleanup >/dev/null 2>&1 || :
|
||||
rm -rf "$PG_WORKING_DIR" "$LOCAL_DIR"
|
||||
}
|
||||
trap 'cleanup' EXIT INT TERM
|
||||
|
||||
LOCAL_DIR=$(mktemp -d)
|
||||
export LOCAL_DIR
|
||||
|
||||
mkdir -p "${LOCAL_DIR}/db/src"
|
||||
: > "${LOCAL_DIR}/db/src/entrypoint.sql"
|
||||
|
||||
if ! postgres-init; then
|
||||
log error "postgres-init failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PGURL="postgresql://$(id -un)@/testdb?host=${PG_WORKING_DIR}/sock&port=5432"
|
||||
export PGURL
|
||||
|
||||
count_hectic_objects() {
|
||||
psql "$PGURL" -v ON_ERROR_STOP=1 -tAc \
|
||||
"SELECT count(*) FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='hectic';"
|
||||
}
|
||||
|
||||
log notice "case 1: postgres-init alone does not create hectic schema"
|
||||
got=$(psql "$PGURL" -v ON_ERROR_STOP=1 -tAc "SELECT count(*) FROM pg_namespace WHERE nspname='hectic';") || exit 1
|
||||
[ "$got" = 0 ] || { log error "hectic schema must not exist after pure postgres-init (got: $got)"; exit 1; }
|
||||
|
||||
log notice "case 2: hydrate --no-hook does not apply bundle"
|
||||
if ! database hydrate --no-hook; then
|
||||
log error "database hydrate --no-hook failed"
|
||||
exit 1
|
||||
fi
|
||||
got=$(psql "$PGURL" -v ON_ERROR_STOP=1 -tAc "SELECT count(*) FROM pg_namespace WHERE nspname='hectic';") || exit 1
|
||||
[ "$got" = 0 ] || { log error "hectic schema unexpectedly created with --no-hook (got: $got)"; exit 1; }
|
||||
|
||||
log notice "case 3: hydrate (default) applies bundle"
|
||||
if ! database hydrate; then
|
||||
log error "database hydrate failed"
|
||||
exit 1
|
||||
fi
|
||||
got=$(count_hectic_objects) || exit 1
|
||||
[ "$got" -ge 6 ] || { log error "expected >=6 hectic objects after hydrate, got: $got"; exit 1; }
|
||||
|
||||
got=$(psql "$PGURL" -v ON_ERROR_STOP=1 -tAc "SELECT count(*) FROM hectic.version;") || exit 1
|
||||
[ "$got" = 1 ] || { log error "hectic.version row missing (got: $got)"; exit 1; }
|
||||
|
||||
log notice "case 4: hydrate is idempotent"
|
||||
if ! database hydrate; then
|
||||
log error "second database hydrate failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log notice "case 5: HECTIC_DOTENV_FILE is honored"
|
||||
dotenv_file=$(mktemp)
|
||||
printf 'TEST_SECRET=hello-world\n' > "$dotenv_file"
|
||||
HECTIC_DOTENV_FILE="$dotenv_file" database hydrate || {
|
||||
log error "hydrate with HECTIC_DOTENV_FILE failed"
|
||||
rm -f "$dotenv_file"
|
||||
exit 1
|
||||
}
|
||||
rm -f "$dotenv_file"
|
||||
|
||||
log notice "test passed"
|
||||
@@ -1,143 +0,0 @@
|
||||
# shellcheck shell=dash
|
||||
|
||||
HECTIC_NAMESPACE=test-db-tool-hectic-inheritance
|
||||
|
||||
PG_WORKING_DIR=$(mktemp -d)
|
||||
export PG_WORKING_DIR PG_DATABASE=testdb PG_PORT=5432 PG_SHARED_PRELOAD_LIBRARIES=''
|
||||
export PG_HECTIC_INHERITANCE=1
|
||||
|
||||
cleanup() {
|
||||
postgres-cleanup >/dev/null 2>&1 || :
|
||||
rm -rf "$PG_WORKING_DIR"
|
||||
}
|
||||
trap 'cleanup' EXIT INT TERM
|
||||
|
||||
if ! postgres-init; then
|
||||
log error "postgres-init with PG_HECTIC_INHERITANCE=1 failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
sockdir="$PG_WORKING_DIR/sock"
|
||||
user=$(id -un)
|
||||
pgurl="postgresql://${user}@/testdb?host=${sockdir}&port=5432"
|
||||
|
||||
run_sql() {
|
||||
psql "$pgurl" -v ON_ERROR_STOP=1 -tAc "$1"
|
||||
}
|
||||
|
||||
run_sql_expect_fail() {
|
||||
if psql "$pgurl" -v ON_ERROR_STOP=1 -c "$1" >/dev/null 2>&1; then
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
log notice "case 1: hectic schema and parent tables exist"
|
||||
got=$(run_sql "SELECT count(*) FROM pg_namespace WHERE nspname='hectic';") || exit 1
|
||||
[ "$got" = 1 ] || { log error "hectic schema missing"; exit 1; }
|
||||
got=$(run_sql "SELECT count(*) FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname='hectic' AND c.relname IN ('created_at','updated_at','immutable');") || exit 1
|
||||
[ "$got" = 3 ] || { log error "parent tables missing"; exit 1; }
|
||||
|
||||
log notice "case 2: CREATE TABLE without inheritance is rejected"
|
||||
if ! run_sql_expect_fail 'CREATE TABLE public.bad_table (id int);'; then
|
||||
log error "non-inheriting CREATE TABLE was accepted"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log notice "case 3: CREATE TABLE inheriting hectic.created_at is accepted"
|
||||
run_sql 'CREATE TABLE public.good_table (id int) INHERITS ("hectic"."created_at");' || exit 1
|
||||
|
||||
log notice "case 4: tables inheriting hectic.updated_at get auto BEFORE UPDATE trigger"
|
||||
run_sql 'CREATE TABLE public.with_updated (id int, val text) INHERITS ("hectic"."created_at", "hectic"."updated_at");' || exit 1
|
||||
got=$(run_sql "SELECT count(*) FROM pg_trigger WHERE tgrelid='public.with_updated'::regclass AND tgname='hectic_set_updated_at' AND NOT tgisinternal;") || exit 1
|
||||
[ "$got" = 1 ] || { log error "auto updated_at trigger missing"; exit 1; }
|
||||
|
||||
run_sql "INSERT INTO public.with_updated (id, val) VALUES (1, 'a');" || exit 1
|
||||
sleep 1
|
||||
run_sql "UPDATE public.with_updated SET val='b' WHERE id=1;" || exit 1
|
||||
got=$(run_sql "SELECT (updated_at > created_at)::int FROM public.with_updated WHERE id=1;") || exit 1
|
||||
[ "$got" = 1 ] || { log error "updated_at not bumped on UPDATE (got: $got)"; exit 1; }
|
||||
|
||||
log notice "case 5: GUC hectic.inheritance_extra_excluded_schemas exempts schemas"
|
||||
run_sql 'CREATE SCHEMA legacy;' || exit 1
|
||||
if ! run_sql_expect_fail 'CREATE TABLE legacy.t1 (id int);'; then
|
||||
log error "legacy.t1 should be rejected before GUC set"
|
||||
exit 1
|
||||
fi
|
||||
run_sql "ALTER DATABASE testdb SET hectic.inheritance_extra_excluded_schemas = 'legacy';" || exit 1
|
||||
psql "$pgurl" -v ON_ERROR_STOP=1 -c 'CREATE TABLE legacy.t1 (id int);' || {
|
||||
log error "legacy.t1 rejected even after GUC exclusion"
|
||||
exit 1
|
||||
}
|
||||
|
||||
log notice "case 6: declarative partitions are exempt"
|
||||
run_sql 'CREATE TABLE public.parted (id int, region text) PARTITION BY LIST (region) INHERITS ("hectic"."created_at");' && {
|
||||
log error "PARTITION BY combined with INHERITS unexpectedly succeeded"
|
||||
exit 1
|
||||
} || :
|
||||
run_sql 'CREATE TABLE public.events (id int, region text, created_at timestamptz NOT NULL DEFAULT NOW()) PARTITION BY LIST (region);' && {
|
||||
log error "partitioned parent without inheritance unexpectedly succeeded"
|
||||
exit 1
|
||||
} || :
|
||||
run_sql "ALTER DATABASE testdb SET hectic.inheritance_extra_excluded_schemas = 'legacy,parts';" || exit 1
|
||||
run_sql 'CREATE SCHEMA parts;' || exit 1
|
||||
psql "$pgurl" -v ON_ERROR_STOP=1 -c 'CREATE TABLE parts.events (id int, region text) PARTITION BY LIST (region);' || {
|
||||
log error "partitioned parent in excluded schema rejected"
|
||||
exit 1
|
||||
}
|
||||
psql "$pgurl" -v ON_ERROR_STOP=1 -c "CREATE TABLE parts.events_us PARTITION OF parts.events FOR VALUES IN ('us');" || {
|
||||
log error "declarative partition was rejected (should be exempt via relispartition)"
|
||||
exit 1
|
||||
}
|
||||
|
||||
log notice "case 7: hectic.immutable inheritors are blocked from DML outside migration_mode"
|
||||
run_sql 'CREATE TABLE public.frozen (id int, label text) INHERITS ("hectic"."created_at", "hectic"."immutable");' || exit 1
|
||||
|
||||
got=$(run_sql "SELECT count(*) FROM pg_trigger WHERE tgrelid='public.frozen'::regclass AND tgname IN ('hectic_block_immutable_dml','hectic_block_immutable_truncate') AND NOT tgisinternal;") || exit 1
|
||||
[ "$got" = 2 ] || { log error "immutable triggers missing on public.frozen (got: $got)"; exit 1; }
|
||||
|
||||
if ! run_sql_expect_fail "INSERT INTO public.frozen (id, label) VALUES (1, 'x');"; then
|
||||
log error "INSERT on immutable table accepted outside migration_mode"
|
||||
exit 1
|
||||
fi
|
||||
if ! run_sql_expect_fail "UPDATE public.frozen SET label='y' WHERE id=1;"; then
|
||||
log error "UPDATE on immutable table accepted outside migration_mode"
|
||||
exit 1
|
||||
fi
|
||||
if ! run_sql_expect_fail "DELETE FROM public.frozen WHERE id=1;"; then
|
||||
log error "DELETE on immutable table accepted outside migration_mode"
|
||||
exit 1
|
||||
fi
|
||||
if ! run_sql_expect_fail "TRUNCATE public.frozen;"; then
|
||||
log error "TRUNCATE on immutable table accepted outside migration_mode"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log notice "case 8: SET LOCAL hectic.migration_mode='on' allows DML"
|
||||
psql "$pgurl" -v ON_ERROR_STOP=1 <<'SQL' || { log error "migration_mode tx failed"; exit 1; }
|
||||
BEGIN;
|
||||
SET LOCAL hectic.migration_mode = 'on';
|
||||
INSERT INTO public.frozen (id, label) VALUES (1, 'x');
|
||||
UPDATE public.frozen SET label = 'y' WHERE id = 1;
|
||||
COMMIT;
|
||||
SQL
|
||||
got=$(run_sql "SELECT label FROM public.frozen WHERE id=1;") || exit 1
|
||||
[ "$got" = y ] || { log error "expected label=y after migration tx, got: $got"; exit 1; }
|
||||
|
||||
log notice "case 9: GUC does not leak past COMMIT"
|
||||
if ! run_sql_expect_fail "INSERT INTO public.frozen (id, label) VALUES (2, 'z');"; then
|
||||
log error "INSERT accepted after migration_mode tx committed (GUC leaked)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log notice "case 10: TRUNCATE allowed under migration_mode"
|
||||
psql "$pgurl" -v ON_ERROR_STOP=1 <<'SQL' || { log error "truncate under migration_mode failed"; exit 1; }
|
||||
BEGIN;
|
||||
SET LOCAL hectic.migration_mode = 'on';
|
||||
TRUNCATE public.frozen;
|
||||
COMMIT;
|
||||
SQL
|
||||
got=$(run_sql "SELECT count(*) FROM public.frozen;") || exit 1
|
||||
[ "$got" = 0 ] || { log error "frozen not truncated"; exit 1; }
|
||||
|
||||
log notice "test passed"
|
||||
Reference in New Issue
Block a user