-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
executable file
·66 lines (58 loc) · 2.01 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Functions for finding and displaying tables and columns.
Mostly leverages the psql client.
"""
import subprocess
import util
def search(
patterns, expanded_display=False, tuples_only=False, most_recent=False, wrap=False,
null_desc_only=False):
"""
Find tables or columns matching passed regular expressions.
Print according to given formatting options.
"""
if len(patterns) not in (1, 2):
raise ValueError("Too many patterns provided to search function")
psql_command = "psql -q << EOF\n"
psql_command += f"SET SEARCH_PATH = { util.USER_CONFIG['schema'] },public;\n"
if expanded_display:
psql_command += "\\x\n"
if tuples_only:
psql_command += "\\t\n"
if wrap:
psql_command += \
"\pset format wrapped\n\pset columns 150" \
# pylint: disable=anomalous-backslash-in-string
null_desc_clause = ""
if null_desc_only:
null_desc_clause = "AND description IS NULL"
table_pattern = patterns[0]
if len(patterns) == 1:
order_clause = "ORDER BY table_schema, table_name"
if most_recent:
order_clause = "ORDER BY inserted_at DESC"
psql_command += f"""
SELECT
table_schema, table_name, description, docs_approved AS aprvd
FROM tables
WHERE table_name ~ '{table_pattern}'
{null_desc_clause}
{order_clause}
;
"""
elif len(patterns) == 2:
column_pattern = patterns[1]
order_clause = "ORDER BY table_name, column_name"
if most_recent:
order_clause = "ORDER BY inserted_at DESC"
psql_command += f"""
SELECT table_schema, table_name, column_name, description
FROM columns
WHERE table_name ~ '{table_pattern}'
AND column_name ~ '{column_pattern}'
{null_desc_clause}
{order_clause}
;
"""
psql_command += "\nEOF"
subprocess.call(psql_command, shell=True)