fix(rest-postgres): idempotent DB init so a partial init can't silently drop databases#2623
fix(rest-postgres): idempotent DB init so a partial init can't silently drop databases#2623kirson-git wants to merge 1 commit into
Conversation
…ly drop databases init-configmap.yaml runs once via docker-entrypoint-initdb.d under ON_ERROR_STOP. If any statement errors, psql aborts and the remaining CREATE DATABASE/USER statements are skipped, leaving a partial state (e.g. nico+keycloak created but temporal/temporal_visibility missing). Postgres only runs init scripts on an empty data dir, so it never re-runs -- the missing databases are silent and permanent, and the Temporal install later fails with 'failed pre-install: timed out' / FATAL: role "temporal" does not exist. Make each database/role creation idempotent (\gexec existence guard for databases, DO-block for roles) so the script is safe to re-run and a single benign error no longer skips later databases. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: ekirson <ekirson@nvidia.com>
Summary by CodeRabbitRelease Notes
WalkthroughThe PostgreSQL initialization ConfigMap SQL is rewritten to be idempotent across all four managed databases ( ChangesIdempotent PostgreSQL Init Script
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
rest-api/deploy/kustomize/base/postgres/init-configmap.yaml (1)
46-53:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winMissing
\c postgresat end of temporal_visibility section breaks consistency.All preceding sections (nico, keycloak, temporal) conclude with
\c postgresto restore the connection context to the default database. This section omits that epilogue, leaving the script terminated while connected totemporal_visibility.While functionally benign for the current script (as this is the final section), the inconsistency introduces a latent defect: any future extension to this script would inherit an unexpected connection context, potentially causing silent misbehavior.
Suggested fix
GRANT ALL ON SCHEMA public TO temporal; ALTER SCHEMA public OWNER TO temporal; + \c postgres🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@rest-api/deploy/kustomize/base/postgres/init-configmap.yaml` around lines 46 - 53, The temporal_visibility section in the init-configmap.yaml file is missing a `\c postgres` statement at the end to restore the connection context to the default database, inconsistent with the preceding sections (nico, keycloak, temporal) which all conclude with this epilogue. Add `\c postgres` after the `ALTER SCHEMA public OWNER TO temporal;` line in the temporal_visibility section to maintain consistency and prevent connection context issues if the script is extended in the future.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@rest-api/deploy/kustomize/base/postgres/init-configmap.yaml`:
- Around line 46-53: The temporal_visibility section in the init-configmap.yaml
file is missing a `\c postgres` statement at the end to restore the connection
context to the default database, inconsistent with the preceding sections (nico,
keycloak, temporal) which all conclude with this epilogue. Add `\c postgres`
after the `ALTER SCHEMA public OWNER TO temporal;` line in the
temporal_visibility section to maintain consistency and prevent connection
context issues if the script is extended in the future.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 3a1aefe3-e33f-4572-af3e-a065ed3ff2c6
📒 Files selected for processing (1)
rest-api/deploy/kustomize/base/postgres/init-configmap.yaml
Problem
rest-api/deploy/kustomize/base/postgres/init-configmap.yamlruns once viadocker-entrypoint-initdb.dunderpsql -v ON_ERROR_STOP=1. If any statement errors, psql aborts and the remainingCREATE DATABASE/CREATE USERstatements are skipped, leaving a partial state — e.g.nico+keycloakcreated buttemporal/temporal_visibilitymissing.Because PostgreSQL only runs init scripts on an empty data dir, the script never re-runs, so the missing databases are silent and permanent. The Temporal install then fails its pre-install hook:
Reproduced during a clean v0.10.3 bring-up.
Fix
Make every database/role creation idempotent:
SELECT … WHERE NOT EXISTS (…)\gexecguard forCREATE DATABASEDO $$ … IF NOT EXISTS … $$for rolesThe script is now safe to re-run, and a single benign "already exists" error no longer aborts it and skips later databases.
Test
Re-applied against a partially-initialized postgres (had only nico+keycloak): all four DBs (
nico,keycloak,temporal,temporal_visibility) present; Temporal install proceeds.