server/migractions.py
changeset 5558 afd1face1faf
parent 5556 9ab2b4c74baf
child 5679 0f2ded880d01
equal deleted inserted replaced
5557:1a534c596bff 5558:afd1face1faf
    49 
    49 
    50 from yams.constraints import SizeConstraint
    50 from yams.constraints import SizeConstraint
    51 from yams.schema2sql import eschema2sql, rschema2sql
    51 from yams.schema2sql import eschema2sql, rschema2sql
    52 
    52 
    53 from cubicweb import AuthenticationError
    53 from cubicweb import AuthenticationError
    54 from cubicweb.schema import (META_RTYPES, VIRTUAL_RTYPES,
    54 from cubicweb.schema import (ETYPE_NAME_MAP, META_RTYPES, VIRTUAL_RTYPES,
       
    55                              PURE_VIRTUAL_RTYPES,
    55                              CubicWebRelationSchema, order_eschemas)
    56                              CubicWebRelationSchema, order_eschemas)
    56 from cubicweb.dbapi import get_repository, repo_connect
    57 from cubicweb.dbapi import get_repository, repo_connect
    57 from cubicweb.migration import MigrationHelper, yes
    58 from cubicweb.migration import MigrationHelper, yes
    58 from cubicweb.server.session import hooks_control
    59 from cubicweb.server.session import hooks_control
    59 try:
    60 try:
   849         """rename an existing entity type in the persistent schema
   850         """rename an existing entity type in the persistent schema
   850 
   851 
   851         `oldname` is a string giving the name of the existing entity type
   852         `oldname` is a string giving the name of the existing entity type
   852         `newname` is a string giving the name of the renamed entity type
   853         `newname` is a string giving the name of the renamed entity type
   853         """
   854         """
   854         self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(oldname)s',
   855         schema = self.repo.schema
   855                      {'newname' : unicode(newname), 'oldname' : oldname},
   856         if newname in schema:
   856                      ask_confirm=False)
   857             assert oldname in ETYPE_NAME_MAP, \
       
   858                    '%s should be mappend to %s in ETYPE_NAME_MAP' % (oldname, newname)
       
   859             attrs = ','.join([SQL_PREFIX + rschema.type
       
   860                               for rschema in schema[newname].subject_relations()
       
   861                               if (rschema.final or rschema.inlined)
       
   862                               and not rschema in PURE_VIRTUAL_RTYPES])
       
   863             self.sqlexec('INSERT INTO %s%s(%s) SELECT %s FROM %s%s' % (
       
   864                 SQL_PREFIX, newname, attrs, attrs, SQL_PREFIX, oldname))
       
   865             # use rql to propagate deletion. XXX we may miss some stuff since
       
   866             # only the bootstrap schema is set.
       
   867             self.rqlexec('DELETE CWEType ET WHERE ET name %(n)s', {'n': oldname})
       
   868         else:
       
   869             self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(oldname)s',
       
   870                          {'newname' : unicode(newname), 'oldname' : oldname},
       
   871                          ask_confirm=False)
   857         if commit:
   872         if commit:
   858             self.commit()
   873             self.commit()
   859 
   874 
   860     def cmd_add_relation_type(self, rtype, addrdef=True, commit=True):
   875     def cmd_add_relation_type(self, rtype, addrdef=True, commit=True):
   861         """register a new relation type named `rtype`, as described in the
   876         """register a new relation type named `rtype`, as described in the