Skip to content

Commit 9c30899

Browse files
committed
respect --logfile with --execute at the CLI
This also works when piping in a script via the standard input.
1 parent d94ca37 commit 9c30899

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
TBD
2+
==============
3+
4+
Bug Fixes
5+
--------
6+
* Respect `--logfile` when using `--execute` or standard input at the shell CLI.
7+
8+
19
1.44.2 (2026/01/13)
210
==============
311

mycli/main.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -992,10 +992,7 @@ def one_iteration(text: str | None = None) -> None:
992992
logger.debug("sql: %r", text)
993993

994994
special.write_tee(self.get_prompt(self.prompt_format) + text)
995-
if self.logfile:
996-
self.logfile.write(f"\n# {datetime.now()}\n")
997-
self.logfile.write(text)
998-
self.logfile.write("\n")
995+
self.log_query(text)
999996

1000997
successful = False
1001998
start = time()
@@ -1176,6 +1173,12 @@ def reconnect(self, database: str = "") -> bool:
11761173
self.echo(str(e), err=True, fg="red")
11771174
return False
11781175

1176+
def log_query(self, query: str) -> None:
1177+
if isinstance(self.logfile, TextIOWrapper):
1178+
self.logfile.write(f"\n# {datetime.now()}\n")
1179+
self.logfile.write(query)
1180+
self.logfile.write("\n")
1181+
11791182
def log_output(self, output: str) -> None:
11801183
"""Log the output in the audit log, if it's enabled."""
11811184
if isinstance(self.logfile, TextIOWrapper):
@@ -1355,6 +1358,7 @@ def get_prompt(self, string: str) -> str:
13551358
def run_query(self, query: str, new_line: bool = True) -> None:
13561359
"""Runs *query*."""
13571360
assert self.sqlexecute is not None
1361+
self.log_query(query)
13581362
results = self.sqlexecute.run(query)
13591363
for result in results:
13601364
title = result.title
@@ -1371,6 +1375,7 @@ def run_query(self, query: str, new_line: bool = True) -> None:
13711375
self.null_string,
13721376
)
13731377
for line in output:
1378+
self.log_output(line)
13741379
click.echo(line, nl=new_line)
13751380

13761381
# get and display warnings if enabled

test/test_main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,3 +878,22 @@ def test_global_init_commands(executor):
878878
expected = "sql_select_limit\t9999\n"
879879
assert result.exit_code == 0
880880
assert expected in result.output
881+
882+
883+
@dbtest
884+
def test_execute_with_logfile(executor):
885+
"""Test that --execute combines with --logfile"""
886+
sql = 'select 1'
887+
runner = CliRunner()
888+
889+
with NamedTemporaryFile(mode="w", delete=False) as logfile:
890+
result = runner.invoke(mycli.main.cli, args=CLI_ARGS + ["--logfile", logfile.name, "--execute", sql])
891+
assert result.exit_code == 0
892+
893+
assert os.path.getsize(logfile.name) > 0
894+
895+
try:
896+
if os.path.exists(logfile.name):
897+
os.remove(logfile.name)
898+
except Exception as e:
899+
print(f"An error occurred while attempting to delete the file: {e}")

0 commit comments

Comments
 (0)