-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_migration.py
131 lines (103 loc) · 2.98 KB
/
db_migration.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
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
"""Requires fully set up mgdbv3 and access to old database"""
import os
import sys
from datetime import timedelta
import psycopg2
import psycopg2.extras
from django.contrib.auth.models import User
# setup django
sys.path.append('../')
os.environ['DJANGO_SETTINGS_MODULE'] = 'sprechstundensystem.settings'
import django # noqa E402, need settings module before setting up django
django.setup()
from management.models import ( # noqa E402
Admin,
Appointment,
Settings
)
# CHANGE SETTINGS HERE
old_server = "dbname='sss' user='sss' host='localhost' password='???'"
DEBUG = True
def load_admins(c):
c.execute("""select * from persons""")
admins = c.fetchall()
adms = []
for admin in admins:
adms.append(Admin(
pk=admin['id'],
first_name=admin['first_name'],
last_name=admin['last_name'],
email=admin['email'],
))
if not DEBUG:
Admin.objects.bulk_create(adms)
def load_appointments(c):
c.execute("""select * from appointments""")
appointments = c.fetchall()
apps = []
for appointment in appointments:
start_time = appointment['time']
end_time = start_time + timedelta(minutes=appointment['duration'])
apps.append(Appointment(
pk=appointment['id'],
start_time=start_time,
end_time=end_time,
reminder_sent=appointment['reminder_sent']
))
if not DEBUG:
Appointment.objects.bulk_create(apps)
def load_settings(c):
Settings.objects.create(
name=Settings.SETTING_SENDER,
value='Leo',
active=True
)
Settings.objects.create(
name=Settings.SETTING_MAILING_LIST,
value='admins@stusta.net',
active=True
)
Settings.objects.create(
name=Settings.SETTING_APPOINTMENT_LOCATION,
value='Haus 10, Raum 0002',
active=True
)
Settings.objects.create(
name=Settings.SETTING_REMINDER_NOTE,
value='',
active=True
)
def load_users(c):
c.execute("""select * from users""")
users = c.fetchall()
usrs = []
for user in users:
u = User(
pk=user['id'],
username=user['username'],
email=user['email'],
is_staff=True,
password=user['password']
)
usrs.append(u)
if not DEBUG:
User.objects.bulk_create(usrs)
def load_many_to_many(c):
c.execute("""select * from users""")
m2m = c.fetchall()
for entry in m2m:
appointment = Appointment.objects.get(pk=entry['appointment'])
admin = Admin.objects.get(pk=entry['person'])
if not DEBUG:
appointment.admins.add(admin)
def main():
con = psycopg2.connect(old_server)
cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
load_admins(cur)
load_appointments(cur)
# load_users(cur)
load_settings(cur)
load_many_to_many(cur)
if __name__ == '__main__':
main()