branch | stable |
changeset 6208 | 07b176640a8c |
parent 6199 | a88902af3813 |
child 6210 | d36104de8459 |
6207:c3f11fadf9ed | 6208:07b176640a8c |
---|---|
189 # add_relation_def return a RelationDefinitionSchema if it has been |
189 # add_relation_def return a RelationDefinitionSchema if it has been |
190 # actually added (can be None on duplicated relation definitions, |
190 # actually added (can be None on duplicated relation definitions, |
191 # e.g. if the relation type is marked as beeing symmetric) |
191 # e.g. if the relation type is marked as beeing symmetric) |
192 rdefs = schema.add_relation_def(rdef) |
192 rdefs = schema.add_relation_def(rdef) |
193 if rdefs is not None: |
193 if rdefs is not None: |
194 ertidx[rdefeid] = rdefs |
|
194 set_perms(rdefs, permsidx) |
195 set_perms(rdefs, permsidx) |
195 |
196 |
196 for values in session.execute( |
197 for values in session.execute( |
197 'Any X,SE,RT,OE,CARD,ORD,DESC,IDX,FTIDX,I18N,DFLT WHERE X is CWAttribute,' |
198 'Any X,SE,RT,OE,CARD,ORD,DESC,IDX,FTIDX,I18N,DFLT WHERE X is CWAttribute,' |
198 'X relation_type RT, X cardinality CARD, X ordernum ORD, X indexed IDX,' |
199 'X relation_type RT, X cardinality CARD, X ordernum ORD, X indexed IDX,' |
217 rdefs = schema.add_relation_def(rdef) |
218 rdefs = schema.add_relation_def(rdef) |
218 except BadSchemaDefinition: |
219 except BadSchemaDefinition: |
219 continue |
220 continue |
220 if rdefs is not None: |
221 if rdefs is not None: |
221 set_perms(rdefs, permsidx) |
222 set_perms(rdefs, permsidx) |
223 unique_togethers = {} |
|
224 try: |
|
225 rset = session.execute( |
|
226 'Any X,E,R WHERE ' |
|
227 'X is CWUniqueTogetherConstraint, ' |
|
228 'X constraint_of E, X relations R', build_descr=False) |
|
229 except Exception: |
|
230 import traceback |
|
231 traceback.print_exc() |
|
232 session.rollback() # first migration introducing CWUniqueTogetherConstraint |
|
233 else: |
|
234 for values in rset: |
|
235 uniquecstreid, eeid, releid = values |
|
236 eschema = schema.schema_by_eid(eeid) |
|
237 relations = unique_togethers.setdefault(uniquecstreid, (eschema, [])) |
|
238 relations[1].append(ertidx[releid].rtype.type) |
|
239 for eschema, unique_together in unique_togethers.itervalues(): |
|
240 eschema._unique_together.append(tuple(sorted(unique_together))) |
|
222 schema.infer_specialization_rules() |
241 schema.infer_specialization_rules() |
223 session.commit() |
242 session.commit() |
224 schema.reading_from_database = False |
243 schema.reading_from_database = False |
225 |
244 |
226 |
245 |
330 for rdef in rdefs: |
349 for rdef in rdefs: |
331 execschemarql(execute, rdef, |
350 execschemarql(execute, rdef, |
332 rdef2rql(rdef, cstrtypemap, groupmap)) |
351 rdef2rql(rdef, cstrtypemap, groupmap)) |
333 if pb is not None: |
352 if pb is not None: |
334 pb.update() |
353 pb.update() |
354 # serialize unique_together constraints |
|
355 for eschema in eschemas: |
|
356 for unique_together in eschema._unique_together: |
|
357 execschemarql(execute, eschema, [uniquetogether2rql(eschema, unique_together)]) |
|
335 for rql, kwargs in specialize2rql(schema): |
358 for rql, kwargs in specialize2rql(schema): |
336 execute(rql, kwargs, build_descr=False) |
359 execute(rql, kwargs, build_descr=False) |
337 if pb is not None: |
360 if pb is not None: |
338 pb.update() |
361 pb.update() |
339 if not quiet: |
362 if not quiet: |
386 def eschemaspecialize2rql(eschema): |
409 def eschemaspecialize2rql(eschema): |
387 specialized_type = eschema.specializes() |
410 specialized_type = eschema.specializes() |
388 if specialized_type: |
411 if specialized_type: |
389 values = {'x': eschema.eid, 'et': specialized_type.eid} |
412 values = {'x': eschema.eid, 'et': specialized_type.eid} |
390 yield 'SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s', values |
413 yield 'SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s', values |
414 |
|
415 def uniquetogether2rql(eschema, unique_together): |
|
416 relations = [] |
|
417 restrictions = [] |
|
418 substs = {} |
|
419 for i, name in enumerate(unique_together): |
|
420 rschema = eschema.rdef(name) |
|
421 var = 'R%d' % i |
|
422 rtype = 'T%d' % i |
|
423 substs[rtype] = rschema.rtype.type |
|
424 relations.append('C relations %s' % var) |
|
425 restrictions.append('%(var)s from_entity X, ' |
|
426 '%(var)s relation_type %(rtype)s, ' |
|
427 '%(rtype)s name %%(%(rtype)s)s' \ |
|
428 % {'var': var, |
|
429 'rtype':rtype}) |
|
430 relations = ', '.join(relations) |
|
431 restrictions = ', '.join(restrictions) |
|
432 rql = ('INSERT CWUniqueTogetherConstraint C: ' |
|
433 ' C constraint_of X, %s ' |
|
434 'WHERE ' |
|
435 ' X eid %%(x)s, %s' ) |
|
436 |
|
437 return rql % (relations, restrictions), substs |
|
438 |
|
391 |
439 |
392 def _ervalues(erschema): |
440 def _ervalues(erschema): |
393 try: |
441 try: |
394 type_ = unicode(erschema.type) |
442 type_ = unicode(erschema.type) |
395 except UnicodeDecodeError, e: |
443 except UnicodeDecodeError, e: |