diff --git a/changelog.md b/changelog.md index f2132346..04387011 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,10 @@ -1.42.0 (2025/12/20) +Upcoming (TBD) ============== +Features +-------- +* Update query processing functions to allow automatic show_warnings to work for more code paths like DDL + Bug Fixes -------- * Update the prompt display logic to handle an edge case where a socket is used without diff --git a/mycli/main.py b/mycli/main.py index 6f9965b5..86dcc5c4 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -1273,7 +1273,7 @@ def format_output( if title: # Only print the title if it's not None. output = itertools.chain(output, [title]) - if cur: + if headers or (cur and title): column_types = None if isinstance(cur, Cursor): @@ -1283,7 +1283,7 @@ def get_col_type(col) -> type: column_types = [get_col_type(tup) for tup in cur.description] - if max_width is not None: + if max_width is not None and isinstance(cur, Cursor): cur = list(cur) formatted = use_formatter.format_output( @@ -1377,7 +1377,9 @@ def get_last_query(self) -> str | None: is_flag=True, help="Automatically switch to vertical output mode if the result is wider than the terminal width.", ) -@click.option("--show-warnings/--no-show-warnings", is_flag=True, help="Automatically show warnings after executing a SQL statement.") +@click.option( + "--show-warnings/--no-show-warnings", "show_warnings", is_flag=True, help="Automatically show warnings after executing a SQL statement." +) @click.option("-t", "--table", is_flag=True, help="Display batch output in table format.") @click.option("--csv", is_flag=True, help="Display batch output in CSV format.") @click.option("--warn/--no-warn", default=None, help="Warn before running a destructive query.") diff --git a/mycli/sqlexecute.py b/mycli/sqlexecute.py index 49c41e8a..d7445abb 100644 --- a/mycli/sqlexecute.py +++ b/mycli/sqlexecute.py @@ -354,7 +354,7 @@ def get_result(self, cursor: Cursor) -> tuple: plural = '' if cursor.warning_count == 1 else 's' status = f'{status}, {cursor.warning_count} warning{plural}' - return (title, cursor if cursor.description else None, headers, status) + return (title, cursor, headers, status) def tables(self) -> Generator[tuple[str], None, None]: """Yields table names""" diff --git a/test/test_main.py b/test/test_main.py index 34cbde66..3d6baaec 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -70,6 +70,17 @@ def test_disable_show_warnings(executor): assert result[0]["status"] == "Show warnings disabled." +@dbtest +def test_output_ddl_with_warning_and_show_warnings_enabled(executor): + runner = CliRunner() + db = "mycli_test_db" + table = "table_that_definitely_does_not_exist_1234" + sql = f"DROP TABLE IF EXISTS {db}.{table}" + result = runner.invoke(cli, args=CLI_ARGS + ["--show-warnings", "--no-warn"], input=sql) + expected = "Level\tCode\tMessage\nNote\t1051\tUnknown table 'mycli_test_db.table_that_definitely_does_not_exist_1234'\n" + assert expected in result.output + + @dbtest def test_output_with_warning_and_show_warnings_enabled(executor): runner = CliRunner()