Add support for -f/--file option to execute SQL from files - #1543
Add support for -f/--file option to execute SQL from files#1543DiegoDAF wants to merge 3 commits into
Conversation
This commit adds support for the -f/--file option to pgcli, similar to psql's behavior. Users can now execute SQL commands from files and exit immediately after execution. Features: - Single file execution: pgcli -f file.sql - Multiple files: pgcli -f file1.sql -f file2.sql - Long form: pgcli --file file.sql - Files are executed sequentially - Pager is automatically disabled in file mode - Proper error handling and exit codes Tests included for all scenarios. Made with ❤️ and 🤖 Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
j-bennet
left a comment
There was a problem hiding this comment.
This looks ready to merge, however, I'm wondering, what will happen if the file contains a destructive command (DROP table_name) and destructive_warning is enabled?
Thanks for the review! Short answer: Concretely, with
One detail worth calling out: Happy to add a behave scenario covering the destructive-file case if you'd like it |
The style gate started failing with ruff 0.15.x: the TimeoutExpired handlers bound `as e` without using it (F841), and step_see_command_output decoded cmd_output into `output` but never used it. The command-output step now asserts on the \dt column headers, which are rendered whether or not any tables exist, so it verifies the special command actually produced its listing.
|
There seems to be a persistent failure in the integration scenario: |
Is the same flake has hit other PRs recently (#1544 and #1609 saw it on different py versions and went green on the next run), so a re-run of the failed job should clear it, I don't have permission to re-trigger it myself. If it keeps biting, I'd be happy to send a tiny separate PR bumping that expect_exact timeout (2s -> 10s); it only affects how long the test waits, not how fast it passes. |
The "edit sql in file with external editor" scenario errors intermittently on slow CI runners: pexpect's expect_exact waited only 2 seconds for the ex-mode banner. It has bitten upstream's CI repeatedly (PRs dbcli#1543/dbcli#1544/dbcli#1609) and now our fork's CI on main (cee716d run: unit 3139 passed, 61 scenarios passed, the only error was this scenario on 3.10 while 3.11 was fully green). All expect timeouts in iocommands.py bumped to 10s. Passing runs are not slowed: pexpect returns as soon as the expected text appears; the timeout only bounds how long a FAILING wait lasts. Verified locally: behave features/iocommands.feature green against a throwaway PG (2 scenarios, 12 steps). Same patch offered to upstream in the dbcli#1543 review thread.
Summary
This PR adds support for the
-f/--fileoption to pgcli, implementing psql-compatible behavior for executing SQL commands from files.Features
pgcli -f file.sqlpgcli -f file1.sql -f file2.sqlpgcli --file file.sqlImplementation Details
-f/--filethat accepts multiple file pathsrun_cli()to check for file mode and execute file contents before entering interactive modeecho_via_pager()to disable pager when in file modehandle_watch_command()methodTesting
Comprehensive BDD tests included covering:
-fflag--fileflag-foptions for different filesCompatibility
This implementation follows psql's behavior and maintains backward compatibility with existing functionality.
Made with ❤️ and 🤖 Claude Code