Parent: [5d0554] (diff)

Child: [d69cb3] (diff)

Download this file

migrations.py    73 lines (58 with data), 2.3 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
import cPickle as pickle
from itertools import chain
import pymongo
from ming.orm import state
from pylons import c
from flyway import Migration
from allura.model import Thread, AppConfig, ArtifactReference
from forgewiki.model import Page
class WikiMigration(Migration):
def __init__(self, *args, **kwargs):
super(WikiMigration, 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
class V0(WikiMigration):
'''Migrate Thread.artifact_id to Thread.artifact_reference'''
version = 0
def up(self):
for pg in self.ormsession.find(Page):
q1 = self.ormsession.find(Thread, dict(artifact_id=pg._id))
q2 = self.ormsession.find(Thread, {'artifact_reference.artifact_id':pg._id})
for t in chain(q1, q2):
t.artifact_reference = self._dump_ref(pg)
t.artifact_id = None
self.ormsession.update_now(t, state(t))
self.ormsession.flush()
def down(self):
for pg in self.ormsession.find(Page):
for t in self.ormsession.find(Thread, dict(artifact_reference=self._dump_ref(pg))):
t.artifact_id = pg._id
t.artifact_reference = None
self.ormsession.update_now(t, state(t))
self.ormsession.flush()
def _dump_ref(self, art):
app_config = self.ormsession.get(AppConfig, art.app_config_id)
return ArtifactReference(dict(
project_id=app_config.project_id,
mount_point=app_config.options.mount_point,
artifact_type=pymongo.bson.Binary(pickle.dumps(art.__class__)),
artifact_id=art._id))
class AddDeletedAttribute(WikiMigration):
version = 1
def up(self):
for pg in self.ormsession.find(Page):
pg.deleted = False
self.ormsession.flush()
def down(self):
for pg in self.ormsession.find(Page):
del pg.deleted
self.ormsession.flush()