From abaebdbffe1395f772ee724198b4f04680ba2532 Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:08:37 -0600 Subject: [PATCH 1/7] Fix - Raised notices are printed backwards #1443 --- pgcli/pgexecute.py | 2 +- tests/features/steps/crud_database.py | 1 + tests/test_pgexecute.py | 36 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py index db56aca3c..fa8b327c6 100644 --- a/pgcli/pgexecute.py +++ b/pgcli/pgexecute.py @@ -437,7 +437,7 @@ def execute_normal_sql(self, split_sql): def handle_notices(n): nonlocal title - title = f"{n.message_primary}\n{n.message_detail}\n{title}" + title = f"{title}{n.message_primary}\n{n.message_detail}\n" self.conn.add_notice_handler(handle_notices) diff --git a/tests/features/steps/crud_database.py b/tests/features/steps/crud_database.py index 87cdc85b5..9507d461a 100644 --- a/tests/features/steps/crud_database.py +++ b/tests/features/steps/crud_database.py @@ -3,6 +3,7 @@ Each step is defined by the string decorating it. This string is used to call the step in "*.feature" file. """ + import pexpect from behave import when, then diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py index 95f60ad74..8e6322687 100644 --- a/tests/test_pgexecute.py +++ b/tests/test_pgexecute.py @@ -690,6 +690,42 @@ def test_function_definition(executor): result = executor.function_definition("the_number_three") +@dbtest +def test_function_notice_order(executor): + run( + executor, + """ + CREATE OR REPLACE FUNCTION pgcli_demo_order() RETURNS VOID AS + $$ + BEGIN + RAISE NOTICE 'first'; + RAISE NOTICE 'second'; + RAISE NOTICE 'third'; + RAISE NOTICE 'fourth'; + RAISE NOTICE 'fifth'; + RAISE NOTICE 'sixth'; + END; + $$ + LANGUAGE plpgsql; + """, + ) + result = executor.function_definition("pgcli_demo_order") + + result = run(executor, "select demo_order()") + assert ( + "first\nNone\nsecond\nNone\nthird\nNone\nfourth\nNone\nfifth\nNone\nsixth\nNone\n" + in result[0] + ) + assert "+------------+" in result[1] + assert "| demo_order |" in result[2] + assert "|------------|" in result[3] + assert "| |" in result[4] + assert "+------------+" in result[5] + assert "SELECT 1" in result[6] + + print(result) + + @dbtest def test_view_definition(executor): run(executor, "create table tbl1 (a text, b numeric)") From 75921494df3c662e8b8a93ca6b427efa5ddda09c Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:11:22 -0600 Subject: [PATCH 2/7] updated changelog --- changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.rst b/changelog.rst index f7182cf5c..85c9b21e6 100644 --- a/changelog.rst +++ b/changelog.rst @@ -9,6 +9,7 @@ Bug fixes: ---------- * Fix display of "short host" in prompt (with `\h`) for IPv4 addresses ([issue 964](https://github.com/dbcli/pgcli/issues/964)). +* Fix backwards display of NOTICEs from a Function ([issue 1443](https://github.com/dbcli/pgcli/issues/1443)) ================== From a146a920bf45e74b0a13129c66a6e3977c3dff93 Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:13:59 -0600 Subject: [PATCH 3/7] removed a print --- tests/test_pgexecute.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py index 8e6322687..ba6e6e6da 100644 --- a/tests/test_pgexecute.py +++ b/tests/test_pgexecute.py @@ -723,8 +723,6 @@ def test_function_notice_order(executor): assert "+------------+" in result[5] assert "SELECT 1" in result[6] - print(result) - @dbtest def test_view_definition(executor): From 7992426d07ced3998dd1c3990dbc779c93b3e179 Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:34:58 -0600 Subject: [PATCH 4/7] fixed up syntax error --- tests/test_pgexecute.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py index ba6e6e6da..6539e74b1 100644 --- a/tests/test_pgexecute.py +++ b/tests/test_pgexecute.py @@ -695,7 +695,7 @@ def test_function_notice_order(executor): run( executor, """ - CREATE OR REPLACE FUNCTION pgcli_demo_order() RETURNS VOID AS + CREATE OR REPLACE FUNCTION demo_order() RETURNS VOID AS $$ BEGIN RAISE NOTICE 'first'; @@ -709,7 +709,7 @@ def test_function_notice_order(executor): LANGUAGE plpgsql; """, ) - result = executor.function_definition("pgcli_demo_order") + result = executor.function_definition("demo_order") result = run(executor, "select demo_order()") assert ( From 4f9c130339a51e0baff624447fb421657e691ae9 Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:53:23 -0600 Subject: [PATCH 5/7] removing unneeded Nones from output --- pgcli/pgexecute.py | 6 +++++- tests/test_pgexecute.py | 5 +---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py index fa8b327c6..e19e21745 100644 --- a/pgcli/pgexecute.py +++ b/pgcli/pgexecute.py @@ -437,7 +437,11 @@ def execute_normal_sql(self, split_sql): def handle_notices(n): nonlocal title - title = f"{title}{n.message_primary}\n{n.message_detail}\n" + title = f"{title}" + if n.message_primary: + title = f"{title}\n{n.message_primary}" + if n.message_detail: + title = f"{title}\n{n.message_detail}" self.conn.add_notice_handler(handle_notices) diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py index 6539e74b1..46dfe5e10 100644 --- a/tests/test_pgexecute.py +++ b/tests/test_pgexecute.py @@ -712,10 +712,7 @@ def test_function_notice_order(executor): result = executor.function_definition("demo_order") result = run(executor, "select demo_order()") - assert ( - "first\nNone\nsecond\nNone\nthird\nNone\nfourth\nNone\nfifth\nNone\nsixth\nNone\n" - in result[0] - ) + assert "first\nsecond\nthird\nfourth\nfifth\nsixth" in result[0] assert "+------------+" in result[1] assert "| demo_order |" in result[2] assert "|------------|" in result[3] From 487c0f849d75bca741f563ac092f86f64e9420e2 Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Sun, 4 Feb 2024 21:01:14 -0600 Subject: [PATCH 6/7] rem var due to github recommendation --- tests/test_pgexecute.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_pgexecute.py b/tests/test_pgexecute.py index 46dfe5e10..80efc606e 100644 --- a/tests/test_pgexecute.py +++ b/tests/test_pgexecute.py @@ -709,7 +709,8 @@ def test_function_notice_order(executor): LANGUAGE plpgsql; """, ) - result = executor.function_definition("demo_order") + + executor.function_definition("demo_order") result = run(executor, "select demo_order()") assert "first\nsecond\nthird\nfourth\nfifth\nsixth" in result[0] From d08244221099697cc07cde7a24134fd9a86f7a91 Mon Sep 17 00:00:00 2001 From: ERYoung11 <78834571+ERYoung11@users.noreply.github.com> Date: Mon, 5 Feb 2024 07:09:08 -0600 Subject: [PATCH 7/7] adjusting if statements. --- pgcli/pgexecute.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pgcli/pgexecute.py b/pgcli/pgexecute.py index e19e21745..f7eb6f865 100644 --- a/pgcli/pgexecute.py +++ b/pgcli/pgexecute.py @@ -438,9 +438,9 @@ def execute_normal_sql(self, split_sql): def handle_notices(n): nonlocal title title = f"{title}" - if n.message_primary: + if n.message_primary is not None: title = f"{title}\n{n.message_primary}" - if n.message_detail: + if n.message_detail is not None: title = f"{title}\n{n.message_detail}" self.conn.add_notice_handler(handle_notices)