[c-c db-check] new checks for entities missing a mandatory relation/attribute stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 03 Mar 2011 16:05:57 +0100
branchstable
changeset 7036 63386b35ec69
parent 7035 8d2cf36bd79d
child 7037 6a8235456fe1
[c-c db-check] new checks for entities missing a mandatory relation/attribute
server/checkintegrity.py
server/serverctl.py
--- a/server/checkintegrity.py	Thu Mar 03 16:05:34 2011 +0100
+++ b/server/checkintegrity.py	Thu Mar 03 16:05:57 2011 +0100
@@ -264,6 +264,54 @@
                     session.system_sql(sql)
 
 
+def check_mandatory_relations(schema, session, eids, fix=1):
+    """check entities missing some mandatory relation"""
+    print 'Checking mandatory relations'
+    for rschema in schema.relations():
+        if rschema.final or rschema in PURE_VIRTUAL_RTYPES:
+            continue
+        smandatory = set()
+        omandatory = set()
+        for rdef in rschema.rdefs.values():
+            if rdef.cardinality[0] in '1+':
+                smandatory.add(rdef.subject)
+            if rdef.cardinality[1] in '1+':
+                omandatory.add(rdef.object)
+        for role, etypes in (('subject', smandatory), ('object', omandatory)):
+            for etype in etypes:
+                if role == 'subject':
+                    rql = 'Any X WHERE NOT X %s Y, X is %s' % (rschema, etype)
+                else:
+                    rql = 'Any X WHERE NOT Y %s X, X is %s' % (rschema, etype)
+                for entity in session.execute(rql).entities():
+                    print >> sys.stderr, '%s #%s is missing mandatory %s relation %s' % (
+                        entity.__regid__, entity.eid, role, rschema)
+                    if fix:
+                        #if entity.cw_describe()['source']['uri'] == 'system': XXX
+                        entity.delete()
+                    notify_fixed(fix)
+
+
+def check_mandatory_attributes(schema, session, eids, fix=1):
+    """check for entities stored in the system source missing some mandatory
+    attribute
+    """
+    print 'Checking mandatory attributes'
+    for rschema in schema.relations():
+        if not rschema.final or rschema in VIRTUAL_RTYPES:
+            continue
+        for rdef in rschema.rdefs.values():
+            if rdef.cardinality[0] in '1+':
+                rql = 'Any X WHERE X %s NULL, X is %s, X cw_source S, S name "system"' % (
+                    rschema, rdef.subject)
+                for entity in session.execute(rql).entities():
+                    print >> sys.stderr, '%s #%s is missing mandatory attribute %s' % (
+                        entity.__regid__, entity.eid, rschema)
+                    if fix:
+                        entity.delete()
+                    notify_fixed(fix)
+
+
 def check_metadata(schema, session, eids, fix=1):
     """check entities has required metadata
 
--- a/server/serverctl.py	Thu Mar 03 16:05:34 2011 +0100
+++ b/server/serverctl.py	Thu Mar 03 16:05:57 2011 +0100
@@ -849,9 +849,12 @@
     options = (
         ('checks',
          {'short': 'c', 'type' : 'csv', 'metavar' : '<check list>',
-          'default' : ('entities', 'relations', 'metadata', 'schema', 'text_index'),
+          'default' : ('entities', 'relations',
+                       'mandatory_relations', 'mandatory_attributes',
+                       'metadata', 'schema', 'text_index'),
           'help': 'Comma separated list of check to run. By default run all \
-checks, i.e. entities, relations, text_index and metadata.'}
+checks, i.e. entities, relations, mandatory_relations, mandatory_attributes, \
+metadata, text_index and schema.'}
          ),
 
         ('autofix',