Skip to content

init-runtime-role.sh: pipe the owner guard's query to psql, not -c - #103

Merged
brettheap merged 1 commit into
mainfrom
fix/init-runtime-role-psql-variable
Nov 17, 2026
Merged

brettheap merged 1 commit into
mainfrom
fix/init-runtime-role-psql-variable

Conversation

@brettheap

@brettheap brettheap commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What was wrong

The migration-owner guard in deploy/compose/init-runtime-role.sh and its byte-identical copy deploy/kubernetes/base/init-runtime-role.sh passed a psql variable inside psql -c:

psql ... -v owner="$migration_owner" -c "select 1 from pg_roles where rolname = :'owner'"

psql 16 hands a -c string to the server as written and interpolates :'owner' only in what it reads as input (stdin or -f). The server received a literal colon and answered syntax error at or near ":". The if ! around the pipeline read that as "no such role", so on every fresh data volume the script exited 1 with a refusal that named an owner that existed. restart: unless-stopped started the container again, the entrypoint skipped initialization on the now non-empty data directory, and PostgreSQL came up healthy without the opendox_runtime role.

This was introduced with #25 and is unchanged in v0.1.0. The reproduction and diagnosis are in #102.

The fix

The query now reaches psql on stdin, where :'owner' is interpolated:

-if ! psql -tAqX --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" \
-      -v owner="$migration_owner" \
-      -c "select 1 from pg_roles where rolname = :'owner'" | grep -q '^1$'; then
+if ! printf '%s\n' "select 1 from pg_roles where rolname = :'owner'" |
+     psql -tAqX --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" \
+          -v owner="$migration_owner" | grep -q '^1$'; then

Both copies change identically and stay byte-identical (test_the_two_copies_of_the_role_bootstrap_are_byte_identical holds that). A short comment above the guard says why it is not -c, so a later "simplification" does not put it back.

Draft PR #99 edits other hunks of these same two files (the header comment and the table lists, not these lines). This diff touches only the guard and the comment beside it. A trial merge of this branch with #99's head merges cleanly, so the two can land in either order.

Tests

New file tests_runtime/test_runtime_role_bootstrap.py (11 cases). tests_runtime/test_deploy_shape.py needed no change: none of its assertions pinned the old guard text, and its 111 cases pass as they are.

DB-backed, and they run the script (test_the_first_start_creates_the_runtime_role, test_a_migration_owner_the_database_lacks_is_refused_by_name), each over both copies, so 4 cases. Each creates a scratch database on the suite's OPENDOX_TEST_DATABASE_URL server, runs the unmodified script against it with psql, then asks the catalogue:

  • the first start exits 0, and the runtime role exists, can log in and is not a superuser, with CONNECT, schema USAGE and the owner's default SELECT/INSERT/UPDATE/DELETE privileges;
  • an owner the database lacks is still refused by name with exit 1 and no role created, and the output carries no PostgreSQL syntax error (the old guard printed that same refusal for every owner, behind one).

The scratch database and role are dropped afterwards. The psql is the one the local extra's bundled server ships (PostgreSQL 16, installed by .[runtime,test], which validate installs), with PATH's psql only as a fallback. The server is the validate job's own postgres:16 service, so this is a faithful run in CI even though that job does not mount the script. What it does not exercise is the image's entrypoint mechanics (docker-entrypoint-initdb.d, the temporary unix-socket server); the Docker run below covers those once, by hand.

Hermetic (7 cases, no server): no psql -c argument in either copy holds a psql variable reference, and the check itself is held to the old three-line form, because a check that cannot fail checks nothing. These keep guarding the class for a developer with no PostgreSQL.

Skip count. A developer without Postgres or psql gets a skip with the reason printed. Under CI the same absence is a failure, as conftest._skip_or_fail does for the rest of the DB-backed suite, so these cases cannot add a skip and EXPECT_SKIPPED stays 11. The 11 new cases only raise SELECTED and PASSED, which the floors permit; validate.yml is not touched.

How it was verified

In postgres:16 containers, with the script supplied the way compose supplies it (OPENDOX_RUNTIME_PG_USER=opendox_runtime, OPENDOX_MIGRATION_PG_USER empty, OPENDOX_PG_SCHEMA=public):

  • Before the fix: the container exits 1 on first start with ERROR: syntax error at or near ":" / LINE 1: select 1 from pg_roles where rolname = :'owner', then the refusal naming 'opendox'. This reproduces init-runtime-role.sh aborts every first start: psql -c does not expand :'owner' #102.
  • After the fix: first start completes ("PostgreSQL init process complete; ready for start up"). opendox_runtime exists (rolcanlogin true, not a superuser), logs in with its password, has CONNECT and schema USAGE, and pg_default_acl holds {opendox_runtime=arwd/opendox}. After a container restart, pg_isready is accepting and the role is still there.
  • A missing owner (OPENDOX_MIGRATION_PG_USER=nosuchowner): exit 1 with the intended refusal naming it, and no syntax error.
  • sh -n and shellcheck -s sh are clean on the fixed script.

Tests, against a postgres:16 server:

  • python -m pytest tests_runtime/test_runtime_role_bootstrap.py: 11 passed.
  • The same file with both scripts reverted to their pre-fix text: 6 failed (both first-start cases, both missing-owner cases, both hermetic variable checks) and 5 passed (the check's own self-test cases, which read no script).
  • python -m pytest tests_runtime/test_deploy_shape.py: 111 passed.
  • The whole suite as CI runs it (python -m pytest -q, .[runtime,test] installed under the repository's constraints file): 5921 selected, 5897 passed and 20 skipped (the skips are the test venv's missing pip and tools this machine lacks: an OMP harness and the model-provider broker binary). Four cases failed or errored for reasons of this machine's own environment: the venv had no pip (three wheel-building cases) and the shell had no LANG set (one environment-allowlist case). With pip installed and LANG=C.UTF-8, all four pass. None involves this change.

The Kubernetes copy is byte-identical to the compose copy and was run through the same tests, but nothing here ran on a cluster.

Fixes #102

🤖 Generated with Claude Code

Summary by Sourcery

Fix runtime-role initialization by ensuring psql interpolates the migration-owner variable before executing the guard query.

Bug Fixes:

  • Fix the runtime-role bootstrap guard so existing migration owners are recognized and fresh database initialization completes successfully.

Enhancements:

  • Document the requirement to feed the owner query to psql through standard input rather than a command string.
  • Keep the Compose and Kubernetes bootstrap scripts synchronized.

Tests:

  • Add database-backed and hermetic regression coverage for both bootstrap script copies, including valid owner creation and missing-owner rejection.

…ixes #102)

The migration-owner guard passed `:'owner'` inside `psql -c`. psql 16 sends a
-c string to the server as written and interpolates a variable only in what it
reads as input, so the server got a literal colon and answered a syntax error.
The `if !` read that as "no such role", so every first start on a fresh data
volume exited 1 with a refusal that blamed an owner which existed. The next
start skipped initialization and PostgreSQL came up healthy without the
runtime role.

The query now goes in on stdin, where `:'owner'` is interpolated. Both copies
of the script change identically and stay byte-identical. Nothing else in
either file moves, so the open edits to the same files do not conflict.

tests_runtime/test_runtime_role_bootstrap.py is new. Its DB-backed cases run
each copy of the script against a real PostgreSQL 16 and ask the catalogue
whether the runtime role, its CONNECT and USAGE grants and its default
privileges are there, and that a migration owner the database lacks is still
refused by name without a syntax error. Its hermetic cases hold that no
`psql -c` argument carries a psql variable. The existing shape cases
(test_deploy_shape.py) needed no change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Nov 17, 2026

Copy link
Copy Markdown

Sorry @brettheap, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 5 days and 3 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@brettheap

Copy link
Copy Markdown
Contributor Author

@codex review

@sourcery-ai

sourcery-ai Bot commented Nov 17, 2026

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Change the migration-owner guard to send its variable-bearing query to psql on stdin, and document why it must not use -c.

  • deploy/compose/init-runtime-role.sh L80-88
  • deploy/kubernetes/base/init-runtime-role.sh L80-88

Tests

Add hermetic checks against psql-variable use in command strings and PostgreSQL-backed tests of both scripts’ role bootstrap and missing-owner refusal behavior.

  • tests_runtime/test_runtime_role_bootstrap.py L1-355

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-17T00:59:50.731828Z
We couldn’t complete this review. Comment @codex review to try again.
85d0c52 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

init-runtime-role.sh aborts every first start: psql -c does not expand :'owner'

1 participant