Child: [d69cb3] (diff)

Download this file

migrations.py    169 lines (137 with data), 6.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
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, ProjectRole
from forgetracker.model import Ticket, Globals
class TrackerMigration(Migration):
def __init__(self, *args, **kwargs):
super(TrackerMigration, self).__init__(*args, **kwargs)
try:
c.project
except (TypeError, AttributeError), exc:
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(TrackerMigration):
'''Migrate Thread.artifact_id to Thread.artifact_reference'''
version = 0
def up(self):
for pg in self.ormsession.find(Ticket):
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(Ticket):
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 AddShowInSearchAttributeToAllCustomFields(TrackerMigration):
version = 1
def up(self):
for custom_field in self.each_custom_field():
custom_field['show_in_search'] = False
self.ormsession.flush()
def down(self):
for custom_field in self.each_custom_field():
del custom_field['show_in_search']
self.ormsession.flush()
def each_custom_field(self):
for tracker_globals in self.ormsession.find(Globals):
for custom_field in tracker_globals.custom_fields:
yield custom_field
class AddSaveSearchesPermission(TrackerMigration):
version = 2
def up(self):
first_ticket = self.ormsession.find(Ticket).first()
if first_ticket:
app_config = self.ormsession.get(AppConfig, first_ticket.app_config_id)
app_config.acl.save_searches = []
developer = self.ormsession.find(ProjectRole, dict(name='Developer')).first()
admin = self.ormsession.find(ProjectRole, dict(name='Admin')).first()
if developer:
app_config.acl.save_searches.append(developer._id)
if admin:
app_config.acl.save_searches.append(admin._id)
self.ormsession.flush()
def down(self):
first_ticket = self.ormsession.find(Ticket).first()
if first_ticket:
app_config = self.ormsession.get(AppConfig, first_ticket.app_config_id)
del app_config.acl.save_searches
self.ormsession.flush()
class SplitStatusNamesIntoOpenAndClosed(TrackerMigration):
version = 3
def up(self):
for tracker_globals in self.ormsession.find(Globals):
old_names = tracker_globals.status_names
tracker_globals.open_status_names = ' '.join([name for name in old_names.split(' ') if name and name != 'closed'])
tracker_globals.closed_status_names = 'closed'
tracker_globals.status_names = ''
self.ormsession.flush()
self.ormsession.clear()
def down(self):
for tracker_globals in self.ormsession.find(Globals):
tracker_globals.status_names = ' '.join([tracker_globals.open_status_names, tracker_globals.closed_status_names])
tracker_globals.open_status_names = ''
tracker_globals.closed_status_names = ''
self.ormsession.flush()
self.ormsession.clear()
class MoveMilestonesToCustom(TrackerMigration):
version = 4
def _custom_field(self, tracker_globals):
names = tracker_globals.milestone_names or ''
return dict(
name='_milestone',
show_in_search=True,
type='milestone',
label='Milestone',
milestones=[
dict(name=name, complete=False, due_date=None)
for name in names.split() ])
def up(self):
for tracker_globals in self.ormsession.find(Globals):
fld = self._custom_field(tracker_globals)
tracker_globals.custom_fields.append(fld)
self.ormsession.flush()
self.ormsession.clear()
def down(self):
for tracker_globals in self.ormsession.find(Globals):
fld = self._custom_field(tracker_globals)
if tracker_globals.custom_fields[-1] == fld:
tracker_globals.custom_fields.pop()
self.ormsession.flush()
self.ormsession.clear()
class FixMilestonesAndTickets(TrackerMigration):
version = 5
def up(self):
for tracker_globals in self.ormsession.find(Globals):
for fld in tracker_globals.custom_fields:
if 'name' not in fld:
fld.name = '_' + fld.label.lower()
if 'show_in_search' not in fld:
fld.show_in_search = True
for ticket in self.ormsession.find(Ticket):
ticket.custom_fields['_milestone'] = ticket.milestone
self.ormsession.flush()
self.ormsession.clear()