|
a/Allura/allura/command/show_models.py |
|
b/Allura/allura/command/show_models.py |
1 |
import sys
|
1 |
import sys
|
2 |
from collections import defaultdict
|
2 |
from collections import defaultdict
|
|
|
3 |
from itertools import groupby
|
3 |
|
4 |
|
4 |
from pylons import c, g
|
5 |
from pylons import c, g
|
|
|
6 |
from pymongo.errors import DuplicateKeyError
|
5 |
|
7 |
|
6 |
from ming.orm import mapper, session, Mapper
|
8 |
from ming.orm import mapper, session, Mapper
|
7 |
from ming.orm.declarative import MappedClass
|
9 |
from ming.orm.declarative import MappedClass
|
8 |
|
10 |
|
9 |
import allura.tasks.index_tasks
|
11 |
import allura.tasks.index_tasks
|
|
... |
|
... |
171 |
base.log.info('...... drop index %s:%s', collection.name, iname)
|
173 |
base.log.info('...... drop index %s:%s', collection.name, iname)
|
172 |
collection.drop_index(iname)
|
174 |
collection.drop_index(iname)
|
173 |
# Ensure all indexes
|
175 |
# Ensure all indexes
|
174 |
for name, idx in uindexes.iteritems():
|
176 |
for name, idx in uindexes.iteritems():
|
175 |
base.log.info('...... ensure %s:%s', collection.name, idx)
|
177 |
base.log.info('...... ensure %s:%s', collection.name, idx)
|
|
|
178 |
while True:
|
|
|
179 |
try:
|
176 |
collection.ensure_index(idx.index_spec, background=True, unique=True)
|
180 |
collection.ensure_index(idx.index_spec, unique=True)
|
|
|
181 |
break
|
|
|
182 |
except DuplicateKeyError, err:
|
|
|
183 |
base.log.info('Found dupe key(%s), eliminating dupes', err)
|
|
|
184 |
self._remove_dupes(collection, idx.index_spec)
|
177 |
for name, idx in indexes.iteritems():
|
185 |
for name, idx in indexes.iteritems():
|
178 |
base.log.info('...... ensure %s:%s', collection.name, idx)
|
186 |
base.log.info('...... ensure %s:%s', collection.name, idx)
|
179 |
collection.ensure_index(idx.index_spec, background=True)
|
187 |
collection.ensure_index(idx.index_spec, background=True)
|
|
|
188 |
|
|
|
189 |
def _remove_dupes(self, collection, spec):
|
|
|
190 |
iname = collection.create_index(spec)
|
|
|
191 |
fields = [ f[0] for f in spec ]
|
|
|
192 |
q = collection.find({}, fields=fields).sort(spec)
|
|
|
193 |
def keyfunc(doc):
|
|
|
194 |
return tuple(doc.get(f, None) for f in fields)
|
|
|
195 |
dupes = []
|
|
|
196 |
for key, doc_iter in groupby(q, key=keyfunc):
|
|
|
197 |
docs = list(doc_iter)
|
|
|
198 |
if len(docs) > 1:
|
|
|
199 |
base.log.info('Found dupes with %s', key)
|
|
|
200 |
dupes += [ doc['_id'] for doc in docs[1:] ]
|
|
|
201 |
collection.drop_index(iname)
|
|
|
202 |
collection.remove(dict(_id={'$in':dupes}))
|
180 |
|
203 |
|
181 |
def build_model_inheritance_graph():
|
204 |
def build_model_inheritance_graph():
|
182 |
graph = dict((m.mapped_class, ([], [])) for m in Mapper.all_mappers())
|
205 |
graph = dict((m.mapped_class, ([], [])) for m in Mapper.all_mappers())
|
183 |
for cls, (parents, children) in graph.iteritems():
|
206 |
for cls, (parents, children) in graph.iteritems():
|
184 |
for b in cls.__bases__:
|
207 |
for b in cls.__bases__:
|