This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_migrate.py
executable file
·120 lines (96 loc) · 3.85 KB
/
db_migrate.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
Migrate data from 'db_filename' to app database URL
"""
import datetime
import logging
import sqlite3
from app import models
from manage import app, db
logging.basicConfig(level=logging.INFO)
date_format = '%Y-%m-%d %H:%M:%S.%f'
db_filename = 'dump.sqlite'
def lesson_handler(row):
res = dict(row)
res['has_changed'] = bool(res['has_changed'])
for f in ['start', 'end']:
res[f] = datetime.datetime.strptime(res[f], date_format)
return res
def user_handler(row):
res = dict(row)
res['confirmed'] = bool(res['confirmed'])
for f in ['last_seen', 'member_since']:
res[f] = datetime.datetime.strptime(res[f], date_format)
return res
def feed_handler(row):
res = dict(row)
res['timestamp'] = datetime.datetime.strptime(res['timestamp'], date_format)
return res
with sqlite3.connect(db_filename) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
with app.app_context():
logging.info('Migrating "locations" ...')
cursor.execute("SELECT * FROM locations")
db.engine.execute(
models.Location.__table__.insert(),
[dict(row) for row in cursor.fetchall()])
logging.info('Migrating "classrooms" ...')
cursor.execute("SELECT * FROM classrooms")
db.engine.execute(
models.Classroom.__table__.insert(),
[dict(row) for row in cursor.fetchall()])
logging.info('Migrating "professors" ...')
cursor.execute("SELECT * FROM professors")
db.engine.execute(
models.Professor.__table__.insert(),
[dict(row) for row in cursor.fetchall()])
logging.info('Migrating "degrees" ...')
cursor.execute("SELECT * FROM degrees")
db.engine.execute(
models.Degree.__table__.insert(),
[dict(row) for row in cursor.fetchall()])
logging.info('Migrating "calendars" ...')
cursor.execute("SELECT * FROM calendars")
db.engine.execute(
models.Calendar.__table__.insert(),
[dict(row) for row in cursor.fetchall()])
logging.info('Migrating "courses" ...')
cursor.execute("SELECT * FROM courses")
db.engine.execute(
models.Course.__table__.insert(),
[dict(row) for row in cursor.fetchall()])
logging.info('Migrating "curriculums" ...')
cursor.execute("SELECT * FROM curriculums")
db.engine.execute(
models.Curriculum.__table__.insert(),
[dict(row) for row in cursor.fetchall()])
logging.info('Migrating "lessons" ...')
cursor.execute("SELECT * FROM lessons")
db.engine.execute(
models.Lesson.__table__.insert(),
[lesson_handler(row) for row in cursor.fetchall()])
logging.info('Migrating "held_at" ...')
cursor.execute("SELECT * FROM held_at")
db.engine.execute(
models.held_at.insert(),
[dict(row) for row in cursor.fetchall()])
logging.info('Migrating "courses_curriculums" ...')
cursor.execute("SELECT * FROM courses_curriculums")
db.engine.execute(
models.courses_curriculums.insert(),
[dict(row) for row in cursor.fetchall()])
logging.info('Migrating "users" ...')
cursor.execute("SELECT * FROM users")
db.engine.execute(
models.User.__table__.insert(),
[user_handler(row) for row in cursor.fetchall()])
logging.info('Migrating "follows" ...')
cursor.execute("SELECT * FROM follows")
db.engine.execute(
models.follows.insert(),
[dict(row) for row in cursor.fetchall()])
logging.info('Migrating "feeds" ...')
cursor.execute("SELECT * FROM feeds")
db.engine.execute(
models.Feed.__table__.insert(),
[feed_handler(row) for row in cursor.fetchall()])