Skip to content

End use and source at the newline in batch input - #2433

Merged
rolandwalker merged 1 commit into
dbcli:mainfrom
jayhemnani9910:fix/batch-line-ended-client-commands
Nov 14, 2026
Merged

rolandwalker merged 1 commit into
dbcli:mainfrom
jayhemnani9910:fix/batch-line-ended-client-commands

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

Description

The mysql client ends use, source, \u and \. at the newline, so scripts often have use db without ;. Piped into mycli, that line swallows the next statement and the run stops:

$ printf 'select 1 as a;\nuse mysql\nselect database();\nselect 5 as later;\n' | mycli -h 127.0.0.1 -u root app
a
1
(1049, "Unknown database 'mysql\nselect database()'")

select database() and select 5 never run. The same input through mysql prints a 1, database() mysql, later 5. Same with --batch file.sql and source file.sql.

statements_from_filehandle now yields a use, source, \u or \. line on its own when it starts a statement and doesn't end with the delimiter, the same way DELIMITER lines are handled. use db; still goes through the normal path.

Tested: a new test_batch_utils.py case fails without the change. pytest mycli_test/pytests: 3356 passed with the change and 3355 on main, with the same 62 failures on both (tests that need a live server here). Against MySQL 8.0 the input above now prints the same as the mysql client and exits 0. ruff check and ruff format clean.

Checklist

  • I added this contribution to the changelog.md file.
  • I added my name to the mycli/AUTHORS.txt file (or it's already there).

Comment thread mycli/packages/utils/batch_utils.py Outdated
MAX_MULTILINE_BATCH_STATEMENT = 5000

DELIMITER_RE = re.compile(r'^\s*delimiter\s+(\S+)\s*$', re.IGNORECASE)
# Client commands the mysql client ends at the newline, without a delimiter

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call it "the vendor client" for clarity.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, the comment now says "the vendor client".

Comment thread mycli/packages/utils/batch_utils.py Outdated

DELIMITER_RE = re.compile(r'^\s*delimiter\s+(\S+)\s*$', re.IGNORECASE)
# Client commands the mysql client ends at the newline, without a delimiter
LINE_COMMAND_RE = re.compile(r'^\s*(use|source|\\u|\\\.)\s+\S', re.IGNORECASE)

@rolandwalker rolandwalker Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex needs to support forward slashes for mycli syntax:

re.compile(r'^\s*(/?use|/?source|[/\\]u|[/\\]\.)\s+\S', re.IGNORECASE)

and, more importantly, can't the filename or database name contain spaces? Edit: Oh I see, there isn't a trailing anchor. Good.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, using your regex, and added a test for /use at the newline. Right, no trailing anchor, so names with spaces still match.

Comment thread changelog.md Outdated
--------
* Open the audit log and the `\T`/`\o` output files as UTF-8, so non-ASCII output no longer fails under non-UTF-8 locales.
* Support `DELIMITER` in batch input (`mycli < file.sql`, `--batch`), so dumps with triggers or routines restore.
* End `use`, `source`, `\u` and `\.` at the newline in batch input, as the mysql client does, so a `use db` line without `;` no longer swallows the next statement.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (making it shorter among other things):

Terminate `use`, `source`, `\u` and `\.` at newline in batch input, as the vendor client does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, changelog uses your wording. Also rebased on main to clear the conflict.

The mysql client ends use, source, \u and \. at the newline, so
scripts often have `use db` without a semicolon. Batch input kept
reading until the next ';', so the following statement was sent as
part of the database name and the run stopped with "Unknown
database".
@jayhemnani9910
jayhemnani9910 force-pushed the fix/batch-line-ended-client-commands branch from 846eb1d to d4492e0 Compare November 14, 2026 11:09
@rolandwalker

Copy link
Copy Markdown
Contributor

Lovely.

@rolandwalker
rolandwalker merged commit 2e0f751 into dbcli:main Nov 14, 2026
13 checks passed
@rolandwalker

Copy link
Copy Markdown
Contributor

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.

2 participants