-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanage.py
72 lines (60 loc) · 2.31 KB
/
manage.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
# @author : coenni
# @date : Jan 03 2017
# @brief : a simple application to migrate database from pg to mysql
import os, pprint
from sqlalchemy import create_engine, exc
from sqlalchemy.orm import sessionmaker
model_file = 'db.py'
model_file_parse = 'db2.py'
db_name = 'socarho'
def clean_next_val():
with open(model_file, "rt") as fin:
with open(model_file_parse, "wt") as fout:
for line in fin:
if 'server_default' in line:
line = line.split(', server_default', 1)[0] + ')\n'
fout.write(line)
def create_mysql():
engine = create_engine('mysql+pymysql://username_mysql:[email protected]:3306')
connection = engine.connect()
result_create = connection.execute("create database IF NOT EXISTS "+db_name)
connection.close()
engine = create_engine('mysql+pymysql://username_mysql:[email protected]:3306/'+db_name)
connection = engine.connect()
try:
import db2
db2.Base.metadata.create_all(engine)
except exc.SQLAlchemyError as err:
print("Unexpected error:", err)
def get_class_by_tablename(tablename):
"""Return class reference mapped to table.
:param tablename: String with name of table.
:return: Class reference or None.
"""
import db2
for c in db2.Base._decl_class_registry.values():
if hasattr(c, '__tablename__') and c.__tablename__ == tablename:
return c
def move_data():
engine_pg = create_engine('postgresql://postgres:[email protected]:5432/ICM_DB_K_New')
engine_pg.connect()
Session_pg = sessionmaker(bind=engine_pg)
session_pg = Session_pg()
import db2
for table in db2.metadata.tables:
print(table)
table_class = get_class_by_tablename(table)
result_pg = session_pg.query(table_class).all()
engine_mysql = create_engine('mysql+pymysql://root:[email protected]:3306/socarho')
engine_mysql.connect()
Session_mysql = sessionmaker(bind=engine_mysql)
session_mysql = Session_mysql()
for row in result_pg:
session_mysql.merge(row)
pprint.pprint(row)
session_mysql.commit()
print(table+' finished')
os.system('sqlacodegen postgresql://postgres:[email protected]/ICM_DB_K_New --outfile '+model_file)
clean_next_val()
create_mysql()
move_data()