-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_load.py
58 lines (31 loc) · 1.36 KB
/
dump_load.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
from models.models import SessionLocal, Customers, Orders, Items
from sqlalchemy.ext.serializer import loads, dumps
from sqlalchemy import Row
def dump():
with SessionLocal() as session:
with open('items_file', 'wb') as file_handler:
file_handler.write(dumps(session.query(Items).all()))
with open('orders_file', 'wb') as file_handler:
file_handler.write(dumps(session.query(Orders).all()))
with open('customers_file', 'wb') as file_handler:
file_handler.write(dumps(session.query(Customers).all()))
def load():
with SessionLocal() as session:
with open('items_file', 'rb') as file_handler:
for row in loads(file_handler.read()):
if isinstance(row, Row):
row = Items(**row._asdict())
session.merge(row)
with open('orders_file', 'rb') as file_handler:
for row in loads(file_handler.read()):
if isinstance(row, Row):
row = Orders(**row._asdict())
session.merge(row)
with open('customers_file', 'rb') as file_handler:
for row in loads(file_handler.read()):
if isinstance(row, Row):
row = Customers(**row._asdict())
session.merge(row)
session.commit()
#dump()
load()