Skip to content

Commit c779e56

Browse files
committed
align \du output with psql
1 parent b26a06d commit c779e56

File tree

2 files changed

+62
-57
lines changed

2 files changed

+62
-57
lines changed

pgspecial/sql/dbcommands.sql

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,47 @@ JOIN pg_catalog.pg_tablespace t ON d.dattablespace = t.oid
1919
WHERE d.datname ~ :pattern
2020
ORDER BY 1
2121
-- name: list_roles_9
22-
SELECT r.rolname,
23-
r.rolsuper,
24-
r.rolinherit,
25-
r.rolcreaterole,
26-
r.rolcreatedb,
27-
r.rolcanlogin,
28-
r.rolconnlimit,
29-
r.rolvaliduntil,
30-
ARRAY(SELECT b.rolname FROM pg_catalog.pg_auth_members m JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid) WHERE m.member = r.oid) as memberof,
31-
pg_catalog.shobj_description(r.oid, 'pg_authid') AS description,
32-
r.rolreplication
22+
SELECT r.rolname as role_name,
23+
ARRAY_TO_STRING(
24+
ARRAY_REMOVE(
25+
ARRAY[
26+
CASE WHEN r.rolsuper THEN 'Superuser' END
27+
, CASE WHEN r.rolcreaterole THEN 'Create role' END
28+
, CASE WHEN r.rolcreatedb THEN 'Create DB' END
29+
, CASE WHEN r.rolreplication THEN 'Replication' END
30+
, CASE WHEN r.rolbypassrls THEN 'Bypass RLS' END
31+
, CASE WHEN r.rolconnlimit != -1 THEN r.rolconnlimit::text END
32+
, CASE WHEN r.rolvaliduntil is not null THEN r.rolvaliduntil::text END
33+
, CASE WHEN not r.rolinherit THEN 'No Inherit' END
34+
, CASE WHEN not r.rolcanlogin THEN 'No Login' END
35+
],
36+
NULL),
37+
', ') AS attributes,
38+
ARRAY_TO_STRING(
39+
ARRAY(SELECT b.rolname
40+
FROM pg_catalog.pg_auth_members m
41+
JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
42+
WHERE m.member = r.oid),
43+
', ') as member_of
44+
, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description
3345
FROM pg_catalog.pg_roles r
34-
WHERE r.rolname ~ :pattern
46+
WHERE CASE WHEN :pattern = '.*' THEN r.rolname !~ '^pg_'
47+
ELSE r.rolname OPERATOR(pg_catalog.~) :pattern COLLATE pg_catalog.default
48+
END
3549
ORDER BY 1
3650
-- name: list_roles
37-
SELECT u.usename AS rolname,
38-
u.usesuper AS rolsuper,
39-
TRUE AS rolinherit,
40-
FALSE AS rolcreaterole,
41-
u.usecreatedb AS rolcreatedb,
42-
TRUE AS rolcanlogin,
43-
-1 AS rolconnlimit,
44-
u.valuntil AS rolvaliduntil,
45-
ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = any(g.grolist)) AS memberof
51+
SELECT u.usename AS role_name,
52+
ARRAY_TO_STRING(
53+
ARRAY_REMOVE(
54+
ARRAY[
55+
CASE WHEN u.usesuper THEN 'Superuser' END
56+
, CASE WHEN u.usecreatedb THEN 'Create DB' END
57+
, CASE WHEN u.valuntil IS NOT NULL THEN u.valuntil::text END
58+
],
59+
NULL),
60+
', ') AS attributes,
61+
ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = any(g.grolist)) AS member_of,
62+
'' AS descripion
4663
FROM pg_catalog.pg_user u
4764
-- name: list_privileges
4865
-- docs: ("\\dp", "\\dp [pattern]", "List roles.", aliases=("\\z",))

tests/test_specials.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,59 +56,47 @@ def test_slash_l_verbose(executor):
5656
@dbtest
5757
def test_slash_du(executor):
5858
results = executor(r"\du")
59-
row = ("postgres", True, True, True, True, True, -1, None, [], True)
59+
row = (
60+
"postgres",
61+
"Superuser, Create role, Create DB, Replication, Bypass RLS",
62+
"",
63+
)
6064
headers = [
61-
"rolname",
62-
"rolsuper",
63-
"rolinherit",
64-
"rolcreaterole",
65-
"rolcreatedb",
66-
"rolcanlogin",
67-
"rolconnlimit",
68-
"rolvaliduntil",
69-
"memberof",
70-
"rolreplication",
65+
"role_name",
66+
"attributes",
67+
"member_of",
7168
]
7269
assert headers == results[2]
7370
assert row in results[1]
7471

7572

7673
@dbtest
7774
def test_slash_du_pattern(executor):
78-
results = executor(r"\du post*")
79-
row = [("postgres", True, True, True, True, True, -1, None, [], True)]
80-
headers = [
81-
"rolname",
82-
"rolsuper",
83-
"rolinherit",
84-
"rolcreaterole",
85-
"rolcreatedb",
86-
"rolcanlogin",
87-
"rolconnlimit",
88-
"rolvaliduntil",
89-
"memberof",
90-
"rolreplication",
91-
]
75+
results = executor(r"\du postgres*")
76+
row = (
77+
"postgres",
78+
"Superuser, Create role, Create DB, Replication, Bypass RLS",
79+
"",
80+
)
81+
headers = ["role_name", "attributes", "member_of"]
9282
assert headers == results[2]
9383
assert row == results[1]
9484

9585

9686
@dbtest
9787
def test_slash_du_verbose(executor):
9888
results = executor(r"\du+")
99-
row = ("postgres", True, True, True, True, True, -1, None, [], None, True)
89+
row = (
90+
"postgres",
91+
"Superuser, Create role, Create DB, Replication, Bypass RLS",
92+
"",
93+
None,
94+
)
10095
headers = [
101-
"rolname",
102-
"rolsuper",
103-
"rolinherit",
104-
"rolcreaterole",
105-
"rolcreatedb",
106-
"rolcanlogin",
107-
"rolconnlimit",
108-
"rolvaliduntil",
109-
"memberof",
96+
"role_name",
97+
"attributes",
98+
"member_of",
11099
"description",
111-
"rolreplication",
112100
]
113101
assert headers == results[2]
114102
assert row in results[1]

0 commit comments

Comments
 (0)