Skip to content

Completions are not refreshed when a CREATE, ALTER, DROP or RENAME starts with a comment #2438

Closed

Description

@goranai

What happens

mycli refreshes its completions after CREATE, ALTER, DROP and RENAME. It does not do that when the statement starts with a SQL comment. For example:

/* add audit table */ create table audit (id int);
-- drop the old one
drop table audit_old;
select 1; /* rename */ rename table a to b;

The statements run fine on the server. But the new table does not show up in completions, and the dropped table is still offered, until the next refresh (\# or another DDL without a comment).

Why

need_completion_refresh() in mycli/packages/utils/sql_utils.py looks at query.split()[0]. When the statement starts with a comment, that first word is /*, -- or #, so it never matches create, alter, drop or rename.

sqlparse.split() also keeps a comment that follows ; with the next statement. So select 1; /* x */ drop table t hits the same problem.

Other helpers in the same file already skip comments. query_starts_with() uses sqlparse.format(query, strip_comments=True).

Repro

On main (679a28c), Python 3.14.7, Windows 11:

from mycli.packages.utils.sql_utils import need_completion_refresh

need_completion_refresh("create table t (a int)")               # True
need_completion_refresh("/* c */ create table t (a int)")       # False
need_completion_refresh("-- c\ncreate table t (a int)")         # False
need_completion_refresh("# c\ndrop table t")                    # False
need_completion_refresh("select 1; /* c */ drop table t")       # False

I also ran _one_iteration() in repl.py with the test harness from test_main_modes_repl.py, the real need_completion_refresh() and a fake executor that returns success. refresh_completions() is called for create table t2 (a int) and is not called for the four commented versions.

I did not run this against a live MySQL server.

Related, not fixed here

/* c */ use foo has a bigger problem. It does not match the use special command, so it goes to the server as plain SQL. The server switches the database, but sql_execute.dbname keeps the old name. A completion refresh alone cannot fix that, so I left it out.

Validated with Claude Opus 5.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions