-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.py
More file actions
21 lines (17 loc) · 851 Bytes
/
migrate.py
File metadata and controls
21 lines (17 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from mysql_connector import connect_mysql, fetch_data
from mongo_connector import connect_mongo, insert_data
from schema_transform import transform_data
def migrate_data(mysql_host, mysql_user, mysql_password, mysql_db, mongo_host, mongo_port, mongo_db):
mysql_conn = connect_mysql(mysql_host, mysql_user, mysql_password, mysql_db)
mongo_conn = connect_mongo(mongo_host, mongo_port, mongo_db)
result = {}
with mysql_conn:
with mysql_conn.cursor() as cursor:
cursor.execute("SHOW TABLES")
tables = [row[0] for row in cursor.fetchall()]
for table in tables:
columns, rows = fetch_data(mysql_conn, table)
transformed_data = transform_data(columns, rows)
mongo_conn[table].insert_many(transformed_data)
result[table] = len(rows)
return result