-
Notifications
You must be signed in to change notification settings - Fork 557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User comments in config file lost after writing to config via pgcli
#1240
Comments
I have looked at this some and am still puzzled about what's going on. I'm just not advanced enough in python to get all the way at this point. I'll leave some notes here in case a smarter developer can see what I cannot. I can tell you that somehow the default pgclirc that comes with pgcli and the changed configuration file are getting mixed together. I suspect that is what is causing issues. Here are the salient tests I have done so far:
Steps to reproduce:
Note that I also saw this sort of reaction when I took the user config file and truncated most of it, leaving just the [named queries] section and the sections that follow it. When I would start the program and do a save of a named query, ALL of the file would reappear, even the things I had cut out. |
@pasenor @j-bennet (I see you're active so I'll ask you for your guidance). This issue is caused by a known issue in ConfigObj.py (DiffSK/configobj#171, submitted in 2018) There is no more recent published version of ConfigObj than what you are using and it has this bug. There are more recent updates to their github. This can be fixed by removing the merges in config.py/load_config() and just pulling in the user config file (which will be the default file if they had nothing at startup). However if you do this and the user were to truncate their config file you would not get most of the default settings (colors, etc). So, recommendations on how to move forward?
|
I looked at the ConfigObj repo, and yeah, not only it has issues opened in 2014, but it has PRs open in 2016 still hanging around. I bumped the issue though, we will see if that helps at all. A couple other things we could try:
If ConfigObj maintainers remain unresponsive, looking into ConfigObj replacements would be my preferred solution. |
Do we really need this fancy merge-with-comments feature? It is difficult to do right and ties us to the few libraries like ConfigObj, and then it doesn't work. Everything will be simpler if we decouple configuration objects (that can be merged) from files (that can only be copied or edited directly).
Then we can replace ConfigObj with either toml or configparser from the standard library |
Either way, I'm willing to try helping if you can point me in the direction you want. I'm just a beginner with a keyboard so I don't feel qualified to make architectural decisions for your product. At this point, I don't really want to take on fixing ConfigObj if that were even possible. |
Update: scratch the above, we have a reasonable case for merging configs. We always want to merge existing config with default pgclirc, because it's possible that pgclirc had some newly added settings. Argh. |
We certainly always want to merge configs in-memory, as dicts. But I don't think we write the results into user's config file if it already exists: https://github.com/dbcli/pgcli/blob/master/pgcli/config.py#L34 the parameter And that's good, overwriting user's configuration without a trace is not something I would want any program to do to me. We can always show warning and let the user fix things manually. At least, if there are any automated edits, the old values should remain as comments, but that's also not something ConfigObj knows how to do. |
How about I try this:
Do you think that would work and be maintainable? |
What do you mean, remove pgclirc from the source tree and store it as a python object, without comments? But then how are we going to initialize the user's config? I'd start with your last bullet point, make a more gentle config editing function and fix the actual bug with disappearing comments. Then we can see how it looks and decide if we want to refactor things further. Also, shouldn't all this config juggling move to cli_helpers? It's quite similar in pgcli and mycli, and can be improved in both |
My thought, perhaps naively, was that we could have both comments and configurations in the code so that we could generate the default file on demand for the user. Then as the code runs we basically ignore the comments and pull in any user configs to overwrite the default configuration sections that they have changed.
The issue with doing just this part is that today, the code doesn't know what the default settings are as it pulls them in from the default file. So, if a user has a full config file, the code is fine. However if the user has (for example) only 2 entries in the config file (and we're not merging in the default file, which is where the error is) then the code won't have values for anything else.
Maybe :) I haven't looked at mycli or cli_helpers. Again, if you all can provide me direction, I'll go there. |
Fixed via #1251. This is by no means a perfect solution. I wish that configparser in stdlib supported comments on write, unfortunately it does not. I'd like to try and submit a PR to https://github.com/DiffSK/configobj to fix this issue, but it will not happen immediately. |
I submitted a PR into ConfigObj repo. We'll see what happens with that. |
Description
First of all, thanks for a great tool, I use it a lot.
User comments to the pgcli config file are getting lost after the config file gets modified via
pgcli
itself (e.g. adding a named query). According to #1162 (comment) they should remain. Additionally, all whitelines from at least the[named queries]
section onward get lost as well.I can reproduce this on a clean virtualenv and with a fresh
config
file.Reproduce
Log from my reproduction attempts:
pip install pgcli
)pgcli
, this created a new config file[named queries]
section onward; same goes for the config file snippets below) at this point:pgcli
:\ns foo bar
. Config file snippet after this:[named query]
section header is now gone as well (while the "# style classes for colored table output" comment aboveoutput.header = "#00ff5f bold"
remained in tact. As did all other comment and whitelines earlier in the file)pgcli
(just in case), add user comment above thefoo = bar
named query entry. Config file snippet after this:pgcli
again, add new named query:\ns bar SELECT 1
. Config file snippet after this:pgcli
(just in case), add record and comment to[alias_dsn]
. Config file snippet after this:pgcli
again, add new named query:\ns baz SELECT 2
. Config file snippet after this:Did another quick test (only some of the steps from above) with a Python 3.9.0 (via
pyenv
) temp virtualenv, same results. Tried this one with both apip install pgcli
and apip install git+https://github.com/dbcli/pgcli@master
.The same happens in my proper config file using my proper
pgcli
install, which contains many more named queries and DSN aliases.Environment, verison info
OS: Ubuntu 18.04.5 (x64)
pgcli:
pip freeze
of my proper install:The text was updated successfully, but these errors were encountered: