Parent: [d69cb3] (diff)

Download this file

migrations.py    545 lines (484 with data), 18.1 kB

  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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
from collections import defaultdict
from pylons import c
from flyway import Migration
import ming
from ming.orm import mapper, ORMSession, session, state
from allura import model as M
from allura.lib import plugin
from allura.ext.project_home import model as PM
from forgetracker import model as TM
from forgewiki import model as WM
from forgediscussion import model as DM
from forgegit import model as GitM
from forgehg import model as HgM
from forgesvn import model as SVNM
STATS_COLLECTION_SIZE=100000
class MigrateProjectsAndFiles(Migration):
version = 14
def up(self):
self._up_projects()
db = self.session.db
for collection in db.collection_names():
if collection.endswith('.files'):
self._up_collection(db, collection)
def down(self):
# Nothing to do, really, as long as we don't update
# any metadata while upgraded
pass
def _up_projects(self):
projects = self.session.db.project
for p in projects.find():
if p.get('database_uri').startswith('mongo://'):
p['database_uri'] = p['database_uri'].replace('mongo://', 'mongodb://')
projects.save(p)
def _up_collection(self, db, collection_name):
collection = db[collection_name]
# First, create a 'root' collection, clearing it out as well
root_collection = db[collection_name.split('.')[0]]
root_collection.remove({})
for doc in collection.find():
newdoc = dict(doc)
newdoc.update(doc['metadata'])
newdoc.pop('metadata')
newdoc['file_id'] = doc['_id']
for aid_name in ('page_id', 'ticket_id'):
if aid_name in newdoc:
newdoc['artifact_id'] = newdoc.pop(aid_name)
if 'post_id' in newdoc:
# post_id is stored along with artifact_id in posts
newdoc['artifact_id'] = newdoc['post_id']
root_collection.save(newdoc)
class CreateStatsCollection(Migration):
version = 13
def up(self):
if self.session.db.name == 'allura':
self.session.db.create_collection(
M.Stats.__mongometa__.name,
capped=True,
size=STATS_COLLECTION_SIZE*10,
max=STATS_COLLECTION_SIZE)
def down(self):
if self.session.db.name == 'allura':
self.session.db.drop_collection(
M.Stats.__mongometa__.name)
class DeleteFlashMailboxes(Migration):
version = 12
def up(self):
if self.session.db.name == 'allura':
self.ormsession.remove(
M.Mailbox,
{'type':'flash'})
def down(self):
raise NotImplementedError, 'ClearMailboxes.down'
class ClearMailboxes(Migration):
version = 11
def up(self):
if self.session.db.name == 'allura':
self.ormsession.remove(M.Mailbox, {})
self.ormsession.ensure_indexes(M.Mailbox)
def down(self):
raise NotImplementedError, 'ClearMailboxes.down'
class AddMountLabels(Migration):
version = 10
def up(self):
configs = self.ormsession.find(M.AppConfig).all()
for config in configs:
config.options['mount_label'] = config.options['mount_point']
self.ormsession.flush()
def down(self):
configs = self.ormsession.find(M.AppConfig).all()
for config in configs:
del config.options['mount_label']
self.ormsession.flush()
class UpdateThemeToOnyx(Migration):
version = 9
def up(self):
if self.session.db.name == 'allura':
theme = self.ormsession.find(M.Theme, {'name':'forge_default'}).first()
if not theme: return
theme.color1='#295d78'
theme.color2='#272727'
theme.color3='#454545'
theme.color4='#c3c3c3'
theme.color5='#d7d7d7'
theme.color6='#ebebeb'
self.ormsession.update_now(theme, state(theme))
self.ormsession.flush()
def down(self):
if self.session.db.name == 'allura':
theme = self.ormsession.find(M.Theme, {'name':'forge_default'}).first()
if not theme: return
theme.color1='#0088cc'
theme.color2='#000000'
theme.color3='#454545'
theme.color4='#6c7681'
theme.color5='#d8d8d8'
theme.color6='#ececec'
self.ormsession.update_now(theme, state(theme))
self.ormsession.flush()
class RemoveOldInitProjects(Migration):
version=8
def __init__(self, *args, **kwargs):
super(RemoveOldInitProjects, self).__init__(*args, **kwargs)
try:
c.project
except TypeError:
class EmptyClass(): pass
c._push_object(EmptyClass())
c.project = EmptyClass()
c.project._id = None
c.app = EmptyClass()
c.app.config = EmptyClass()
c.app.config.options = EmptyClass()
c.app.config.options.mount_point = None
def up(self):
self.ormsession.remove(M.Project, {'shortname':'--init--'})
self.ormsession.update(M.Project, {'shortname':'__init__'}, {'$set':{'shortname':'--init--'}})
def down(self):
pass # Do nothing
class UnderToDash(Migration):
version = 7
def __init__(self, *args, **kwargs):
super(UnderToDash, self).__init__(*args, **kwargs)
try:
c.project
except TypeError:
class EmptyClass(): pass
c._push_object(EmptyClass())
c.project = EmptyClass()
c.project._id = None
c.app = EmptyClass()
c.app.config = EmptyClass()
c.app.config.options = EmptyClass()
c.app.config.options.mount_point = None
def up(self):
def fixup(s):
return s.replace('_', '-')
fix_pathnames(self.ormsession,fixup)
def down(self):
pass # Do nothing
class MergeDuplicateRoles(Migration):
version = 6
def up(self):
if self.session.db.name == 'allura': self.up_allura()
else: self.up_project()
def down(self):
pass
def up_allura(self):
# Uniquify User.projects list
for u in self.session.find(self.User):
u.projects = list(set(u.projects))
self.session.save(u)
def up_project(self):
# Consolidate roles by user_id
roles_by_user = defaultdict(list)
for role in self.session.find(self.Role):
if role.get('user_id') is None: continue
roles_by_user[role.user_id].append(role)
for user_id, roles in roles_by_user.iteritems():
if len(roles) <= 1: continue
main_role = roles[0]
subroles = set()
for r in roles:
for sr_id in r.get('roles', []):
subroles.add(sr_id)
main_role.roles = list(subroles)
self.session.save(main_role)
for r in roles[1:]:
self.session.delete(r)
# Add index
try:
self.session.drop_indexes(self.Role)
except:
pass
self.session.ensure_indexes(self.Role)
class User(ming.Document):
class __mongometa__:
name='user'
class Role(ming.Document):
class __mongometa__:
name='user'
unique_indexes = [ ('user_id', 'name') ]
class UnifyPermissions(Migration):
version = 5
def up(self):
perm_owner = self.ormsession.find(M.ProjectRole, dict(name='owner')).first()
perm_Owner = self.ormsession.find(M.ProjectRole, dict(name='Owner')).first()
if perm_owner:
perm_owner.name = 'Admin'
elif perm_Owner:
perm_Owner.name = 'Admin'
perm_member = self.ormsession.find(M.ProjectRole, dict(name='member')).first()
if perm_member:
perm_member.name = 'Member'
perm_developer = self.ormsession.find(M.ProjectRole, dict(name='developer')).first()
if perm_developer:
perm_developer.name = 'Developer'
if self.session.db.name != 'allura':
role_names = [r.name for r in self.ormsession.find(M.ProjectRole)]
if len(role_names) and 'Admin' not in role_names:
new_admin = M.ProjectRole(name='Admin')
if len(role_names) and 'Developer' not in role_names:
new_admin = M.ProjectRole(name='Developer')
self.ormsession.flush()
def down(self):
perm_Admin = self.ormsession.find(M.ProjectRole, dict(name='Admin')).first()
if perm_Admin:
perm_Admin.name = 'Owner'
self.ormsession.flush()
class UpdateProjectsToTools(Migration):
version = 4
def up(self):
# if self.session.db.name == 'allura':
# import pdb; pdb.set_trace()
rename_key(self.session,M.User,'plugin_preferences','tool_preferences')
rename_key(self.session,M.AppConfig,'plugin_name','tool_name')
rename_key(self.session,M.ArtifactLink,'plugin_name','tool_name')
rename_key(self.session,M.Project,'plugin','tool',inside='acl')
if c.app:
c.app.__version__ = '0.1'
c.app.config.tool_name = 'project_home'
for pc in self.ormsession.find(PM.PortalConfig, {}):
for div in pc.layout:
for widget in div.content:
if widget.widget_name == 'plugin_status':
widget.widget_name = 'tool_status'
self.ormsession.flush()
# fix artifacts
for cls in (
M.Artifact,
M.VersionedArtifact,
M.Snapshot,
M.Message,
M.Post,
M.AwardGrant,
M.Discussion,
M.Award,
M.Thread,
M.Post,
M.PostHistory,
DM.Forum,
DM.ForumPost,
DM.forum.ForumPostHistory,
DM.ForumThread,
WM.Page,
WM.wiki.PageHistory,
TM.Bin,
TM.Ticket,
TM.ticket.TicketHistory,
PM.PortalConfig,
GitM.GitRepository,
HgM.HgRepository,
SVNM.SVNRepository,
):
rename_key(self.session,cls,'plugin_verson','tool_version')
def down(self):
# if self.session.db.name == 'allura':
rename_key(self.session,M.User,'tool_preferences','plugin_preferences')
rename_key(self.session,M.AppConfig,'tool_name','plugin_name')
rename_key(self.session,M.ArtifactLink,'tool_name','plugin_name')
rename_key(self.session,M.Project,'tool','plugin',inside='acl')
if c.app:
c.app.__version__ = '0.1'
c.app.config.tool_name = 'project_home'
for pc in self.ormsession.find(PM.PortalConfig, {}):
for div in pc.layout:
for widget in div.content:
if widget.widget_name == 'tool_status':
widget.widget_name = 'plugin_status'
self.ormsession.flush()
# fix artifacts
for cls in (
M.Artifact,
M.VersionedArtifact,
M.Snapshot,
M.Message,
M.Post,
M.AwardGrant,
M.Discussion,
M.Award,
M.Thread,
M.Post,
M.PostHistory,
DM.Forum,
DM.ForumPost,
DM.forum.ForumPostHistory,
DM.ForumThread,
WM.Page,
WM.wiki.PageHistory,
GitM.GitRepository,
HgM.HgRepository,
SVNM.SVNRepository,
TM.Bin,
TM.Ticket,
TM.ticket.TicketHistory,
PM.PortalConfig,
):
rename_key(self.session,cls,'tool_version','plugin_verson')
class UpdateThemeToShinyBook(Migration):
version = 3
def up(self):
if self.session.db.name == 'allura':
theme = self.ormsession.find(M.Theme, {'name':'forge_default'}).first()
if not theme: return
theme.color1='#0088cc'
theme.color2='#000000'
theme.color3='#454545'
theme.color4='#6c7681'
theme.color5='#d8d8d8'
theme.color6='#ececec'
self.ormsession.update_now(theme, state(theme))
self.ormsession.flush()
def down(self):
if self.session.db.name == 'allura':
theme = self.ormsession.find(M.Theme, {'name':'forge_default'}).first()
if not theme: return
theme.color1='#104a75'
theme.color2='#aed0ea'
theme.color3='#EDF3FB'
theme.color4='#D7E8F5'
theme.color5='#000'
theme.color6='#000'
self.ormsession.update_now(theme, state(theme))
self.ormsession.flush()
class RenameNeighborhoods(Migration):
version = 2
def up(self):
n_users = self.ormsession.find(M.Neighborhood, dict(name='Users')).first()
n_projects = self.ormsession.find(M.Neighborhood, dict(name='Projects')).first()
if n_users:
n_users.url_prefix = '/u/'
n_users.shortname_prefix = 'u/'
if n_projects:
n_projects.url_prefix = '/p/'
for p in self.ormsession.find(M.Project, {}):
if p.shortname.startswith('users/'):
p.shortname = p.shortname.replace('users/', 'u/')
self.ormsession.flush()
def down(self):
n_users = self.ormsession.find(M.Neighborhood, dict(name='Users')).first()
n_projects = self.ormsession.find(M.Neighborhood, dict(name='Projects')).first()
if n_users:
n_users.url_prefix = '/users/'
n_users.shortname_prefix = 'users/'
if n_projects:
n_projects.url_prefix = '/projects/'
for p in self.ormsession.find(M.Project, {}):
if p.shortname.startswith('u/'):
p.shortname = p.shortname.replace('u/', 'users/')
self.ormsession.flush()
class DowncaseMountPoints(Migration):
version = 1
def __init__(self, *args, **kwargs):
super(DowncaseMountPoints, self).__init__(*args, **kwargs)
try:
c.project
except TypeError:
class EmptyClass(): pass
c._push_object(EmptyClass())
c.project = EmptyClass()
c.project._id = None
c.app = EmptyClass()
c.app.config = EmptyClass()
c.app.config.options = EmptyClass()
c.app.config.options.mount_point = None
def up_requires(self):
yield ('ForgeWiki', 0)
yield ('ForgeTracker', 0)
yield ('pyforge', 0)
def up(self):
fix_pathnames(self.ormsession, lambda s:s.lower().replace(' ', '_'))
def down(self):
pass # Do nothing
class V0(Migration):
version = 0
def up(self): pass
def down(self): pass
def rename_key(session, cls, old_key, new_key, inside=None):
pm = session._impl(mapper(cls).doc_cls)
for item in pm.find():
if inside:
if inside in item and old_key in item[inside]:
item[inside][new_key] = item[inside][old_key]
del item[inside][old_key]
else:
if old_key in item:
item[new_key] = item[old_key]
del item[old_key]
pm.save(item)
def fix_pathnames(ormsession, mutator):
def fix_aref(aref):
if aref and aref.mount_point:
aref.mount_point = mutator(aref.mount_point)
# Fix neigborhoods
for n in ormsession.find(M.Neighborhood, {}):
n.shortname_prefix = mutator(n.shortname_prefix)
# Fix Projects
for p in ormsession.find(M.Project, {}):
p.shortname = mutator(p.shortname)
# Fix AppConfigs
for ac in ormsession.find(M.AppConfig, {}):
ac.options.mount_point = mutator(ac.options.mount_point)
if ac.tool_name == 'Forum':
ac.tool_name = 'Discussion'
ormsession.flush(); ormsession.clear()
# Fix ArtifactLinks
for al in ormsession.find(M.ArtifactLink, {}):
fix_aref(al.artifact_reference)
# Fix feeds
for f in ormsession.find(M.Feed, {}):
fix_aref(f.artifact_reference)
# Fix notifications
for n in ormsession.find(M.Notification, {}):
fix_aref(n.artifact_reference)
# Fix tags
for n in ormsession.find(M.TagEvent, {}):
fix_aref(n.artifact_ref)
for n in ormsession.find(M.UserTags, {}):
fix_aref(n.artifact_reference)
for n in ormsession.find(M.Tag, {}):
fix_aref(n.artifact_ref)
# fix PortalConfig
for pc in ormsession.find(PM.PortalConfig):
for layout in pc.layout:
for w in layout.content:
w.mount_point = mutator(w.mount_point)
# Fix thread (has explicit artifact_reference property)
for t in ormsession.find(M.Thread, {}):
fix_aref(t.artifact_reference)
for t in ormsession.find(DM.ForumThread, {}):
fix_aref(t.artifact_reference)
ormsession.flush(); ormsession.clear()
# fix artifacts
for cls in (
M.Artifact,
M.VersionedArtifact,
M.Snapshot,
M.Message,
M.Post,
M.AwardGrant,
M.Discussion,
M.Award,
M.Thread,
M.Post,
M.PostHistory,
DM.Forum,
DM.ForumPost,
DM.forum.ForumPostHistory,
DM.ForumThread,
WM.Page,
WM.wiki.PageHistory,
GitM.GitRepository,
HgM.HgRepository,
SVNM.SVNRepository,
TM.Bin,
TM.Ticket,
TM.ticket.TicketHistory,
PM.PortalConfig,
):
for obj in ormsession.find(cls, {}):
for ref in obj.references:
fix_aref(ref)
for ref in obj.backreferences.itervalues():
fix_aref(ref)
ormsession.flush(); ormsession.clear()