-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_schema.py
234 lines (175 loc) · 7.5 KB
/
update_schema.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from peewee import *
from playhouse.migrate import *
from app.models.models import *
from app.loadConfig import *
here = os.path.dirname(__file__)
cfg = load_config()
# print("db", db)
# mainDB = SqliteDatabase(cfg['databases']['dev'])
# my_db = SqliteDatabase(db,
# pragmas = ( ('busy_timeout', 100),
# ('journal_mode,WAL')
# ),
# threadlocals = True
# )
mainDB = MySQLDatabase ( cfg["db"]["db_name"], host = cfg["db"]["host"], user = cfg["db"]["username"], passwd = cfg["db"]["password"])
# Creates the class that will be used by Peewee to store the database
class dbModel (Model):
class Meta:
database = mainDB
migrator = MySQLMigrator(mainDB)
# my_db.drop_tables([RoomPreferences])
#TODO: make a function & wrap it up in try/catch statement so it doesn't break when tables are already there/aren't there
# def dropTables():
# tables = [Rooms, Building, EducationTech, RoomPreferences, CourseChange, ScheduleDays, Course]
# for table in tables:
# try:
# my_db.drop_tables([table])
# except:
# pass
# class TermStates(dbModel):
# csID = PrimaryKeyField()
# number = IntegerField(null = False)
# name = CharField(null = False)
# order = IntegerField(null = False)
# display_name = CharField(null = False)
# To add states to Temstates table
# state_1 = TermStates(number = 0, order = 0, name = "term_created", display_name = "Term Created").save()
# state_2 = TermStates(number = 1, order = 1, name = "schedule_opened", display_name = "Open Scheduling").save()
# state_3 = TermStates(number = 2, order = 2, name = "schedule_closed", display_name = "Lock Scheduling").save()
# state_3 = TermStates(number = 3, order = 3, name = "roomprefrences_opened", display_name = "Open Room Preferences").save()
# state_4 = TermStates(number = 4, order = 4, name = "roomprefrences_closed", display_name = "Lock Room Preferences").save()
# state_5 = TermStates(number = 5, order = 5, name = "rooms_assigned", display_name = "Assign Rooms").save()
# state_6 = TermStates(number = 6, order = 6, name = "term_finished", display_name = "Finish").save()
# state_7 = TermStates(number = 7, order = 7, name = "term_archived", display_name = "Archive").save()
# '''
# t = Term.select()
# for term in t:
# term.algorithm_running = False
# term.save()
# '''
# my_db.drop_tables([RoomPreferences])
# my_db.drop_tables([Building, EducationTech])
# my_db.drop_tables([Building, Rooms, EducationTech, RoomPreferences])
#TODO: make a function & wrap it up in try/catch statement so it doesn't break when tables are already there/aren't there
# def dropTables():
# tables = [Rooms, Building, EducationTech, RoomPreferences, CourseChange, ScheduleDays, Course]
# for table in tables:
# try:
# my_db.drop_tables([table])
# except:
# pass
# q = Course.select()
# for course in q:
# course.rid = None
# course.save()
# q = SpecialTopicCourse.select()
# for course in q:
# course.rid = None
# course.save()
#my_db.create_tables([RoomPreferences, EducationTech, Building, Rooms, Course, CourseChange, ScheduleDays])
#Add these columns to existing tables in the production
#Building column add
# migrate(
# migrator.add_column('Building,shortName', TextField(default='')),
# )
# #Rooms Column Add
# migrate(
# #migrator.add_column('Rooms,maxCapacity', IntegerField(null=False)), #update already exists
# migrator.add_column('Rooms,visualAcc', CharField(null=True)),
# migrator.add_column('Rooms,audioAcc', CharField(null=True)),
# migrator.add_column('Rooms,physicalAcc',CharField(null=True)),
# migrator.add_column('Rooms,educationTech_id', ForeignKeyField(EducationTech, to_field = EducationTech.eId, related_name='rooms', null=True)),
# migrator.add_column('Rooms,specializedEq', CharField(null=True)),
# migrator.add_column('Rooms,specialFeatures', CharField(null=True)),
# migrator.add_column('Rooms,movableFurniture', BooleanField(default=False)),
# )
# migrate(
# migrator.drop_column("Course", "rid"),
# migrator.add_column("Course", "rid_id", ForeignKeyField(Rooms, to_field = Rooms.rID, null = True, related_name='courses_rid'))
# )
# my_db.drop_tables([ScheduleDays])
# class ScheduleDays(dbModel):
# sdID = PrimaryKeyField()
# schedule = ForeignKeyField(BannerSchedule, null = True, related_name='course_schedule_days')
# day = CharField(null=True)
# my_db.create_tables([BuildingManager])
# bmanager = BuildingManager( username = "stamperf",
# bmid = 6
# ).save()
# migrate(
# migrator.add_column('Rooms,lastModified', CharField(null=True))
# migrator.add_column('RoomPreferences,priority', IntegerField(default=6)),
# migrator.add_column('Course,days_id', ForeignKeyField(ScheduleDays, to_field = ScheduleDays.sdID , null = True, related_name='course_days'))
# my_db.create_tables([ScheduleDays])
# my_db.create_tables([RoomPreferences])
# migrate(
# migrator.add_column('RoomPreferences,priority', IntegerField(default=6))
# # migrator.add_column('Course,days_id', ForeignKeyField(ScheduleDays, to_field = ScheduleDays.sdID , null = True, related_name='course_days'))
# # migrator.drop_not_null('CourseChange','rid')
# )
# my_db.drop_tables([ScheduleDays])
# class ScheduleDays(dbModel):
# sdID = PrimaryKeyField()
# schedule = ForeignKeyField(BannerSchedule, null = True, related_name='course_schedule_days')
# day = CharField(null=True)
# migrate(
# # migrator.add_column('RoomPreferences,priority', IntegerField(default=6)),
# # migrator.drop_column("Term", "state"),
# migrator.add_column('Term,term_state_id', ForeignKeyField(TermStates, to_field = TermStates.csID , default = 1, related_name='term_states')),
# # migrator.add_column('Term,algorithm_running', BooleanField(null = False, default = False))
# migrator.add_column('Term,editable', BooleanField(null = False, default = True))
# # migrator.drop_not_null('CourseChange','rid')
# )
# t = Term.select()
# for term in t:
# term.algorithm_running = False
# term.save()
# q = Course.select()
# for course in q:
# course.rid = None
# course.save()
# q = SpecialTopicCourse.select()
# for course in q:
# course.rid = None
# course.save()
# PART OF PR 265
# migrate(
# migrator.add_column("rooms", "lastModified", CharField(null = True)))
# try:
# mainDB.create_tables([CrossListed])
# except:
# print("Table Crosslisted already exists")
#
# try:
# migrate(
# migrator.add_column('rooms,lastModified', CharField(null=True)),
# )
# except:
# print("Column lastModified in Table Rooms already exists")
#
# try:
# migrate(
# migrator.add_column('course,parentCourse_id', ForeignKeyField(Course, to_field = Course.cId, null=True, default=True))
# )
# except:
# print("Column parentCourse_id in table Course already exists")
#PART OF PR XXX (ADD IMAGES TO EACH ROOM)
import os, json
try:
os.mkdir("app/static/images")
except:
pass
print("Start migrate")
try:
migrate(migrator.add_column('rooms','roomImageURL', CharField(null=True)),)
print("Column added")
except Exception as e:
print(e)
f = open("app/static/roomsMap.json") # Preloaded data from Judy
rms = json.load(f)
for rm in rms:
if rm["roomImageURL"] != "":
updateRoom = Rooms.get(Rooms.rID == rm["rID"])
updateRoom.roomImageURL = rm["roomImageURL"]
updateRoom.save()