Skip to content

Exit -c and -f with psql's status codes - #1652

Open
DiegoDAF wants to merge 1 commit into
dbcli:mainfrom
DiegoDAF:upstream/exit-status-like-psql
Open

DiegoDAF wants to merge 1 commit into
dbcli:mainfrom
DiegoDAF:upstream/exit-status-like-psql

Conversation

@DiegoDAF

Copy link
Copy Markdown
Contributor

Description

pgcli -c "select nope" exits 0, so a shell script or a CI job that calls pgcli cannot tell a failed statement from a good one. This makes -c and -f exit the way psql does.

psql's rule, measured with psql 18 against a live server: the exit status is that of the last -c or -f that ran. A failed -c exits 1. A script exits 0 even with errors in it, unless ON_ERROR_STOP is set; then the first failure stops everything after it, and a failed script exits 3. pgcli's on_error = STOP (the default) plays the part of ON_ERROR_STOP, and RESUME plays psql's default:

psql pgcli before pgcli after
-c "select nope" 1 0 1
-c "select 1" -c "select nope", RESUME 1 0, second -c runs 1
-c "select 1" -c "select nope" -c "select 2", RESUME 0, all run 0, select 2 skipped 0, all run
-f err.sql, STOP / ON_ERROR_STOP 3 0 3
-f err.sql -f ok.sql, RESUME 0, both run 0, ok.sql skipped 0, both run
-c "select nope" -f err.sql, STOP / ON_ERROR_STOP 1, file skipped 0, file runs 1, file skipped

The last rows are the same loop's other departures from psql: under RESUME a failure kept the next -c or -f from running, and under STOP a failed -c did not stop the files.

One difference remains and is left alone here: pgcli runs every -c before every -f, while psql follows the order on the command line.

Tests: nine parametrized cases mirroring the measured psql runs, plus two through a real connection (a failed -c exits 1, a failed script exits 3). Ten of the eleven fail on main; the remaining case already matched psql. The two behave scenarios that run an invalid query asserted pgcli exits successfully, which wrote the bug down as expected behaviour (that assertion came with the original -c/-f PRs, mine); they now assert an error exit, and fail without this change.

Checklist

  • I have added this contribution to the changelog.rst.
  • I have added my name to the AUTHORS file (or it is already there).
  • I installed pre-commit hooks (pip install pre-commit && pre-commit install).
  • Please squash merge this pull request (uncheck if you would like us to merge as multiple commits)

On the third box: .pre-commit-config.yaml pins an older ruff-pre-commit than the one tox -e style installs, and the two disagree on formatting, so I ran ruff check and ruff format matching the CI instead, both clean.

pgcli -c "select nope" exited 0, so a script calling pgcli could not
tell a failed statement from a good one. psql uses the status of the
last -c or -f that ran: a failed -c exits 1, a script exits 0 even with
errors in it, and with ON_ERROR_STOP the first failure stops everything
after it and a failed script exits 3. on_error = STOP (the default) now
plays ON_ERROR_STOP and RESUME plays psql's default.

The same loop had two more departures from psql: under RESUME a failed
-c kept the next -c from running, and under STOP it did not stop the -f
files. Both follow psql now.

Nine cases, each measured with psql 18 against a live server, plus two
tests through a real connection; ten of the eleven fail without the
change (the remaining case already matched). The two behave scenarios
that ran an invalid query expected a successful exit; they now expect
an error, and fail without the change.
DiegoDAF added a commit to DiegoDAF/pgcli.daf that referenced this pull request Oct 16, 2026
@DiegoDAF

Copy link
Copy Markdown
Contributor Author

The red builds are test_format_array_output_expanded only, the same in every job: cli_helpers 2.15.2, released today, aligns expanded output by display width, and that test has the old alignment written down. It fails on main too and is fixed in #1653; the other 2804 tests, including the new ones here, pass.

This branch has not been deployed

No deployments
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.

1 participant