fix(nexus-db): address codex review — early encoding guard, robust helpers
CI / Shell-Lint (bash -n, source-check, Validierungs-Tests) (pull_request) Successful in 2s

- assert encoding BEFORE role/password mutation on re-run, so an old
  SQL_ASCII DB aborts with no side effects (codex finding 1)
- ensure_utf8_locale_active honours its locale argument consistently in
  match, locale.gen line and export (codex finding 2)
- assert_db_encoding_utf8 uses argv-clean runuser psql with :'db' literal
  binding instead of nested su -c shell; docs keep su - postgres -c
  (codex finding 3)
This commit is contained in:
2026-06-13 14:34:35 +02:00
parent bd4293f53e
commit 6543fd77d8
2 changed files with 26 additions and 11 deletions
+10 -5
View File
@@ -101,6 +101,13 @@ systemctl restart postgresql
# ── role + database + extension (idempotent, password kept on re-run) ─────────
run_psql() { runuser -u postgres -- psql -v ON_ERROR_STOP=1 -qAt "$@"; }
# Re-run safety (issue #8): if the database already exists, verify its
# encoding BEFORE touching roles/passwords. An old SQL_ASCII database must
# abort the run with NO side effects — not after rotating credentials.
if [[ "$(run_psql -c "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'")" == "1" ]]; then
assert_db_encoding_utf8 "$DB_NAME"
fi
if [[ "$(run_psql -c "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER'")" != "1" ]]; then
msg_info "Creating role $DB_USER + database $DB_NAME..."
DB_PASS="$(openssl rand -base64 32 | tr -d '/+=' | head -c 32)"
@@ -126,16 +133,14 @@ if [[ "$(run_psql -c "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'")" != "
run_psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8' TEMPLATE template0"
# Only the owner may connect — no PUBLIC access.
run_psql -c "REVOKE CONNECT ON DATABASE $DB_NAME FROM PUBLIC"
# Verify what we just created (the pre-existing case was already checked
# before the role block, issue #8).
assert_db_encoding_utf8 "$DB_NAME"
msg_ok "Database $DB_NAME created (UTF8, owner $DB_USER, PUBLIC revoked)"
else
msg_warn "Database $DB_NAME already exists, skipping creation"
fi
# Encoding guard (issue #8): catch both a freshly mis-created DB and a
# pre-existing SQL_ASCII database from an old provisioning. Abort before the
# app ever connects — a wrong encoding is DB damage, not a warning.
assert_db_encoding_utf8 "$DB_NAME"
# pgvector: CREATE EXTENSION needs superuser; installed now (per ADR-0002:
# "Extension ab Tag 1 installiert, ungenutzt bis Phase 2").
run_psql -d "$DB_NAME" -c "CREATE EXTENSION IF NOT EXISTS vector" >/dev/null