server/schemaserial.py
changeset 4475 37c413a07216
parent 4467 0e73d299730a
child 4570 ede247bbbf62
equal deleted inserted replaced
4474:55fe19813bb7 4475:37c413a07216
    50                 except ValueError:
    50                 except ValueError:
    51                     print 'eid should be an integer'
    51                     print 'eid should be an integer'
    52                     continue
    52                     continue
    53     return res
    53     return res
    54 
    54 
    55 def _set_sql_prefix(prefix):
       
    56     """3.2.0 migration function: allow to unset/reset SQL_PREFIX"""
       
    57     for module in ('checkintegrity', 'migractions', 'schemahooks',
       
    58                    'sources.rql2sql', 'sources.native', 'sqlutils'):
       
    59         try:
       
    60             sys.modules['cubicweb.server.%s' % module].SQL_PREFIX = prefix
       
    61             print 'changed SQL_PREFIX for %s' % module
       
    62         except KeyError:
       
    63             pass
       
    64 
       
    65 def _update_database(schema, sqlcu):
       
    66     """3.2.0 migration function: update database schema by adding SQL_PREFIX to
       
    67     entity type tables and columns
       
    68     """
       
    69     for etype in schema.entities():
       
    70         if etype.final:
       
    71             continue
       
    72         try:
       
    73             sql = 'ALTER TABLE %s RENAME TO cw_%s' % (
       
    74                 etype, ETYPE_NAME_MAP.get(etype, etype))
       
    75             print sql
       
    76             sqlcu.execute(sql)
       
    77         except:
       
    78             pass
       
    79         for rschema in etype.subject_relations():
       
    80             if rschema == 'has_text':
       
    81                 continue
       
    82             if rschema.final or rschema.inlined:
       
    83                 sql = 'ALTER TABLE cw_%s RENAME %s TO cw_%s' % (
       
    84                     etype, rschema, rschema)
       
    85                 print sql
       
    86                 sqlcu.execute(sql)
       
    87 
       
    88 # schema / perms deserialization ##############################################
    55 # schema / perms deserialization ##############################################
    89 OLD_SCHEMA_TYPES = frozenset(('EFRDef', 'ENFRDef', 'ERType', 'EEType',
    56 OLD_SCHEMA_TYPES = frozenset(('EFRDef', 'ENFRDef', 'ERType', 'EEType',
    90                               'EConstraintType', 'EConstraint', 'EGroup',
    57                               'EConstraintType', 'EConstraint', 'EGroup',
    91                               'EUser', 'ECache', 'EPermission', 'EProperty'))
    58                               'EUser', 'ECache', 'EPermission', 'EProperty'))
    92 
    59 
    93 def deserialize_schema(schema, session):
    60 def deserialize_schema(schema, session):
    94     """return a schema according to information stored in an rql database
    61     """return a schema according to information stored in an rql database
    95     as CWRType and CWEType entities
    62     as CWRType and CWEType entities
    96     """
    63     """
    97     #
       
    98     repo = session.repo
    64     repo = session.repo
       
    65     dbhelper = repo.system_source.dbhelper
       
    66     # 3.6 migration
    99     sqlcu = session.pool['system']
    67     sqlcu = session.pool['system']
   100     _3_2_migration = False
       
   101     dbhelper = repo.system_source.dbhelper
       
   102     tables = set(t.lower() for t in dbhelper.list_tables(sqlcu))
       
   103     if 'eetype' in tables:
       
   104         _3_2_migration = True
       
   105         # 3.2 migration
       
   106         _set_sql_prefix('')
       
   107         # first rename entity types whose name changed in 3.2 without adding the
       
   108         # cw_ prefix
       
   109         for etype in OLD_SCHEMA_TYPES:
       
   110             if etype.lower() in tables:
       
   111                 sql = 'ALTER TABLE %s RENAME TO %s' % (etype,
       
   112                                                        ETYPE_NAME_MAP[etype])
       
   113                 print sql
       
   114                 sqlcu.execute(sql)
       
   115         # other table renaming done once schema has been read
       
   116     # 3.6 migration
       
   117     sqlcu.execute("SELECT * FROM cw_CWRType WHERE cw_name='symetric'")
    68     sqlcu.execute("SELECT * FROM cw_CWRType WHERE cw_name='symetric'")
   118     if sqlcu.fetchall():
    69     if sqlcu.fetchall():
   119         sql = dbhelper.sql_rename_col('cw_CWRType', 'cw_symetric', 'cw_symmetric',
    70         sql = dbhelper.sql_rename_col('cw_CWRType', 'cw_symetric', 'cw_symmetric',
   120                                       dbhelper.TYPE_MAPPING['Boolean'], True)
    71                                       dbhelper.TYPE_MAPPING['Boolean'], True)
   121         sqlcu.execute(sql)
    72         sqlcu.execute(sql)
   207         rdefs = schema.add_relation_def(rdef)
   158         rdefs = schema.add_relation_def(rdef)
   208         # rdefs can be None on duplicated relation definitions (e.g. symmetrics)
   159         # rdefs can be None on duplicated relation definitions (e.g. symmetrics)
   209         if rdefs is not None:
   160         if rdefs is not None:
   210             set_perms(rdefs, permsdict)
   161             set_perms(rdefs, permsdict)
   211     schema.infer_specialization_rules()
   162     schema.infer_specialization_rules()
   212     if _3_2_migration:
       
   213         _update_database(schema, sqlcu)
       
   214         _set_sql_prefix('cw_')
       
   215     session.commit()
   163     session.commit()
   216     schema.reading_from_database = False
   164     schema.reading_from_database = False
   217 
   165 
   218 
   166 
   219 def deserialize_ertype_permissions(session):
   167 def deserialize_ertype_permissions(session):