1
- import os
2
1
import json
2
+ import os
3
3
from datetime import datetime
4
4
5
5
old_schemas_dir = "original_schemas"
13
13
print (f"Directory { new_schemas_dir } does not exist." )
14
14
exit (1 )
15
15
16
+
16
17
def read_json_file (filepath ):
17
- with open (filepath , 'r' ) as f :
18
+ with open (filepath , "r" ) as f :
18
19
return json .load (f )
19
20
21
+
20
22
new_schemas = [file .replace ("_schema.json" , "" ) for file in os .listdir (new_schemas_dir )]
21
23
old_schemas = [file .replace ("_schema.json" , "" ) for file in os .listdir (old_schemas_dir )]
22
24
@@ -27,36 +29,37 @@ def read_json_file(filepath):
27
29
schema_changes = {}
28
30
29
31
for schema in common_tables :
30
- old_file_path = os .path .join (old_schemas_dir , schema + ' _schema.json' )
31
- new_file_path = os .path .join (new_schemas_dir , schema + ' _schema.json' )
32
+ old_file_path = os .path .join (old_schemas_dir , schema + " _schema.json" )
33
+ new_file_path = os .path .join (new_schemas_dir , schema + " _schema.json" )
32
34
33
35
new_data = read_json_file (new_file_path )
34
36
old_data = read_json_file (old_file_path )
35
37
36
- old_dict = {item [' name' ]: item for item in old_data }
37
- new_dict = {item [' name' ]: item for item in new_data }
38
+ old_dict = {item [" name" ]: item for item in old_data }
39
+ new_dict = {item [" name" ]: item for item in new_data }
38
40
39
- added = [new_dict [name ][' name' ] for name in new_dict if name not in old_dict ]
40
- deleted = [old_dict [name ][' name' ] for name in old_dict if name not in new_dict ]
41
+ added = [new_dict [name ][" name" ] for name in new_dict if name not in old_dict ]
42
+ deleted = [old_dict [name ][" name" ] for name in old_dict if name not in new_dict ]
41
43
type_changed = [
42
- (new_dict [name ]['name' ], new_dict [name ]['type' ], old_dict [name ]['type' ]) for name in new_dict
43
- if name in old_dict and new_dict [name ]['type' ] != old_dict [name ]['type' ]
44
+ (new_dict [name ]["name" ], new_dict [name ]["type" ], old_dict [name ]["type" ])
45
+ for name in new_dict
46
+ if name in old_dict and new_dict [name ]["type" ] != old_dict [name ]["type" ]
44
47
]
45
48
46
49
if added :
47
50
if schema not in schema_changes :
48
51
schema_changes [schema ] = {}
49
- schema_changes [schema ][' column_added' ] = added
52
+ schema_changes [schema ][" column_added" ] = added
50
53
51
54
if deleted :
52
55
if schema not in schema_changes :
53
56
schema_changes [schema ] = {}
54
- schema_changes [schema ][' column_removed' ] = deleted
57
+ schema_changes [schema ][" column_removed" ] = deleted
55
58
56
59
if type_changed :
57
60
if schema not in schema_changes :
58
61
schema_changes [schema ] = {}
59
- schema_changes [schema ][' type_changed' ] = type_changed
62
+ schema_changes [schema ][" type_changed" ] = type_changed
60
63
61
64
if tables_added or tables_removed or schema_changes :
62
65
current_date = datetime .now ().strftime ("%Y-%m-%d" )
@@ -73,15 +76,19 @@ def read_json_file(filepath):
73
76
print ("### Tables Removed:" )
74
77
print ([table for table in tables_removed ])
75
78
79
+
76
80
def sort_schema_changes (changes ):
77
81
sorted_data = {}
78
82
79
83
for table_name in sorted (changes .keys ()):
80
- sorted_operations = {op_type : sorted (changes [table_name ][op_type ])
81
- for op_type in sorted (changes [table_name ].keys ())}
84
+ sorted_operations = {
85
+ op_type : sorted (changes [table_name ][op_type ])
86
+ for op_type in sorted (changes [table_name ].keys ())
87
+ }
82
88
sorted_data [table_name ] = sorted_operations
83
89
return sorted_data
84
90
91
+
85
92
if schema_changes :
86
93
sorted_schema_changes = sort_schema_changes (schema_changes )
87
94
print ("" )
@@ -92,9 +99,13 @@ def sort_schema_changes(changes):
92
99
93
100
for table_name , operations in sorted_schema_changes .items ():
94
101
for operation , columns in operations .items ():
95
- if operation in [' column_added' , ' column_removed' ]:
102
+ if operation in [" column_added" , " column_removed" ]:
96
103
columns_str = ", " .join (columns )
97
- if operation in ['type_changed' ]:
98
- columns_str = ", " .join ([f"{ column [0 ]} ({ column [2 ]} -> { column [1 ]} )" for column in columns ])
99
- markdown_table += f"| { table_name :<33} | { operation :<13} | { columns_str :<24} |\n "
104
+ if operation in ["type_changed" ]:
105
+ columns_str = ", " .join (
106
+ [f"{ column [0 ]} ({ column [2 ]} -> { column [1 ]} )" for column in columns ]
107
+ )
108
+ markdown_table += (
109
+ f"| { table_name :<33} | { operation :<13} | { columns_str :<24} |\n "
110
+ )
100
111
print (markdown_table )
0 commit comments