`exit` is undefined when the `site` module isn't loaded (i.e. when
Python is executed with the `-S` option), and `exit` isn't explicitly
defined as a function or imported from elsewhere in `main.py`, so
reaching any code path that attempts to execute `exit(1)` in this
environment results in a `NameError` being raised:
```
Traceback (most recent call last):
File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "[...]/__main__.py", line 415, in <module>
File "[...]/__main__.py", line 399, in run
File "[...]/__main__.py", line 389, in main
File "/usr/lib/python3.10/runpy.py", line 220, in run_module
mod_name, mod_spec, code = _get_module_details(mod_name)
File "/usr/lib/python3.10/runpy.py", line 157, in _get_module_details
code = loader.get_code(mod_name)
File "[...]/__main__.py", line 264, in get_code
File "[...]/__main__.py", line 204, in load_module
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "third_party/python3/__pgcli_main__.py", line 3, in <module>
File "[...]/click/core.py", line 1157, in __call__
File "[...]/click/core.py", line 1078, in main
File "[...]/click/core.py", line 1434, in invoke
File "[...]/click/core.py", line 783, in invoke
File "[...]/pgcli/main.py", line 1617, in cli
File "[...]/pgcli/main.py", line 766, in connect
NameError: name 'exit' is not defined. Did you mean: 'atexit'?
```
When exiting unsuccessfully, do so with `sys.exit(1)` - this is already
used in several other places in `main.py`, and is always used (with exit
code 0) when exiting successfully.
* Add `--ping` as a CLI option
This builds in the functionality asked for in #1470; it allows pgcli to
replace `pg_isready` from the postgresql command line toolchain
* Update the changelog and AUTHORS
* Use click.echo
pgcli.echo doesn't seem to actually _echo_ anything
* Add integ tests for the ping feature
* Refactor try / else
Co-authored-by: Damien Baty <damien@damienbaty.com>
* Unused variable
---------
Co-authored-by: Irina Truong <637013+j-bennet@users.noreply.github.com>
Co-authored-by: Damien Baty <damien@damienbaty.com>
* Add --log-file & \log-file option to always capture output
Currently outputting to a file via \o disables the console output
This patch adds a `--log-file` cli arg with similar behavior to `psql`* and a \log-file
special command to enable/disable it from the console
*https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-OPTION-LOG-FILE
* switch to use context manager
Co-authored-by: Irina Truong <637013+j-bennet@users.noreply.github.com>
* switch to use context manager
Co-authored-by: Irina Truong <637013+j-bennet@users.noreply.github.com>
* use isoformat explicitly
Co-authored-by: Irina Truong <637013+j-bennet@users.noreply.github.com>
* change test to use a mock, update changelog & authors
* reformat
* black
---------
Co-authored-by: Irina Truong <637013+j-bennet@users.noreply.github.com>
The application_name to be used when connecting to a database can now be
specified as a command line argument (--application-name) or be taken
directly from environment variables (PGAPPNAME). It still defaults to
'pgcli' when not specified.
Closes#1421.
* fix psycopg.sql.Identifier in \ev handling (#1384)
* Allow defining a json file with preferred aliases
At $WORK we have a lot of tables with names like `foo_noun_verb` or
`foo_noun_related-noun_verb` and so while the default aliasing is very
helpful for shortening unwieldy names we do end up with lots of aliases
like `LEFT JOIN fnv on fnv2.id = fnv.fnv2_id`
This change will allow defining a json file of preferred aliases
```
> cat ~/.config/pgcli/aliases.json
{
"foo_user": "user",
"foo_user_group": "user_group"
}
```
so the alias suggestion for `SELECT * FROM foo_user` will be `SELECT * FROM foo_user AS user`
instead of the default `SELECT * FROM foo_user AS fu`
* When cannot open or parse alias_map_file raise error
Raise a (hopefully) helpful exception when the alias_map_file cannot be
parsed or does not exist
* Add tests for load_alias_map_file
* Add tests for generate_alias
* Update AUTHORS file
* Remove comment.
Discussed this on the PR with a project maintainer
---------
Co-authored-by: Andy Schoenberger <akschoenberger@gmail.com>
Co-authored-by: Rob B <rob@example.com>
Co-authored-by: Irina Truong <i.chernyavska@gmail.com>
When this option is on, any statement that is deemed destructive
(through the use of the `destructive_warning` config option) will
not be executed unless a transaction has been started.
* Changed destructive_warning to take a list of destructive commands and added the dsn_alias as part of the destructive command warning
* Updated parse_destructive_warning to handle None
* Reverted auto formatted change to AUTHORS
* Reverted auto formatted change to AUTHORS
* Sub: Add new formatter to export data to sql like mycli
Body: New formatter is added, we can export query result to sql
insertion like mycli
==== End ====
* Sub: Install black and reformat code
Body:
==== End ====
* Sub: Add unit tests for new formatter
Body:
==== End ====
* Sub: Add new formatter to pgclirc
1. `class A(object)` can be written as `class A:`
2. replace `dict([…])` and `set([…])` with `{…}`
3. use f-strings or compact `.format`
4. use `yield from` instead of `yield` in a `for` loop
5. import `mock` from `unittest`
6. expect `OSError` instead of `IOError` or `select` error
7. use Python3 defaults for file reading or `super()`
8. remove redundant parenthesis (keep those in tuples though)
9. shorten set intersection instead of creating lists
10. backslashes in strings do not have to be escaped if prepended with `r`
These config properties got introduced in 41dd24e8 as a means to have
more granular control over the syntax highlighting. The problem is that
these cannot be in the default config file since `get_config()` always
reads both the default config file and the user specified one, and there
is no way to unset these variables in the user specified config file to
restore their default behavior. Even if there would be a way, it
wouldn't be intuitive at all to be required to unset some random
settings under the `[colors]` section just to be able to use the well
documented `syntax_style` setting.
Note that one *can* still set these three lines in their user config
file if they want to utilize them.
Resolves#1212
* Add schema suggestion for functions
- Update sqlcompletion.py
- Update metadata files
* Move autocomletion for function under condition
- Make sure suggestion are only added under drop, alter etc.
* Stop printing "status" when table_format is "csv"
* Use the "unix" dialect on *nix for CSV output.
* Use a pager when `pspg` has been configured with CSV "table_format".
Fix#1102
* adds ternary to decide whether bottom_toolbar is shown based on config value
* runs black format
* adds name to authors
* modifies changelog.rst
* removes change in changelog.rst
* formats config
* removes unused variable, adds change to changelog
* changes default back to true
* removed reference to var
* Added changes to skip empty queries
Empty queries consist of one or more semicolons
* Reformatted with black
* Added contribution to changelog and name to AUTHORS
* Black.
* README.rst: tidy up redundant instructions
Remove the README section which detailed how to force `psycopg2` 2.7 dependency installation from source rather than binary.
`psycopg2` 2.7 would automatically favour installation from binary. A bug affecting some users made it desirable to have the option to install from source instead. The README detailed how to do this using pip's `--no-binary` option (nice!).
`psycopg2` 2.8 has now split the binary and source dependencies into different packages with different names (source: [psycopg2 docs](http://initd.org/psycopg/docs/install.html#change-in-binary-packages-between-psycopg-2-7-and-2-8)) which makes the `--no-binary` instructions redundant. To specify source/binary you now need to explicitly specify the desired package.
As of #1060, installing `pgcli` from pip will result in the installation of `psycopg2` 2.8 from source.
* README: additional instructions for psycopg2 2.7 users
Anyone limited to psycopg2 2.7 may still encounter some known issues when installing from binary.
Add a link to the README to acknowledge this, and help those users install it from source.
* Update changelog.rst
* Update AUTHORS
* Update changelog
* Allow psycopg2 up to 2.8.2
This fixes the issue with pgcli not working on Arch Linux, since the package `python2-psycopg2` is already on 2.8.2.
* Added name to 'AUTHORS' and contribution to 'changelog.rst' referring to #2de6266
* Allow for newer versions of psycopg2 to be used
* Switch to psycopg2 parse_dsn instead of urlparse
* Added wronglink to contributors and updated changelog
* Fix test codestyle
* Support for PGPORT customization in tests
* Support for PGPORT customization in tests
* Refactored PGExecute init and moved short_host generation to object property
* Fix test util codestyle
* Fix local tests run
* Store PGExecute initial params in _conn_params and added PGExecute.copy method
* Fix codestyle
* Added docstring to PGExecute.copy() method