Skip to content

query-db: replace psql -c with -f tmpfile to avoid Windows cmd.exe newline truncation #10

@Zhiying-Li-dot

Description

@Zhiying-Li-dot

Follow-up from PR #8

bin/helpers/query-db.ts:230 still uses the psql -c "<sql>" pattern that PR #8 replaced in db-utils.ts:

const result = execSync(
  `"${psqlPath}" -h ${host} -p ${port} -U ${user} -d ${database} -c "${sql}"`,
  ...
);

Why this wasn't fixed in PR #8

optima-query-db only takes single-statement SQL from argv (the CLI surface), so it doesn't hit the multi-line newline-truncation that broke grant-balance silently. Scope was tightened to keep PR #8 focused on the breaking bug.

Why it should still be fixed

  1. Consistency — three helpers (db-utils, query-db, show-env) all touch shell escaping; aligning them on the same execFileSync(psql, argv) + tmpfile pattern removes a class of foot-guns.
  2. Latent risk — if someone later pastes a multi-line query (e.g. CTE with comments) into optima-query-db, it would partially execute on Windows the same way grant-balance did.
  3. ON_ERROR_STOP=1 — same reason as PR fix: multiple Windows shell-compat bugs in dev-skills CLI #8: psql with -c swallows non-fatal errors and still returns 0.

Proposed fix

Mirror the queryDB rewrite from PR #8 commit 7ce79bf:

  • Write SQL to a temp file via fs.writeFileSync + os.tmpdir()
  • Invoke with execFileSync(psql, ['-h', ..., '-v', 'ON_ERROR_STOP=1', '-f', tmpFile], {...})
  • Clean up the temp file in a finally block

Estimated effort: 5 min + smoke test.

Refs: PR #8, commit 7ce79bf

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions