-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataBaseSchemaSync.py
59 lines (49 loc) · 1.6 KB
/
DataBaseSchemaSync.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
# coding: utf-8
from pymysql import OperationalError
import sys
import os
import json
import shutil
from db.MysqlDatabaseConnector import MysqlDatabaseConnector
from service.SchemaComparision import SchemaComparison
from util.Configer import Configer
class DataBaseSchemaSync(object):
"""
:type db_config: dict
:type source: entity.MysqlDatabaseConnector
:type target: entity.MysqlDatabaseConnector
"""
def __init__(self, conf_path=None):
try:
configure = Configer(conf_path)
except IOError:
sys.stderr.write('configure file not exist.')
sys.exit(1)
self.db_config = json.loads(configure.config)
try:
self.source = MysqlDatabaseConnector(self.db_config['source'])
self.source.fetch()
except OperationalError:
sys.stderr.write('database source connect error.')
sys.exit(1)
try:
self.target = MysqlDatabaseConnector(self.db_config['target'])
self.target.fetch()
except OperationalError:
sys.stderr.write('database target connect error.')
sys.exit(1)
def compare(self):
if os.path.exists('update'):
shutil.rmtree('update')
os.mkdir('update')
sc = SchemaComparison(self.source, self.target)
sc.compare()
if __name__ == '__main__':
foo = DataBaseSchemaSync()
# for table in foo.source.database.tables:
# print(table.name)
if len(sys.argv) <= 1:
foo = DataBaseSchemaSync()
else:
foo = DataBaseSchemaSync(sys.argv[1])
foo.compare()