Skip to content

Support --username and environment variable to set username#1721

Open
rolandwalker wants to merge 1 commit intomainfrom
RW/support-alternate-spelling-option-username
Open

Support --username and environment variable to set username#1721
rolandwalker wants to merge 1 commit intomainfrom
RW/support-alternate-spelling-option-username

Conversation

@rolandwalker
Copy link
Contributor

Description

  • make --username an alias for --user at the CLI
  • support an environment variable $MYSQL_USER which can be used in place of the CLI argument

Rationale: we support both --pass and --password. The distinction between --user and --username is hard to remember. All other connection coordinates support an environment variable.

Checklist

  • I added this contribution to the changelog.md file.
  • I added my name to the AUTHORS file (or it's already there).
  • To lint and format the code, I ran
    uv run ruff check && uv run ruff format && uv run mypy --install-types .

 * make --username an alias for --user at the CLI
 * support an environment variable $MYSQL_USER which can be used in
   place of the CLI argument

Rationale: we support both --pass and --password.  The distinction
between --user and --username is hard to remember.  All other
connection coordinates support an environment variable.
@rolandwalker rolandwalker self-assigned this Mar 17, 2026
@github-actions
Copy link

Findings

  1. High – MYSQL_USER now overrides explicit DSN intent and can disable DSN alias resolution
    In the new option definition, user is populated from env at parse time (envvar='MYSQL_USER'), so later DSN logic treats user as already set.
    • Alias resolution is skipped when user is truthy: mycli/main.py:2231
    • DSN username is ignored when user is already set: mycli/main.py:2260
    • Source of this behavior: mycli/main.py:1928
      This means MYSQL_USER can unexpectedly change connection target semantics (e.g., mycli prod no longer resolves prod DSN alias, or mysql://dsn_user@... silently uses env user instead).
      The new test currently codifies this regression: test/test_main.py:1173.

Actionable fix

  • Treat MYSQL_USER as a fallback only after DSN/alias parsing (like: if user is None: user = os.environ.get("MYSQL_USER")), instead of binding it directly in Click.
  • Update tests to assert precedence: CLI --user/--username > DSN username > MYSQL_USER and that DSN alias still resolves when MYSQL_USER is set.

Could not run tests locally because pytest/uv are not installed in this environment.

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.

1 participant