server/schemaserial.py
changeset 5558 afd1face1faf
parent 5426 0d4853a6e5ee
child 5896 67683b7e591a
equal deleted inserted replaced
5557:1a534c596bff 5558:afd1face1faf
    25 
    25 
    26 from logilab.common.shellutils import ProgressBar
    26 from logilab.common.shellutils import ProgressBar
    27 
    27 
    28 from yams import schema as schemamod, buildobjs as ybo
    28 from yams import schema as schemamod, buildobjs as ybo
    29 
    29 
    30 from cubicweb.schema import CONSTRAINTS, ETYPE_NAME_MAP, VIRTUAL_RTYPES
    30 from cubicweb import CW_SOFTWARE_ROOT
       
    31 from cubicweb.schema import (CONSTRAINTS, ETYPE_NAME_MAP,
       
    32                              VIRTUAL_RTYPES, PURE_VIRTUAL_RTYPES)
    31 from cubicweb.server import sqlutils
    33 from cubicweb.server import sqlutils
    32 
    34 
    33 def group_mapping(cursor, interactive=True):
    35 def group_mapping(cursor, interactive=True):
    34     """create a group mapping from an rql cursor
    36     """create a group mapping from an rql cursor
    35 
    37 
    98             eschema = schema.eschema(etype)
   100             eschema = schema.eschema(etype)
    99             eschema.eid = eid
   101             eschema.eid = eid
   100             sidx[eid] = eschema
   102             sidx[eid] = eschema
   101             continue
   103             continue
   102         if etype in ETYPE_NAME_MAP:
   104         if etype in ETYPE_NAME_MAP:
       
   105             needcopy = False
   103             netype = ETYPE_NAME_MAP[etype]
   106             netype = ETYPE_NAME_MAP[etype]
   104             # can't use write rql queries at this point, use raw sql
   107             # can't use write rql queries at this point, use raw sql
   105             session.system_sql('UPDATE %(p)sCWEType SET %(p)sname=%%(n)s WHERE %(p)seid=%%(x)s'
   108             sqlexec = session.system_sql
   106                                % {'p': sqlutils.SQL_PREFIX},
   109             if sqlexec('SELECT 1 FROM %(p)sCWEType WHERE %(p)sname=%%(n)s'
   107                                {'x': eid, 'n': netype})
   110                        % {'p': sqlutils.SQL_PREFIX}, {'n': netype}).fetchone():
   108             session.system_sql('UPDATE entities SET type=%(n)s WHERE type=%(x)s',
   111                 # the new type already exists, we should merge
   109                                {'x': etype, 'n': netype})
   112                 assert etype.lower() != netype.lower()
       
   113                 needcopy = True
       
   114             else:
       
   115                 # the new type doesn't exist, we should rename
       
   116                 sqlexec('UPDATE %(p)sCWEType SET %(p)sname=%%(n)s WHERE %(p)seid=%%(x)s'
       
   117                         % {'p': sqlutils.SQL_PREFIX}, {'x': eid, 'n': netype})
       
   118                 if etype.lower() != netype.lower():
       
   119                     sqlexec('ALTER TABLE %s%s RENAME TO %s%s' % (
       
   120                         sqlutils.SQL_PREFIX, etype, sqlutils.SQL_PREFIX, netype))
       
   121             sqlexec('UPDATE entities SET type=%(n)s WHERE type=%(x)s',
       
   122                     {'x': etype, 'n': netype})
   110             session.commit(False)
   123             session.commit(False)
   111             try:
   124             try:
   112                 session.system_sql('UPDATE deleted_entities SET type=%(n)s WHERE type=%(x)s',
   125                 sqlexec('UPDATE deleted_entities SET type=%(n)s WHERE type=%(x)s',
   113                                    {'x': etype, 'n': netype})
   126                         {'x': etype, 'n': netype})
   114             except:
   127             except:
   115                 pass
   128                 pass
   116             tocleanup = [eid]
   129             tocleanup = [eid]
   117             tocleanup += (eid for eid, (eidetype, uri, extid) in repo._type_source_cache.items()
   130             tocleanup += (eid for eid, (eidetype, uri, extid) in repo._type_source_cache.items()
   118                           if etype == eidetype)
   131                           if etype == eidetype)
   119             repo.clear_caches(tocleanup)
   132             repo.clear_caches(tocleanup)
   120             session.commit(False)
   133             session.commit(False)
       
   134             if needcopy:
       
   135                 from logilab.common.testlib import mock_object
       
   136                 sidx[eid] = mock_object(type=netype)
       
   137                 # copy / CWEType entity removal expected to be done through
       
   138                 # rename_entity_type in a migration script
       
   139                 continue
   121             etype = netype
   140             etype = netype
   122         etype = ybo.EntityType(name=etype, description=desc, eid=eid)
   141         etype = ybo.EntityType(name=etype, description=desc, eid=eid)
   123         eschema = schema.add_entity_type(etype)
   142         eschema = schema.add_entity_type(etype)
   124         sidx[eid] = eschema
   143         sidx[eid] = eschema
   125         set_perms(eschema, permsdict)
   144         set_perms(eschema, permsdict)