-
Notifications
You must be signed in to change notification settings - Fork 21
/
update_schema.py
49 lines (36 loc) · 1.47 KB
/
update_schema.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
# coding: utf-8
#!/usr/bin/env python
import json
update_column_sql = """
-- cast column {table}.{column} to {cast}
BEGIN;
alter table {table} rename column {column} to {column}_x;
alter table {table} add column {column} {cast};
update {table} set {column} = {column}_x::{cast};
alter table {table} drop column {column}_x;
COMMIT;
"""
replace_empty_sql = """
--- update empty values for {table}.{column}
UPDATE {table} SET {column} = NULL WHERE {column} = '';
"""
def update_column(table, column, cast):
"""Привести данные в столбце к нужному типу данных."""
return update_column_sql.format(table=table, column=column, cast=cast)
def null(table, column):
"""Установить пустые значения в колонке в NULL."""
return replace_empty_sql.format(table=table, column=column)
if __name__=='__main__':
sql = []
spec = json.load(open('schema.json', 'r'))
tables_to_process = spec['process_tables']
for table, task in spec['schema'].items():
if table in tables_to_process:
sql.append(
"\n--============ {} ============--\n".format(table.upper()))
for column in task.get('null', []):
sql.append(null(table, column))
for column, cast in sorted(task.get('convert', {}).items()):
sql.append(update_column(table, column, cast))
with open('update_schema.sql', 'w') as f:
f.writelines(sql)