# HG changeset patch # User Sylvain Thénault # Date 1299164757 -3600 # Node ID 63386b35ec697d15d2f852cf9d1e0a2fa30a8e31 # Parent 8d2cf36bd79d73b85b0b8c6a919a3095b24b406a [c-c db-check] new checks for entities missing a mandatory relation/attribute diff -r 8d2cf36bd79d -r 63386b35ec69 server/checkintegrity.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 diff -r 8d2cf36bd79d -r 63386b35ec69 server/serverctl.py --- 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' : '', - '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',