kristijanhusak / vim-dadbod-ui

Simple UI for https://github.com/tpope/vim-dadbod
MIT License
1.48k stars 86 forks source link

Dynamic database URL in ddev project #266

Closed cknott closed 1 month ago

cknott commented 2 months ago

I'm trying to use a dynamic, per project database url in the configuration, but somehow the connection fails, even though when printing the resulting url and trying to connect to it, the connection is established. Heres my approach:

vim.g.dbs = {
    {
        name = "ddevtest",
        url = "mysql://root:root@127.0.0.1:51990"
    },
    {
        name = "ddev",
        url = function()
            local url =
                "mysql://root:root@127.0.0.1:" ..
                vim.fn.system("ddev st -j | jq '.raw.services.db.host_ports|tonumber'")
            return url
        end
    }
}

The connection named 'ddevtest' is the same connection as the one from the below. Only difference is that i get the connection from below dynamically by calling the ddev status command and parsing the resulting json to get the exposed port.

Any idea what could be wrong here?

Thanks!

kristijanhusak commented 1 month ago

system might return a new line at the end. Try logging and see what's the output.

cknott commented 1 month ago

Awesome, thank you for the quick reply. The newline was indeed the cause of the error. I know use vim.fn.systemlist instead:

local url = "mysql://root:root@127.0.0.1:" .. vim.fn.systemlist("ddev st -j | jq '.raw.services.db.host_ports|tonumber'")[1]