server/checkintegrity.py
branchstable
changeset 9171 be9596750678
parent 8900 010a59e12d89
child 9463 d62e13eba033
child 9492 c7fc56eecd1a
equal deleted inserted replaced
9170:e6fe77dbcfdf 9171:be9596750678
    53                                {'x': eid}):
    53                                {'x': eid}):
    54                 eids[eid] = True
    54                 eids[eid] = True
    55                 return True
    55                 return True
    56         except Exception: # TypeResolverError, Unauthorized...
    56         except Exception: # TypeResolverError, Unauthorized...
    57             pass
    57             pass
       
    58         eids[eid] = False
       
    59         return False
       
    60     if etype not in session.vreg.schema:
    58         eids[eid] = False
    61         eids[eid] = False
    59         return False
    62         return False
    60     sqlcursor.execute('SELECT * FROM %s%s WHERE %seid=%s' % (SQL_PREFIX, etype,
    63     sqlcursor.execute('SELECT * FROM %s%s WHERE %seid=%s' % (SQL_PREFIX, etype,
    61                                                              SQL_PREFIX, eid))
    64                                                              SQL_PREFIX, eid))
    62     result = sqlcursor.fetchall()
    65     result = sqlcursor.fetchall()
   177 
   180 
   178 def check_entities(schema, session, eids, fix=1):
   181 def check_entities(schema, session, eids, fix=1):
   179     """check all entities registered in the repo system table"""
   182     """check all entities registered in the repo system table"""
   180     print 'Checking entities system table'
   183     print 'Checking entities system table'
   181     # system table but no source
   184     # system table but no source
   182     msg = '  Entity with eid %s exists in the system table but in no source (autofix will delete the entity)'
   185     msg = '  Entity %s with eid %s exists in the system table but in no source (autofix will delete the entity)'
   183     cursor = session.system_sql('SELECT eid FROM entities;')
   186     cursor = session.system_sql('SELECT eid,type FROM entities;')
   184     for row in cursor.fetchall():
   187     for row in cursor.fetchall():
   185         eid = row[0]
   188         eid, etype = row
   186         if not has_eid(session, cursor, eid, eids):
   189         if not has_eid(session, cursor, eid, eids):
   187             sys.stderr.write(msg % eid)
   190             sys.stderr.write(msg % (etype, eid))
   188             if fix:
   191             if fix:
   189                 session.system_sql('DELETE FROM entities WHERE eid=%s;' % eid)
   192                 session.system_sql('DELETE FROM entities WHERE eid=%s;' % eid)
   190             notify_fixed(fix)
   193             notify_fixed(fix)
   191     # source in entities, but no relation cw_source
   194     # source in entities, but no relation cw_source
   192     applcwversion = session.repo.get_versions().get('cubicweb')
   195     applcwversion = session.repo.get_versions().get('cubicweb')
   256 def bad_related_msg(rtype, target, eid, fix):
   259 def bad_related_msg(rtype, target, eid, fix):
   257     msg = '  A relation %s with %s eid %s exists but no such entity in sources'
   260     msg = '  A relation %s with %s eid %s exists but no such entity in sources'
   258     sys.stderr.write(msg % (rtype, target, eid))
   261     sys.stderr.write(msg % (rtype, target, eid))
   259     notify_fixed(fix)
   262     notify_fixed(fix)
   260 
   263 
       
   264 def bad_inlined_msg(rtype, parent_eid, eid, fix):
       
   265     msg = ('  An inlined relation %s from %s to %s exists but the latter '
       
   266            'entity does not exist')
       
   267     sys.stderr.write(msg % (rtype, parent_eid, eid))
       
   268     notify_fixed(fix)
       
   269 
   261 
   270 
   262 def check_relations(schema, session, eids, fix=1):
   271 def check_relations(schema, session, eids, fix=1):
   263     """check that eids referenced by relations are registered in the repo system
   272     """check that eids referenced by relations are registered in the repo system
   264     table
   273     table
   265     """
   274     """
   269             continue
   278             continue
   270         if rschema.inlined:
   279         if rschema.inlined:
   271             for subjtype in rschema.subjects():
   280             for subjtype in rschema.subjects():
   272                 table = SQL_PREFIX + str(subjtype)
   281                 table = SQL_PREFIX + str(subjtype)
   273                 column = SQL_PREFIX +  str(rschema)
   282                 column = SQL_PREFIX +  str(rschema)
   274                 sql = 'SELECT %s FROM %s WHERE %s IS NOT NULL;' % (
   283                 sql = 'SELECT cw_eid,%s FROM %s WHERE %s IS NOT NULL;' % (
   275                     column, table, column)
   284                     column, table, column)
   276                 cursor = session.system_sql(sql)
   285                 cursor = session.system_sql(sql)
   277                 for row in cursor.fetchall():
   286                 for row in cursor.fetchall():
   278                     eid = row[0]
   287                     parent_eid, eid = row
   279                     if not has_eid(session, cursor, eid, eids):
   288                     if not has_eid(session, cursor, eid, eids):
   280                         bad_related_msg(rschema, 'object', eid, fix)
   289                         bad_inlined_msg(rschema, parent_eid, eid, fix)
   281                         if fix:
   290                         if fix:
   282                             sql = 'UPDATE %s SET %s=NULL WHERE %s=%s;' % (
   291                             sql = 'UPDATE %s SET %s=NULL WHERE %s=%s;' % (
   283                                 table, column, column, eid)
   292                                 table, column, column, eid)
   284                             session.system_sql(sql)
   293                             session.system_sql(sql)
   285             continue
   294             continue
   364     print 'Checking metadata'
   373     print 'Checking metadata'
   365     cursor = session.system_sql("SELECT DISTINCT type FROM entities;")
   374     cursor = session.system_sql("SELECT DISTINCT type FROM entities;")
   366     eidcolumn = SQL_PREFIX + 'eid'
   375     eidcolumn = SQL_PREFIX + 'eid'
   367     msg = '  %s with eid %s has no %s (autofix will set it to now)'
   376     msg = '  %s with eid %s has no %s (autofix will set it to now)'
   368     for etype, in cursor.fetchall():
   377     for etype, in cursor.fetchall():
       
   378         if etype not in session.vreg.schema:
       
   379             sys.stderr.write('entities table references unknown type %s\n' %
       
   380                              etype)
       
   381             if fix:
       
   382                 session.system_sql("DELETE FROM entities WHERE type = %(type)s",
       
   383                                    {'type': etype})
       
   384             continue
   369         table = SQL_PREFIX + etype
   385         table = SQL_PREFIX + etype
   370         for rel, default in ( ('creation_date', datetime.now()),
   386         for rel, default in ( ('creation_date', datetime.now()),
   371                               ('modification_date', datetime.now()), ):
   387                               ('modification_date', datetime.now()), ):
   372             column = SQL_PREFIX + rel
   388             column = SQL_PREFIX + rel
   373             cursor = session.system_sql("SELECT %s FROM %s WHERE %s is NULL"
   389             cursor = session.system_sql("SELECT %s FROM %s WHERE %s is NULL"