Skip to content

Commit

Permalink
align \du output with psql
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuku committed Nov 25, 2023
1 parent b26a06d commit c779e56
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 57 deletions.
59 changes: 38 additions & 21 deletions pgspecial/sql/dbcommands.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,47 @@ JOIN pg_catalog.pg_tablespace t ON d.dattablespace = t.oid
WHERE d.datname ~ :pattern
ORDER BY 1
-- name: list_roles_9
SELECT r.rolname,
r.rolsuper,
r.rolinherit,
r.rolcreaterole,
r.rolcreatedb,
r.rolcanlogin,
r.rolconnlimit,
r.rolvaliduntil,
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,
pg_catalog.shobj_description(r.oid, 'pg_authid') AS description,
r.rolreplication
SELECT r.rolname as role_name,
ARRAY_TO_STRING(
ARRAY_REMOVE(
ARRAY[
CASE WHEN r.rolsuper THEN 'Superuser' END
, CASE WHEN r.rolcreaterole THEN 'Create role' END
, CASE WHEN r.rolcreatedb THEN 'Create DB' END
, CASE WHEN r.rolreplication THEN 'Replication' END
, CASE WHEN r.rolbypassrls THEN 'Bypass RLS' END
, CASE WHEN r.rolconnlimit != -1 THEN r.rolconnlimit::text END
, CASE WHEN r.rolvaliduntil is not null THEN r.rolvaliduntil::text END
, CASE WHEN not r.rolinherit THEN 'No Inherit' END
, CASE WHEN not r.rolcanlogin THEN 'No Login' END
],
NULL),
', ') AS attributes,
ARRAY_TO_STRING(
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 member_of
, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description
FROM pg_catalog.pg_roles r
WHERE r.rolname ~ :pattern
WHERE CASE WHEN :pattern = '.*' THEN r.rolname !~ '^pg_'
ELSE r.rolname OPERATOR(pg_catalog.~) :pattern COLLATE pg_catalog.default
END
ORDER BY 1
-- name: list_roles
SELECT u.usename AS rolname,
u.usesuper AS rolsuper,
TRUE AS rolinherit,
FALSE AS rolcreaterole,
u.usecreatedb AS rolcreatedb,
TRUE AS rolcanlogin,
-1 AS rolconnlimit,
u.valuntil AS rolvaliduntil,
ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = any(g.grolist)) AS memberof
SELECT u.usename AS role_name,
ARRAY_TO_STRING(
ARRAY_REMOVE(
ARRAY[
CASE WHEN u.usesuper THEN 'Superuser' END
, CASE WHEN u.usecreatedb THEN 'Create DB' END
, CASE WHEN u.valuntil IS NOT NULL THEN u.valuntil::text END
],
NULL),
', ') AS attributes,
ARRAY(SELECT g.groname FROM pg_catalog.pg_group g WHERE u.usesysid = any(g.grolist)) AS member_of,
'' AS descripion
FROM pg_catalog.pg_user u
-- name: list_privileges
-- docs: ("\\dp", "\\dp [pattern]", "List roles.", aliases=("\\z",))
Expand Down
60 changes: 24 additions & 36 deletions tests/test_specials.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,59 +56,47 @@ def test_slash_l_verbose(executor):
@dbtest
def test_slash_du(executor):
results = executor(r"\du")
row = ("postgres", True, True, True, True, True, -1, None, [], True)
row = (
"postgres",
"Superuser, Create role, Create DB, Replication, Bypass RLS",
"",
)
headers = [
"rolname",
"rolsuper",
"rolinherit",
"rolcreaterole",
"rolcreatedb",
"rolcanlogin",
"rolconnlimit",
"rolvaliduntil",
"memberof",
"rolreplication",
"role_name",
"attributes",
"member_of",
]
assert headers == results[2]
assert row in results[1]


@dbtest
def test_slash_du_pattern(executor):
results = executor(r"\du post*")
row = [("postgres", True, True, True, True, True, -1, None, [], True)]
headers = [
"rolname",
"rolsuper",
"rolinherit",
"rolcreaterole",
"rolcreatedb",
"rolcanlogin",
"rolconnlimit",
"rolvaliduntil",
"memberof",
"rolreplication",
]
results = executor(r"\du postgres*")
row = (
"postgres",
"Superuser, Create role, Create DB, Replication, Bypass RLS",
"",
)
headers = ["role_name", "attributes", "member_of"]
assert headers == results[2]
assert row == results[1]


@dbtest
def test_slash_du_verbose(executor):
results = executor(r"\du+")
row = ("postgres", True, True, True, True, True, -1, None, [], None, True)
row = (
"postgres",
"Superuser, Create role, Create DB, Replication, Bypass RLS",
"",
None,
)
headers = [
"rolname",
"rolsuper",
"rolinherit",
"rolcreaterole",
"rolcreatedb",
"rolcanlogin",
"rolconnlimit",
"rolvaliduntil",
"memberof",
"role_name",
"attributes",
"member_of",
"description",
"rolreplication",
]
assert headers == results[2]
assert row in results[1]
Expand Down

0 comments on commit c779e56

Please sign in to comment.