pgcli icon indicating copy to clipboard operation
pgcli copied to clipboard

Add support for -f/--file option to execute SQL from files

Open DiegoDAF opened this issue 4 weeks ago • 0 comments

Summary

This PR adds support for the -f/--file option to pgcli, implementing psql-compatible behavior for executing SQL commands from files.

Features

  • Single file execution: pgcli -f file.sql
  • Multiple files: pgcli -f file1.sql -f file2.sql
  • Long form support: pgcli --file file.sql
  • Files are executed sequentially
  • Pager is automatically disabled in file mode
  • Proper error handling and exit codes

Implementation Details

  • Added new click option -f/--file that accepts multiple file paths
  • File paths are validated to ensure they exist and are readable
  • Modified run_cli() to check for file mode and execute file contents before entering interactive mode
  • Updated echo_via_pager() to disable pager when in file mode
  • File contents are read and executed using the existing handle_watch_command() method

Testing

Comprehensive BDD tests included covering:

  • Single file with -f flag
  • Single file with --file flag
  • Multiple statements in one file
  • Multiple -f options for different files
  • Files with special commands
  • Error handling for invalid SQL

Compatibility

This implementation follows psql's behavior and maintains backward compatibility with existing functionality.

Made with ❤️ and 🤖 Claude Code

DiegoDAF avatar Dec 05 '25 18:12 DiegoDAF