acaloiaro / neoq

Queue-agnostic background job library for Go, with a pleasant API and powerful features.
MIT License
270 stars 4 forks source link

Migration Database URL doesn't support symbols well #112

Closed pconstantinou closed 8 months ago

pconstantinou commented 9 months ago

I got this somewhat mystifying error:

`unable to run migrations" error="parse \"postgres://postgres:d+]pa>\": invalid port \":d+]pa>\" after host"

Turns out that in initializeDB() we re-construct the connection URL from scratch (instead of using the value passed in. So, even if the caller provides a password that is correctly encoded, the connection URL used to execute migrations is not. This is also a little problematic with sslMode if it's not specified in the initial URL.

Most importantly, this introduces a bug.

in the code that reconstructs the URL, each parameter should be URL encoded. Realistically, the password is the most important, in it's current form you can't have the # or : characters in passwords. This is a pain because AWS likes to generate passwords for the databases and you can't modify them.

    pqConnectionString := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s&x-migrations-table=neoq_schema_migrations",
        pgxCfg.User,
        pgxCfg.Password,
        pgxCfg.Host,
        pgxCfg.Database,
        sslMode)

I recommend:

import "net/url"

    pqConnectionString := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s&x-migrations-table=neoq_schema_migrations",
        pgxCfg.User,
        url.QueryEscape(pgxCfg.Password),
        pgxCfg.Host,
        pgxCfg.Database,
        sslMode)