server/migractions.py
changeset 9205 ea32e964fbf8
parent 9195 e9fb9377229e
child 9241 cbee712dd310
equal deleted inserted replaced
9149:31ed9dd946d1 9205:ea32e964fbf8
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    81         # least)
    81         # least)
    82         if not cls.__regid__ in repo.vreg['after_add_entity_hooks']:
    82         if not cls.__regid__ in repo.vreg['after_add_entity_hooks']:
    83             repo.vreg.register(ClearGroupMap)
    83             repo.vreg.register(ClearGroupMap)
    84 
    84 
    85 class ServerMigrationHelper(MigrationHelper):
    85 class ServerMigrationHelper(MigrationHelper):
    86     """specific migration helper for server side  migration scripts,
    86     """specific migration helper for server side migration scripts,
    87     providind actions related to schema/data migration
    87     providing actions related to schema/data migration
    88     """
    88     """
    89 
    89 
    90     def __init__(self, config, schema, interactive=True,
    90     def __init__(self, config, schema, interactive=True,
    91                  repo=None, cnx=None, verbosity=1, connect=True):
    91                  repo=None, cnx=None, verbosity=1, connect=True):
    92         MigrationHelper.__init__(self, config, interactive, verbosity)
    92         MigrationHelper.__init__(self, config, interactive, verbosity)
   573                 self.rqlexec('DELETE CWUniqueTogetherConstraint C '
   573                 self.rqlexec('DELETE CWUniqueTogetherConstraint C '
   574                              'WHERE C constraint_of E, '
   574                              'WHERE C constraint_of E, '
   575                              '      E eid %%(x)s,'
   575                              '      E eid %%(x)s,'
   576                              '      %s' % ', '.join(restrictions),
   576                              '      %s' % ', '.join(restrictions),
   577                              substs)
   577                              substs)
       
   578             def possible_unique_constraint(ut):
       
   579                 for name in ut:
       
   580                     rschema = repoeschema.subjrels.get(name)
       
   581                     if rschema is None:
       
   582                         print 'dont add %s unique constraint on %s, missing %s' % (
       
   583                             ','.join(ut), eschema, name)
       
   584                         return False
       
   585                     if not (rschema.final or rschema.final.inlined):
       
   586                         (eschema, name)
       
   587                         print 'dont add %s unique constraint on %s, %s is neither final nor inlined' % (
       
   588                             ','.join(ut), eschema, name)
       
   589                         return False
       
   590                 return True
   578             for ut in unique_together - repo_unique_together:
   591             for ut in unique_together - repo_unique_together:
   579                 rql, substs = ss.uniquetogether2rql(eschema, ut)
   592                 if possible_unique_constraint(ut):
   580                 substs['x'] = repoeschema.eid
   593                     rql, substs = ss.uniquetogether2rql(eschema, ut)
   581                 self.rqlexec(rql, substs)
   594                     substs['x'] = repoeschema.eid
       
   595                     self.rqlexec(rql, substs)
   582 
   596 
   583     def _synchronize_rdef_schema(self, subjtype, rtype, objtype,
   597     def _synchronize_rdef_schema(self, subjtype, rtype, objtype,
   584                                  syncperms=True, syncprops=True):
   598                                  syncperms=True, syncprops=True):
   585         """synchronize properties of the persistent relation definition schema
   599         """synchronize properties of the persistent relation definition schema
   586         against its current definition:
   600         against its current definition:
   686             new.add(eschema.type)
   700             new.add(eschema.type)
   687         # check if attributes has been added to existing entities
   701         # check if attributes has been added to existing entities
   688         for rschema in newcubes_schema.relations():
   702         for rschema in newcubes_schema.relations():
   689             existingschema = self.repo.schema.rschema(rschema.type)
   703             existingschema = self.repo.schema.rschema(rschema.type)
   690             for (fromtype, totype) in rschema.rdefs:
   704             for (fromtype, totype) in rschema.rdefs:
   691                 if (fromtype, totype) in existingschema.rdefs:
   705                 # if rdef already exists or is infered from inheritance,
       
   706                 # don't add it
       
   707                 if (fromtype, totype) in existingschema.rdefs \
       
   708                         or rschema.rdefs[(fromtype, totype)].infered:
   692                     continue
   709                     continue
   693                 # check we should actually add the relation definition
   710                 # check we should actually add the relation definition
   694                 if not (fromtype in new or totype in new or rschema in new):
   711                 if not (fromtype in new or totype in new or rschema in new):
   695                     continue
   712                     continue
   696                 self.cmd_add_relation_definition(str(fromtype), rschema.type,
   713                 self.cmd_add_relation_definition(str(fromtype), rschema.type,
   927 
   944 
   928         `oldname` is a string giving the name of the existing entity type
   945         `oldname` is a string giving the name of the existing entity type
   929         `newname` is a string giving the name of the renamed entity type
   946         `newname` is a string giving the name of the renamed entity type
   930         """
   947         """
   931         schema = self.repo.schema
   948         schema = self.repo.schema
       
   949         if oldname not in schema:
       
   950             print 'warning: entity type %s is unknown, skip renaming' % oldname
       
   951             return
       
   952         # if merging two existing entity types
   932         if newname in schema:
   953         if newname in schema:
   933             assert oldname in ETYPE_NAME_MAP, \
   954             assert oldname in ETYPE_NAME_MAP, \
   934                    '%s should be mapped to %s in ETYPE_NAME_MAP' % (oldname,
   955                    '%s should be mapped to %s in ETYPE_NAME_MAP' % (oldname,
   935                                                                     newname)
   956                                                                     newname)
   936             if attrs is None:
   957             if attrs is None:
  1001                     for sql in sqls:
  1022                     for sql in sqls:
  1002                         self.sqlexec(sql % thiseids, ask_confirm=False)
  1023                         self.sqlexec(sql % thiseids, ask_confirm=False)
  1003             # remove the old type: use rql to propagate deletion
  1024             # remove the old type: use rql to propagate deletion
  1004             self.rqlexec('DELETE CWEType ET WHERE ET name %(on)s', {'on': oldname},
  1025             self.rqlexec('DELETE CWEType ET WHERE ET name %(on)s', {'on': oldname},
  1005                          ask_confirm=False)
  1026                          ask_confirm=False)
       
  1027         # elif simply renaming an entity type
  1006         else:
  1028         else:
  1007             self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(on)s',
  1029             self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(on)s',
  1008                          {'newname' : unicode(newname), 'on' : oldname},
  1030                          {'newname' : unicode(newname), 'on' : oldname},
  1009                          ask_confirm=False)
  1031                          ask_confirm=False)
  1010         if commit:
  1032         if commit: