Skip to content

Bug: schema matching fails when schema name contains hyphens #32

@joehorz

Description

@joehorz

Error message:

Error in TextChangedI Autocommands for "*":
Lua callback: ....local/share/nvim/lazy/cmp-dbee/lua/cmp-dbee/handler.lua:121: bad argument #1 to 'ipairs' (table expected, got nil)
stack traceback:
        [C]: in function 'ipairs'
        ....local/share/nvim/lazy/cmp-dbee/lua/cmp-dbee/handler.lua:121: in function 'get_documentation'
        ....local/share/nvim/lazy/cmp-dbee/lua/cmp-dbee/handler.lua:198: in function 'convert_to_completion_item'
        ....local/share/nvim/lazy/cmp-dbee/lua/cmp-dbee/handler.lua:214: in function 'get_completion'
        .../.local/share/nvim/lazy/cmp-dbee/lua/cmp-dbee/source.lua:29: in function 'complete'
        ...hihao/.local/share/nvim/lazy/nvim-cmp/lua/cmp/source.lua:342: in function 'complete'
        ...ezhihao/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:308: in function 'complete'
        ...ezhihao/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:178: in function 'callback'
        ...ezhihao/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:238: in function 'autoindent'
        ...ezhihao/.local/share/nvim/lazy/nvim-cmp/lua/cmp/core.lua:170: in function 'on_change'
        ...ezhihao/.local/share/nvim/lazy/nvim-cmp/lua/cmp/init.lua:372: in function 'callback'
        ...local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/autocmd.lua:53: in function 'emit'
        ...local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/autocmd.lua:14: in function <...local/share/nvim/lazy/nvim-cmp/lua/cmp/utils/autocmd.lua:13>

Description:

The function Connection:get_models(schema) function in cmp-dbee/lua/cmp-dbee/connection.lua fails to match schema names that contain hyphens (e.g., "key-monitor") due to incorrect use of Lua's pattern matching.

Steps to reproduce:

Have a schema/database named with hyphens (e.g., "key-monitor")
Trigger SQL completion in a context where this schema should be matched
Expected: Should return models/tables for that schema
Actual: Returns empty results

Root cause:

The code uses Lua's string.matchfor pattern matching:

if schema:match(node.name) and node.children then
    return node.children
end

In Lua pattern matching, the hyphen (-) is a special character that means "zero or more occurrences of the previous character, matching as few as possible" (lazy matching). Therefore:

  • string.match("key-monitor", "key-monitor")returns nil

The correct pattern would be:

  • string.match("key-monitor", "key%-monitor")(with hyphen escaped)

Solution:

Since the function is checking for exact string equality (not pattern matching), it should use the equality operator instead:

if node.name and node.name == schema and node.children then
    return node.children
end

Affected code:

The issue is in lua/cmp_dbee/connection.lua in the get_models function. There might be similar issues elsewhere in the codebase where string.matchis used for exact string comparisons

Additional context:

This issue might manifest differently in standard Lua vs. LuaJIT (used by Neovim). In standard Lua, string.match("key-monitor", "key-monitor")might work, but in LuaJIT it returns nil. Using the equality operator ensures consistent behavior across Lua implementations.

Suggested fix:

I recommend changing the pattern matching to exact string comparison as shown above. This is a minimal, safe change that preserves the original intent while fixing the bug.


Personal note:

This is my first time submitting an issue to an open-source project. I spent a full day debugging this issue with the help of an AI assistant, and I learned a lot about Lua pattern matching in the process. I want to thank you for creating and maintaining this plugin—it's been incredibly helpful in my daily workflow. I appreciate all the work you've put into it, and I hope this detailed bug report is helpful!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions