cubicweb/hooks/syncschema.py
changeset 11417 5e5e224239c3
parent 11416 9c2fbb872e91
child 11763 39df042f4ab4
--- a/cubicweb/hooks/syncschema.py	Fri Jul 08 10:17:42 2016 +0200
+++ b/cubicweb/hooks/syncschema.py	Fri Jul 08 09:59:18 2016 +0200
@@ -45,7 +45,7 @@
 
 # core entity and relation types which can't be removed
 CORE_TYPES = BASE_TYPES | SCHEMA_TYPES | META_RTYPES | set(
-    ('CWUser', 'CWGroup','login', 'upassword', 'name', 'in_group'))
+    ('CWUser', 'CWGroup', 'login', 'upassword', 'name', 'in_group'))
 
 
 def get_constraints(cnx, entity):
@@ -76,7 +76,8 @@
     table = SQL_PREFIX + etype
     column = SQL_PREFIX + rtype
     try:
-        cnx.system_sql(str('ALTER TABLE %s ADD %s integer REFERENCES entities (eid)' % (table, column)),
+        cnx.system_sql(str('ALTER TABLE %s ADD %s integer REFERENCES entities (eid)'
+                           % (table, column)),
                        rollback_on_failure=False)
         cnx.info('added column %s to table %s', column, table)
     except Exception:
@@ -240,7 +241,7 @@
       CWAttribute entities
     * add <meta rtype> relation by creating the necessary CWRelation entity
     """
-    entity = None # make pylint happy
+    entity = None  # make pylint happy
 
     def precommit_event(self):
         cnx = self.cnx
@@ -360,11 +361,11 @@
                     op.add_data(objtype)
                     op.add_data(subjtype)
         # update the in-memory schema first
-        self.oldvalues = dict( (attr, getattr(rschema, attr)) for attr in self.values)
+        self.oldvalues = dict((attr, getattr(rschema, attr)) for attr in self.values)
         self.rschema.__dict__.update(self.values)
         # then make necessary changes to the system source database
         if 'inlined' not in self.values:
-            return # nothing to do
+            return  # nothing to do
         inlined = self.values['inlined']
         # check in-lining is possible when inlined
         if inlined:
@@ -376,7 +377,7 @@
         if not inlined:
             # need to create the relation if it has not been already done by
             # another event of the same transaction
-            if not rschema.type in cnx.transaction_data.get('createdtables', ()):
+            if rschema.type not in cnx.transaction_data.get('createdtables', ()):
                 # create the necessary table
                 for sql in y2sql.rschema2sql(rschema):
                     sqlexec(sql)
@@ -389,7 +390,6 @@
                 sqlexec('INSERT INTO %s_relation SELECT %s, %s FROM %s WHERE NOT %s IS NULL'
                         % (rtype, eidcolumn, column, table, column))
             # drop existant columns
-            #if cnx.repo.system_source.dbhelper.alter_column_support:
             for etype in rschema.subjects():
                 DropColumn.get_instance(cnx).add_data((str(etype), rtype))
         else:
@@ -427,7 +427,7 @@
 
 class CWComputedRTypeUpdateOp(MemSchemaOperation):
     """actually update some properties of a computed relation definition"""
-    rschema = entity = rule = None # make pylint happy
+    rschema = entity = rule = None  # make pylint happy
     old_rule = None
 
     def precommit_event(self):
@@ -449,7 +449,7 @@
 
     constraints are handled by specific hooks
     """
-    entity = None # make pylint happy
+    entity = None  # make pylint happy
 
     def init_rdef(self, **kwargs):
         entity = self.entity
@@ -524,7 +524,7 @@
         try:
             eschema = schema.eschema(rdefdef.subject)
         except KeyError:
-            return # entity type currently being added
+            return  # entity type currently being added
         # propagate attribute to children classes
         rschema = schema.rschema(rdefdef.name)
         # if relation type has been inserted in the same transaction, its final
@@ -535,7 +535,7 @@
         if default is not None:
             default = convert_default_value(self.rdefdef, default)
             cnx.system_sql('UPDATE %s SET %s=%%(default)s' % (table, column),
-                               {'default': default})
+                           {'default': default})
         # if attribute is computed, compute it
         if getattr(entity, 'formula', None):
             # add rtype attribute for RelationDefinitionSchema api compat, this
@@ -563,7 +563,7 @@
 
     constraints are handled by specific hooks
     """
-    entity = None # make pylint happy
+    entity = None  # make pylint happy
 
     def precommit_event(self):
         cnx = self.cnx
@@ -607,7 +607,7 @@
 
 class RDefDelOp(MemSchemaOperation):
     """an actual relation has been removed"""
-    rdef = None # make pylint happy
+    rdef = None  # make pylint happy
 
     def precommit_event(self):
         cnx = self.cnx
@@ -670,7 +670,7 @@
 
 class RDefUpdateOp(MemSchemaOperation):
     """actually update some properties of a relation definition"""
-    rschema = rdefkey = values = None # make pylint happy
+    rschema = rdefkey = values = None  # make pylint happy
     rdef = oldvalues = None
     indexed_changed = null_allowed_changed = False
 
@@ -678,15 +678,15 @@
         cnx = self.cnx
         rdef = self.rdef = self.rschema.rdefs[self.rdefkey]
         # update the in-memory schema first
-        self.oldvalues = dict( (attr, getattr(rdef, attr)) for attr in self.values)
+        self.oldvalues = dict((attr, getattr(rdef, attr)) for attr in self.values)
         rdef.update(self.values)
         # then make necessary changes to the system source database
         syssource = cnx.repo.system_source
         if 'indexed' in self.values:
             syssource.update_rdef_indexed(cnx, rdef)
             self.indexed_changed = True
-        if 'cardinality' in self.values and rdef.rtype.final \
-              and self.values['cardinality'][0] != self.oldvalues['cardinality'][0]:
+        if ('cardinality' in self.values and rdef.rtype.final
+                and self.values['cardinality'][0] != self.oldvalues['cardinality'][0]):
             syssource.update_rdef_null_allowed(self.cnx, rdef)
             self.null_allowed_changed = True
         if 'fulltextindexed' in self.values:
@@ -717,7 +717,7 @@
 
 class CWConstraintDelOp(MemSchemaOperation):
     """actually remove a constraint of a relation definition"""
-    rdef = oldcstr = newcstr = None # make pylint happy
+    rdef = oldcstr = newcstr = None  # make pylint happy
     size_cstr_changed = unique_changed = False
 
     def precommit_event(self):
@@ -775,7 +775,7 @@
 
 class CWConstraintAddOp(CWConstraintDelOp):
     """actually update constraint of a relation definition"""
-    entity = None # make pylint happy
+    entity = None  # make pylint happy
 
     def precommit_event(self):
         cnx = self.cnx
@@ -820,7 +820,7 @@
 
 
 class CWUniqueTogetherConstraintAddOp(MemSchemaOperation):
-    entity = None # make pylint happy
+    entity = None  # make pylint happy
 
     def precommit_event(self):
         cnx = self.cnx
@@ -841,8 +841,8 @@
 
 
 class CWUniqueTogetherConstraintDelOp(MemSchemaOperation):
-    entity = cstrname = None # for pylint
-    cols = () # for pylint
+    entity = cstrname = None  # make pylint happy
+    cols = ()  # make pylint happy
 
     def insert_index(self):
         # We need to run before CWConstraintDelOp: if a size constraint is
@@ -873,7 +873,7 @@
 
 class MemSchemaCWETypeDel(MemSchemaOperation):
     """actually remove the entity type from the instance's schema"""
-    etype = None # make pylint happy
+    etype = None  # make pylint happy
 
     def postcommit_event(self):
         # del_entity_type also removes entity's relations
@@ -882,7 +882,7 @@
 
 class MemSchemaCWRTypeAdd(MemSchemaOperation):
     """actually add the relation type to the instance's schema"""
-    rtypedef = None # make pylint happy
+    rtypedef = None  # make pylint happy
 
     def precommit_event(self):
         self.cnx.vreg.schema.add_relation_type(self.rtypedef)
@@ -893,7 +893,7 @@
 
 class MemSchemaCWRTypeDel(MemSchemaOperation):
     """actually remove the relation type from the instance's schema"""
-    rtype = None # make pylint happy
+    rtype = None  # make pylint happy
 
     def postcommit_event(self):
         try:
@@ -906,7 +906,7 @@
 class MemSchemaPermissionAdd(MemSchemaOperation):
     """synchronize schema when a *_permission relation has been added on a group
     """
-    eid = action = group_eid = expr = None # make pylint happy
+    eid = action = group_eid = expr = None  # make pylint happy
 
     def precommit_event(self):
         """the observed connections.cnxset has been commited"""
@@ -961,7 +961,7 @@
 
 
 class MemSchemaSpecializesAdd(MemSchemaOperation):
-    etypeeid = parentetypeeid = None # make pylint happy
+    etypeeid = parentetypeeid = None  # make pylint happy
 
     def precommit_event(self):
         eschema = self.cnx.vreg.schema.schema_by_eid(self.etypeeid)
@@ -973,7 +973,7 @@
 
 
 class MemSchemaSpecializesDel(MemSchemaOperation):
-    etypeeid = parentetypeeid = None # make pylint happy
+    etypeeid = parentetypeeid = None  # make pylint happy
 
     def precommit_event(self):
         try:
@@ -1077,9 +1077,9 @@
             raise validation_error(self.entity, {None: _("can't be deleted")})
         # delete relation definitions using this relation type
         self._cw.execute('DELETE CWAttribute X WHERE X relation_type Y, Y eid %(x)s',
-                        {'x': self.entity.eid})
+                         {'x': self.entity.eid})
         self._cw.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s',
-                        {'x': self.entity.eid})
+                         {'x': self.entity.eid})
         MemSchemaCWRTypeDel(self._cw, rtype=name)
 
 
@@ -1185,10 +1185,8 @@
         pendingrdefs = cnx.transaction_data.setdefault('pendingrdefs', set())
         # first delete existing relation if necessary
         if rschema.final:
-            rdeftype = 'CWAttribute'
             pendingrdefs.add((subjschema, rschema))
         else:
-            rdeftype = 'CWRelation'
             pendingrdefs.add((subjschema, rschema, objschema))
         RDefDelOp(cnx, rdef=rdef)
 
@@ -1309,6 +1307,7 @@
         else:
             CWConstraintDelOp(self._cw, rdef=rdef, oldcstr=cstr)
 
+
 # unique_together constraints
 # XXX: use setoperations and before_add_relation here (on constraint_of and relations)
 class AfterAddCWUniqueTogetherConstraintHook(SyncSchemaHook):
@@ -1351,7 +1350,7 @@
         if self._cw.entity_metas(self.eidto)['type'] == 'CWGroup':
             MemSchemaPermissionAdd(self._cw, action=action, eid=self.eidfrom,
                                    group_eid=self.eidto)
-        else: # RQLExpression
+        else:  # RQLExpression
             expr = self._cw.entity_from_eid(self.eidto).expression
             MemSchemaPermissionAdd(self._cw, action=action, eid=self.eidfrom,
                                    expr=expr)
@@ -1372,13 +1371,12 @@
         if self._cw.entity_metas(self.eidto)['type'] == 'CWGroup':
             MemSchemaPermissionDel(self._cw, action=action, eid=self.eidfrom,
                                    group_eid=self.eidto)
-        else: # RQLExpression
+        else:  # RQLExpression
             expr = self._cw.entity_from_eid(self.eidto).expression
             MemSchemaPermissionDel(self._cw, action=action, eid=self.eidfrom,
                                    expr=expr)
 
 
-
 class UpdateFTIndexOp(hook.DataOperationMixIn, hook.SingleLastOperation):
     """operation to update full text indexation of entity whose schema change
 
@@ -1410,11 +1408,8 @@
             cnx.cnxset.commit()
 
 
-
-
 # specializes synchronization hooks ############################################
 
-
 class AfterAddSpecializesHook(SyncSchemaHook):
     __regid__ = 'syncaddspecializes'
     __select__ = SyncSchemaHook.__select__ & hook.match_rtype('specializes')