-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_with_upstream.py
98 lines (85 loc) · 3 KB
/
sync_with_upstream.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
Syncs documentation tables with upstream data tables.
"""
import util
def update_table_list():
"""
Look for new tables upstream that don't exist in documentation tables, and add them.
"""
query_params = []
query_params += list(util.USER_CONFIG["table_ignore_patterns"])
query_params.append(tuple(util.USER_CONFIG["schemas_to_ignore"]))
query = f"""
INSERT INTO tables (table_schema, table_name) (
SELECT table_schema, table_name
FROM information_schema.columns
WHERE 1 = 1
{ util.TABLE_IGNORE_STRING }
AND table_schema NOT IN %s
)
ON CONFLICT DO NOTHING;
"""
util.cursor.execute(query, tuple(query_params))
num_inserted_rows = util.cursor.rowcount
print("%i rows inserted" % num_inserted_rows)
util.db_conn.commit()
def update_column_list():
"""
Insert rows for columns that exist in data tables but not in documentation tables
"""
query_params = []
query_params += list(util.USER_CONFIG["table_ignore_patterns"])
query_params.append(tuple(util.USER_CONFIG["schemas_to_ignore"]))
query = f"""
INSERT INTO columns (table_schema, table_name, column_name, data_type) (
SELECT table_schema, table_name, column_name, data_type
FROM information_schema.columns
WHERE 1 = 1
{ util.TABLE_IGNORE_STRING }
AND table_schema NOT IN %s
AND table_name IN (SELECT table_name FROM tables)
)
ON CONFLICT DO NOTHING;
"""
util.cursor.execute(query, tuple(query_params))
num_inserted_rows = util.cursor.rowcount
print("%i rows inserted" % num_inserted_rows)
util.db_conn.commit()
def update_orphans():
"""
Marks tables/columns as orphaned, if their corresponding upstream entry has disappeared.
"""
table_query = f"""
WITH isc AS (
SELECT DISTINCT table_schema, table_name
FROM information_schema.columns
)
UPDATE tables t
SET orphaned = True
WHERE (orphaned IS NOT True) AND NOT EXISTS (
SELECT * FROM isc
WHERE table_name = t.table_name
AND table_schema = t.table_schema);
"""
util.cursor.execute(table_query)
num_updated_rows = util.cursor.rowcount
print("%i tables marked as orphaned" % num_updated_rows)
util.db_conn.commit()
column_query = """
WITH isc AS (
SELECT DISTINCT table_schema, table_name, column_name
FROM information_schema.columns
)
UPDATE columns t
SET orphaned = True
WHERE (orphaned IS NOT True) AND NOT EXISTS (
SELECT * FROM isc
WHERE table_name = t.table_name
AND table_schema = t.table_schema
AND column_name = t.column_name
);
"""
util.cursor.execute(column_query)
num_updated_rows = util.cursor.rowcount
print("%i columns marked as orphaned" % num_updated_rows)
util.db_conn.commit()