-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_diff.py
86 lines (72 loc) · 2.37 KB
/
pg_diff.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
import enum
import re
import sys
class DBChanges(enum.StrEnum):
ColumnAdded = "column added"
ColumnDeleted = "column deleted"
ColumnChanged = "column changed"
TableAdded = "table added"
TableDeleted = "table deleted"
TableChanged = "table changed"
TOC = {
"Missing Column(s)": DBChanges.ColumnAdded,
"Unexpected Column(s)": DBChanges.ColumnDeleted,
"Changed Column(s)": DBChanges.ColumnChanged,
"Missing Table(s)": DBChanges.TableAdded,
"Unexpected Table(s)": DBChanges.TableDeleted,
"Changed Table(s)": DBChanges.TableChanged,
}
HEADING_PATTERN = r"(\w+.\w+\(s\)):"
COLUMN_PATTERN = r"^\s{5}public.(\w+).(\w+)"
TABLE_PATTERN = r"^\s{5}(\w+)"
current_type = None
columns_changes = {}
tables_changes = {}
for line in sys.stdin.readlines():
heading = re.match(HEADING_PATTERN, line)
if heading:
current_type = TOC.get(heading.group(1))
continue
match current_type:
case DBChanges.ColumnAdded | DBChanges.ColumnDeleted | DBChanges.ColumnChanged:
column_name = re.match(COLUMN_PATTERN, line)
columns_changes.setdefault(current_type, {}).setdefault(
column_name.group(1),
[],
).append(column_name.group(2))
case DBChanges.TableAdded | DBChanges.TableDeleted | DBChanges.TableChanged:
table_name = re.match(TABLE_PATTERN, line)
if table_name:
tables_changes.setdefault(
current_type,
[],
).append(table_name.group(1))
sys.stdout.write("*" * 100 + "\n")
sys.stdout.write(" " * 42 + "DB changes report\n")
sys.stdout.write("*" * 100 + "\n\n")
ignore_tables = set()
for change_type, tables in tables_changes.items():
ignore_tables.update(tables)
sys.stdout.writelines(
[
"- {0} `{1}`\n".format(
change_type.value,
table,
)
for table in tables
],
)
for change_type, tables in columns_changes.items():
for table, columns in tables.items():
if table in ignore_tables:
continue
sys.stdout.writelines(
[
"- {0} `{1}` table `{2}`\n".format(
change_type.value,
column,
table,
)
for column in columns
],
)