dbcli / pgspecial

Python implementation of postgres meta commands (backslash commands)
BSD 3-Clause "New" or "Revised" License
76 stars 53 forks source link

Fix view definition displaying 1 element tuple string #131

Closed andyscho closed 1 year ago

andyscho commented 1 year ago

Description

Currently in pgcli version 3.5.0, displaying view definitions with \d+ appears as the following:

postgres@/tmp:postgres> \d+ test_vw
+----------+-----------------------+-----------+----------+-------------+
| Column   | Type                  | Modifiers | Storage  | Description |
|----------+-----------------------+-----------+----------+-------------|
| username | character varying(50) |           | extended | <null>      |
| value    | integer               |           | plain    | <null>      |
+----------+-----------------------+-----------+----------+-------------+
View definition:
(' SELECT test.username,\n    test.value\n   FROM test;',)

The view definitions displaying this way is new behavior to 3.5.0 and was not present in pgcli==3.4.1, which is the output shown below:

postgres@/tmp:postgres> \d+ test_vw
+----------+-----------------------+-----------+----------+-------------+
| Column   | Type                  | Modifiers | Storage  | Description |
|----------+-----------------------+-----------+----------+-------------|
| username | character varying(50) |           | extended | <null>      |
| value    | integer               |           | plain    | <null>      |
+----------+-----------------------+-----------+----------+-------------+
View definition:
 SELECT test.username,
    test.value
   FROM test;

I believe this behavior was introduced during the switch to f-strings in this pgspecial commit, since python3 displays tuples with just a single string differently when using different string formatting methods:

>>> view_def = ("test",)
>>> "%s \n" % view_def
'test \n'
>>> f"{view_def} \n"
"('test',) \n"

This PR fixes this by making a minor change to unpack the 1-tuple. It also adds a test for displaying view definitions for a view. It is safe to always unpack the 1-tuple in this case since we know cur.rowcount > 0.

I did a cursory search through other instances of f-strings in pgspecial, and this was the only instance that I found that I think is negatively impacted by this behavior.

Checklist

j-bennet commented 1 year ago

@andyscho Nice catch. 👍