Skip to content

fix(init): quote role and database names in the generated SQL - #124

Open
jayhemnani9910 wants to merge 1 commit into
pgrundev:mainfrom
jayhemnani9910:fix/init-quote-identifiers
Open

jayhemnani9910 wants to merge 1 commit into
pgrundev:mainfrom
jayhemnani9910:fix/init-quote-identifiers

Conversation

@jayhemnani9910

Copy link
Copy Markdown

What and why

pgbot init writes the role and database names into its SQL as they are. A hyphen, a space or an uppercase letter makes the output fail in psql or name a different object. With --role Mon-Role --database "My-App DB":

CREATE ROLE Mon-Role LOGIN PASSWORD '...';
GRANT CONNECT ON DATABASE My-App DB TO Mon-Role;
ERROR:  syntax error at or near "-"

The same happens when the database name is detected from the connection string (postgres://.../My-App%20DB), and for the pg_read_binary_file grant that --logs and the pgbot logs permission hint print.

conn.QuoteIdent double-quotes a name unless it is a plain lowercase identifier, the same rule internal/advisor uses for its CREATE INDEX suggestions, so pgbot_ro and yourdb print unchanged. initSQL and pglog.ReadGrantSQL use it. The DATABASE_URL hint at the end escapes the role and database for a URL (my-app%20db).

Checked against Postgres 17: pgbot init --role Mon-Role --database "My-App DB" --logs | psql fails with the syntax error above on main and creates the role with pg_monitor and CONNECT on that database with this branch. TestInitSQLQuotesNames (cmd/pgbot) and TestQuoteIdent (internal/conn) are new; the first fails on main.

Checklist

  • scripts/gate.sh passes (builds HEAD, not just the working tree)
  • New SQL is read-only; no EXPLAIN ANALYZE; findings stay deterministic (computed in Go)
  • No PII enters a model.Context / --json / the store
  • --json change is additive, or model.SchemaVersion bumped + schema regenerated (go run ./tools/schemagen) (no --json change)
  • A new finding has a docs/findings/<id>.md page + catalog entry (no new finding)

Copilot AI balanced review requested due to automatic review settings October 19, 2026 10:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

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.

2 participants