Fix alias_dsn detection for --list-dsn and -D - #1617
Conversation
--list-dsn and -D/--dsn read the config with a bare load_config that skips the packaged default, unlike the rest of pgcli. On a fresh install (config not written yet) --list-dsn hit a KeyError on the missing [alias_dsn] section and printed a misleading "Invalid DSNs found" error, and -D could crash on the missing [main] section when the user config only had an [alias_dsn] block. Load config via get_config instead, which writes the default template if needed and always carries the expected sections. Fixes dbcli#1489
|
|
||
|
|
||
| @pytest.fixture | ||
| def isolate_config(monkeypatch, tmp_path): |
|
@ChrisJr404 Nice work! The fix looks beautifully minimal. But I would suggest a different approach. When the user wants to With the What do you think? |
|
That makes sense, and I agree the current approach does more work than it should. I'll rework it so:
That keeps both paths read-only in the common case. I'll push an update along those lines. |
Description
--list-dsnand-D/--dsnread the config with a bareload_config(pgclirc, config_full_path)that skips the packaged defaultpgclirc, unlike everything else in pgcli which goes throughget_config. That's the root cause of #1489.Two ways it bites:
--list-dsnruns beforePGCli()is constructed, so on a fresh install the config file hasn't been written yet.load_configthen reads a non-existent file,cfg["alias_dsn"]raisesKeyError, and the broadexcept Exceptionswallows it and printsInvalid DSNs found in the config filewith exit 1. No template gets written either, so there's nothing for the user to edit. To someone who did add an alias it just looks like the section is being ignored.-D <alias>has the same bare read plus acfg["main"]access further down (timezone handling). If the user's config only contains an[alias_dsn]block,[main]isn't there and it blows up after resolving the alias.Switching both call sites to
get_config(pgclirc)fixes it:get_configwrites the default template when the file is missing and merges the packaged default, so the expected sections ([main],[alias_dsn]) are always present and aliases resolve the same way they do everywhere else.I couldn't reproduce the original report on a config that already has a populated
[alias_dsn](that path works), but the fresh/minimal-config cases above are reproducible and match the reported symptoms.Tests in
tests/test_alias_dsn.py: fresh config now lists nothing and exits 0 instead of erroring; populated[alias_dsn]is listed;-D <alias>resolves to the right connection;-D <unknown>still errors. The fresh-config and-D-resolve tests fail onmainand pass with this change.Checklist
changelog.rst.AUTHORSfile (or it's already there).pip install pre-commit && pre-commit install).