server/schemaserial.py
changeset 9375 8e88576787c3
parent 9299 c5eed908117d
child 9395 96dba2efd16d
equal deleted inserted replaced
9374:1236d9058ad3 9375:8e88576787c3
    23 from itertools import chain
    23 from itertools import chain
    24 import json
    24 import json
    25 
    25 
    26 from logilab.common.shellutils import ProgressBar
    26 from logilab.common.shellutils import ProgressBar
    27 
    27 
    28 from yams import BadSchemaDefinition, schema as schemamod, buildobjs as ybo
    28 from yams import (BadSchemaDefinition, schema as schemamod, buildobjs as ybo,
       
    29                   schema2sql as y2sql)
    29 
    30 
    30 from cubicweb import CW_SOFTWARE_ROOT, Binary, typed_eid
    31 from cubicweb import CW_SOFTWARE_ROOT, Binary, typed_eid
    31 from cubicweb.schema import (KNOWN_RPROPERTIES, CONSTRAINTS, ETYPE_NAME_MAP,
    32 from cubicweb.schema import (KNOWN_RPROPERTIES, CONSTRAINTS, ETYPE_NAME_MAP,
    32                              VIRTUAL_RTYPES, PURE_VIRTUAL_RTYPES)
    33                              VIRTUAL_RTYPES, PURE_VIRTUAL_RTYPES)
    33 from cubicweb.server import sqlutils
    34 from cubicweb.server import sqlutils
   365                           rdef2rql(rdef, cstrtypemap, groupmap))
   366                           rdef2rql(rdef, cstrtypemap, groupmap))
   366         if pb is not None:
   367         if pb is not None:
   367             pb.update()
   368             pb.update()
   368     # serialize unique_together constraints
   369     # serialize unique_together constraints
   369     for eschema in eschemas:
   370     for eschema in eschemas:
   370         for unique_together in eschema._unique_together:
   371         if eschema._unique_together:
   371             execschemarql(execute, eschema, [uniquetogether2rql(eschema, unique_together)])
   372             execschemarql(execute, eschema, uniquetogether2rqls(eschema))
   372     # serialize yams inheritance relationships
   373     # serialize yams inheritance relationships
   373     for rql, kwargs in specialize2rql(schema):
   374     for rql, kwargs in specialize2rql(schema):
   374         execute(rql, kwargs, build_descr=False)
   375         execute(rql, kwargs, build_descr=False)
   375         if pb is not None:
   376         if pb is not None:
   376             pb.update()
   377             pb.update()
   425     specialized_type = eschema.specializes()
   426     specialized_type = eschema.specializes()
   426     if specialized_type:
   427     if specialized_type:
   427         values = {'x': eschema.eid, 'et': specialized_type.eid}
   428         values = {'x': eschema.eid, 'et': specialized_type.eid}
   428         yield 'SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s', values
   429         yield 'SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s', values
   429 
   430 
   430 def uniquetogether2rql(eschema, unique_together):
   431 def uniquetogether2rqls(eschema):
       
   432     rql_args = []
       
   433     for columns in eschema._unique_together:
       
   434         rql, args = _uniquetogether2rql(eschema, columns)
       
   435         args['name'] = y2sql.unique_index_name(eschema, columns)
       
   436         rql_args.append((rql, args))
       
   437     return rql_args
       
   438 
       
   439 def _uniquetogether2rql(eschema, unique_together):
   431     relations = []
   440     relations = []
   432     restrictions = []
   441     restrictions = []
   433     substs = {}
   442     substs = {}
   434     for i, name in enumerate(unique_together):
   443     for i, name in enumerate(unique_together):
   435         rschema = eschema.schema.rschema(name)
   444         rschema = eschema.schema.rschema(name)
   437         substs[rtype] = rschema.type
   446         substs[rtype] = rschema.type
   438         relations.append('C relations %s' % rtype)
   447         relations.append('C relations %s' % rtype)
   439         restrictions.append('%(rtype)s name %%(%(rtype)s)s' % {'rtype': rtype})
   448         restrictions.append('%(rtype)s name %%(%(rtype)s)s' % {'rtype': rtype})
   440     relations = ', '.join(relations)
   449     relations = ', '.join(relations)
   441     restrictions = ', '.join(restrictions)
   450     restrictions = ', '.join(restrictions)
   442     rql = ('INSERT CWUniqueTogetherConstraint C: '
   451     rql = ('INSERT CWUniqueTogetherConstraint C: C name %%(name)s, C constraint_of X, %s '
   443            '    C constraint_of X, %s  '
   452            'WHERE X eid %%(x)s, %s')
   444            'WHERE '
       
   445            '    X eid %%(x)s, %s')
       
   446     return rql % (relations, restrictions), substs
   453     return rql % (relations, restrictions), substs
   447 
   454 
   448 
   455 
   449 def _ervalues(erschema):
   456 def _ervalues(erschema):
   450     try:
   457     try: