mirror of https://github.com/dbcli/pgcli
Compare commits
3 Commits
b46df1de7e
...
3cc5f3f462
Author | SHA1 | Date |
---|---|---|
Andrew MacFie | 3cc5f3f462 | |
Andrew MacFie | a14bb23128 | |
Andrew | a7a70fdc92 |
|
@ -4,8 +4,9 @@ Upcoming
|
|||
Features:
|
||||
---------
|
||||
* Support `PGAPPNAME` as an environment variable and `--application-name` as a command line argument.
|
||||
* Add `verbose_errors` config which enables the displaying of all Postgres
|
||||
error fields received.
|
||||
* Add `verbose_errors` config and `\v` special command which enable the
|
||||
displaying of all Postgres error fields received.
|
||||
* Show Postgres notifications.
|
||||
|
||||
Bug fixes:
|
||||
----------
|
||||
|
|
|
@ -73,7 +73,7 @@ from urllib.parse import urlparse
|
|||
|
||||
from getpass import getuser
|
||||
|
||||
from psycopg import OperationalError, InterfaceError
|
||||
from psycopg import OperationalError, InterfaceError, Notify
|
||||
from psycopg.conninfo import make_conninfo, conninfo_to_dict
|
||||
from psycopg.errors import Diagnostic
|
||||
|
||||
|
@ -129,6 +129,15 @@ class PgCliQuitError(Exception):
|
|||
pass
|
||||
|
||||
|
||||
def notify_callback(notify: Notify):
|
||||
click.secho(
|
||||
'Notification received on channel "{}" (PID {}):\n{}'.format(
|
||||
notify.channel, notify.pid, notify.payload
|
||||
),
|
||||
fg="green",
|
||||
)
|
||||
|
||||
|
||||
class PGCli:
|
||||
default_prompt = "\\u@\\h:\\d> "
|
||||
max_len_prompt = 30
|
||||
|
@ -684,7 +693,16 @@ class PGCli:
|
|||
# prompt for a password (no -w flag), prompt for a passwd and try again.
|
||||
try:
|
||||
try:
|
||||
pgexecute = PGExecute(database, user, passwd, host, port, dsn, **kwargs)
|
||||
pgexecute = PGExecute(
|
||||
database,
|
||||
user,
|
||||
passwd,
|
||||
host,
|
||||
port,
|
||||
dsn,
|
||||
notify_callback,
|
||||
**kwargs,
|
||||
)
|
||||
except (OperationalError, InterfaceError) as e:
|
||||
if should_ask_for_password(e):
|
||||
passwd = click.prompt(
|
||||
|
@ -694,7 +712,14 @@ class PGCli:
|
|||
type=str,
|
||||
)
|
||||
pgexecute = PGExecute(
|
||||
database, user, passwd, host, port, dsn, **kwargs
|
||||
database,
|
||||
user,
|
||||
passwd,
|
||||
host,
|
||||
port,
|
||||
dsn,
|
||||
notify_callback,
|
||||
**kwargs,
|
||||
)
|
||||
else:
|
||||
raise e
|
||||
|
|
|
@ -167,6 +167,7 @@ class PGExecute:
|
|||
host=None,
|
||||
port=None,
|
||||
dsn=None,
|
||||
notify_callback=None,
|
||||
**kwargs,
|
||||
):
|
||||
self._conn_params = {}
|
||||
|
@ -179,6 +180,7 @@ class PGExecute:
|
|||
self.port = None
|
||||
self.server_version = None
|
||||
self.extra_args = None
|
||||
self.notify_callback = notify_callback
|
||||
self.connect(database, user, password, host, port, dsn, **kwargs)
|
||||
self.reset_expanded = None
|
||||
|
||||
|
@ -237,6 +239,9 @@ class PGExecute:
|
|||
self.conn = conn
|
||||
self.conn.autocommit = True
|
||||
|
||||
if self.notify_callback is not None:
|
||||
self.conn.add_notify_handler(self.notify_callback)
|
||||
|
||||
# When we connect using a DSN, we don't really know what db,
|
||||
# user, etc. we connected to. Let's read it.
|
||||
# Note: moved this after setting autocommit because of #664.
|
||||
|
|
|
@ -9,6 +9,7 @@ from utils import (
|
|||
db_connection,
|
||||
drop_tables,
|
||||
)
|
||||
import pgcli.main
|
||||
import pgcli.pgexecute
|
||||
|
||||
|
||||
|
@ -37,6 +38,7 @@ def executor(connection):
|
|||
password=POSTGRES_PASSWORD,
|
||||
port=POSTGRES_PORT,
|
||||
dsn=None,
|
||||
notify_callback=pgcli.main.notify_callback,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import os
|
||||
import platform
|
||||
import re
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
@ -13,6 +14,7 @@ from pgcli.main import (
|
|||
obfuscate_process_password,
|
||||
duration_in_words,
|
||||
format_output,
|
||||
notify_callback,
|
||||
PGCli,
|
||||
OutputSettings,
|
||||
COLOR_CODE_REGEX,
|
||||
|
@ -450,6 +452,7 @@ def test_pg_service_file(tmpdir):
|
|||
"b_host",
|
||||
"5435",
|
||||
"",
|
||||
notify_callback,
|
||||
application_name="pgcli",
|
||||
)
|
||||
del os.environ["PGPASSWORD"]
|
||||
|
@ -505,7 +508,7 @@ def test_application_name_db_uri(tmpdir):
|
|||
cli = PGCli(pgclirc_file=str(tmpdir.join("rcfile")))
|
||||
cli.connect_uri("postgres://bar@baz.com/?application_name=cow")
|
||||
mock_pgexecute.assert_called_with(
|
||||
"bar", "bar", "", "baz.com", "", "", application_name="cow"
|
||||
"bar", "bar", "", "baz.com", "", "", notify_callback, application_name="cow"
|
||||
)
|
||||
|
||||
|
||||
|
@ -532,3 +535,23 @@ def test_application_name_db_uri(tmpdir):
|
|||
)
|
||||
def test_duration_in_words(duration_in_seconds, words):
|
||||
assert duration_in_words(duration_in_seconds) == words
|
||||
|
||||
|
||||
@dbtest
|
||||
def test_notifications(executor):
|
||||
run(executor, "listen chan1")
|
||||
|
||||
with mock.patch("pgcli.main.click.secho") as mock_secho:
|
||||
run(executor, "notify chan1, 'testing1'")
|
||||
mock_secho.assert_called()
|
||||
arg = mock_secho.call_args_list[0].args[0]
|
||||
assert re.match(
|
||||
r'Notification received on channel "chan1" \(PID \d+\):\ntesting1',
|
||||
arg,
|
||||
)
|
||||
|
||||
run(executor, "unlisten chan1")
|
||||
|
||||
with mock.patch("pgcli.main.click.secho") as mock_secho:
|
||||
run(executor, "notify chan1, 'testing2'")
|
||||
mock_secho.assert_not_called()
|
||||
|
|
|
@ -220,10 +220,13 @@ def test_database_list(executor):
|
|||
|
||||
@dbtest
|
||||
def test_invalid_syntax(executor, exception_formatter):
|
||||
result = run(executor, "invalid syntax!", exception_formatter=lambda x:
|
||||
main_exception_formatter(x, verbose_errors=False))
|
||||
result = run(
|
||||
executor,
|
||||
"invalid syntax!",
|
||||
exception_formatter=lambda x: main_exception_formatter(x, verbose_errors=False),
|
||||
)
|
||||
assert 'syntax error at or near "invalid"' in result[0]
|
||||
assert 'SQLSTATE' not in result[0]
|
||||
assert "SQLSTATE" not in result[0]
|
||||
|
||||
|
||||
@dbtest
|
||||
|
|
|
@ -6,7 +6,7 @@ from configobj import ConfigObj
|
|||
from click.testing import CliRunner
|
||||
from sshtunnel import SSHTunnelForwarder
|
||||
|
||||
from pgcli.main import cli, PGCli
|
||||
from pgcli.main import cli, notify_callback, PGCli
|
||||
from pgcli.pgexecute import PGExecute
|
||||
|
||||
|
||||
|
@ -61,6 +61,7 @@ def test_ssh_tunnel(
|
|||
"127.0.0.1",
|
||||
pgcli.ssh_tunnel.local_bind_ports[0],
|
||||
"",
|
||||
notify_callback,
|
||||
)
|
||||
mock_ssh_tunnel_forwarder.reset_mock()
|
||||
mock_pgexecute.reset_mock()
|
||||
|
@ -96,6 +97,7 @@ def test_ssh_tunnel(
|
|||
"127.0.0.1",
|
||||
pgcli.ssh_tunnel.local_bind_ports[0],
|
||||
"",
|
||||
notify_callback,
|
||||
)
|
||||
mock_ssh_tunnel_forwarder.reset_mock()
|
||||
mock_pgexecute.reset_mock()
|
||||
|
|
Loading…
Reference in New Issue