server/checkintegrity.py
branchstable
changeset 5339 b83327846450
parent 5338 3e5a256d17ba
child 5340 4de474016568
equal deleted inserted replaced
5338:3e5a256d17ba 5339:b83327846450
    17 
    17 
    18 from cubicweb.schema import PURE_VIRTUAL_RTYPES
    18 from cubicweb.schema import PURE_VIRTUAL_RTYPES
    19 from cubicweb.server.sqlutils import SQL_PREFIX
    19 from cubicweb.server.sqlutils import SQL_PREFIX
    20 from cubicweb.server.session import security_enabled
    20 from cubicweb.server.session import security_enabled
    21 
    21 
    22 def has_eid(sqlcursor, eid, eids):
    22 def has_eid(session, sqlcursor, eid, eids):
    23     """return true if the eid is a valid eid"""
    23     """return true if the eid is a valid eid"""
    24     if eids.has_key(eid):
    24     if eids.has_key(eid):
    25         return eids[eid]
    25         return eids[eid]
    26     sqlcursor.execute('SELECT type, source FROM entities WHERE eid=%s' % eid)
    26     sqlcursor.execute('SELECT type, source FROM entities WHERE eid=%s' % eid)
    27     try:
    27     try:
    28         etype, source = sqlcursor.fetchone()
    28         etype, source = sqlcursor.fetchone()
    29     except:
    29     except:
    30         eids[eid] = False
    30         eids[eid] = False
    31         return False
    31         return False
    32     if source and source != 'system':
    32     if source and source != 'system':
    33         # XXX what to do...
    33         try:
    34         eids[eid] = True
    34             # insert eid *and* etype to attempt checking entity has not been
    35         return True
    35             # replaced by another subsquently to a restore of an old dump
       
    36             if session.execute('Any X WHERE X is %s, X eid %%(x)s' % etype,
       
    37                                {'x': eid}):
       
    38                 eids[eid] = True
       
    39                 return True
       
    40         except: # TypeResolverError, Unauthorized...
       
    41             pass
       
    42         eids[eid] = False
       
    43         return False
    36     sqlcursor.execute('SELECT * FROM %s%s WHERE %seid=%s' % (SQL_PREFIX, etype,
    44     sqlcursor.execute('SELECT * FROM %s%s WHERE %seid=%s' % (SQL_PREFIX, etype,
    37                                                              SQL_PREFIX, eid))
    45                                                              SQL_PREFIX, eid))
    38     result = sqlcursor.fetchall()
    46     result = sqlcursor.fetchall()
    39     if len(result) == 0:
    47     if len(result) == 0:
    40         eids[eid] = False
    48         eids[eid] = False
   128     """check all entities registered in the text index"""
   136     """check all entities registered in the text index"""
   129     print 'Checking text index'
   137     print 'Checking text index'
   130     cursor = session.system_sql('SELECT uid FROM appears;')
   138     cursor = session.system_sql('SELECT uid FROM appears;')
   131     for row in cursor.fetchall():
   139     for row in cursor.fetchall():
   132         eid = row[0]
   140         eid = row[0]
   133         if not has_eid(cursor, eid, eids):
   141         if not has_eid(session, cursor, eid, eids):
   134             msg = '  Entity with eid %s exists in the text index but in no source'
   142             msg = '  Entity with eid %s exists in the text index but in no source'
   135             print >> sys.stderr, msg % eid,
   143             print >> sys.stderr, msg % eid,
   136             if fix:
   144             if fix:
   137                 session.system_sql('DELETE FROM appears WHERE uid=%s;' % eid)
   145                 session.system_sql('DELETE FROM appears WHERE uid=%s;' % eid)
   138                 print >> sys.stderr, ' [FIXED]'
   146                 print >> sys.stderr, ' [FIXED]'
   144     """check all entities registered in the repo system table"""
   152     """check all entities registered in the repo system table"""
   145     print 'Checking entities system table'
   153     print 'Checking entities system table'
   146     cursor = session.system_sql('SELECT eid FROM entities;')
   154     cursor = session.system_sql('SELECT eid FROM entities;')
   147     for row in cursor.fetchall():
   155     for row in cursor.fetchall():
   148         eid = row[0]
   156         eid = row[0]
   149         if not has_eid(cursor, eid, eids):
   157         if not has_eid(session, cursor, eid, eids):
   150             msg = '  Entity with eid %s exists in the system table but in no source'
   158             msg = '  Entity with eid %s exists in the system table but in no source'
   151             print >> sys.stderr, msg % eid,
   159             print >> sys.stderr, msg % eid,
   152             if fix:
   160             if fix:
   153                 session.system_sql('DELETE FROM entities WHERE eid=%s;' % eid)
   161                 session.system_sql('DELETE FROM entities WHERE eid=%s;' % eid)
   154                 print >> sys.stderr, ' [FIXED]'
   162                 print >> sys.stderr, ' [FIXED]'
   197                 sql = 'SELECT %s FROM %s WHERE %s IS NOT NULL;' % (
   205                 sql = 'SELECT %s FROM %s WHERE %s IS NOT NULL;' % (
   198                     column, table, column)
   206                     column, table, column)
   199                 cursor = session.system_sql(sql)
   207                 cursor = session.system_sql(sql)
   200                 for row in cursor.fetchall():
   208                 for row in cursor.fetchall():
   201                     eid = row[0]
   209                     eid = row[0]
   202                     if not has_eid(cursor, eid, eids):
   210                     if not has_eid(session, cursor, eid, eids):
   203                         bad_related_msg(rschema, 'object', eid, fix)
   211                         bad_related_msg(rschema, 'object', eid, fix)
   204                         if fix:
   212                         if fix:
   205                             sql = 'UPDATE %s SET %s=NULL WHERE %s=%s;' % (
   213                             sql = 'UPDATE %s SET %s=NULL WHERE %s=%s;' % (
   206                                 table, column, column, eid)
   214                                 table, column, column, eid)
   207                             session.system_sql(sql)
   215                             session.system_sql(sql)
   208             continue
   216             continue
   209         cursor = session.system_sql('SELECT eid_from FROM %s_relation;' % rschema)
   217         cursor = session.system_sql('SELECT eid_from FROM %s_relation;' % rschema)
   210         for row in cursor.fetchall():
   218         for row in cursor.fetchall():
   211             eid = row[0]
   219             eid = row[0]
   212             if not has_eid(cursor, eid, eids):
   220             if not has_eid(session, cursor, eid, eids):
   213                 bad_related_msg(rschema, 'subject', eid, fix)
   221                 bad_related_msg(rschema, 'subject', eid, fix)
   214                 if fix:
   222                 if fix:
   215                     sql = 'DELETE FROM %s_relation WHERE eid_from=%s;' % (
   223                     sql = 'DELETE FROM %s_relation WHERE eid_from=%s;' % (
   216                         rschema, eid)
   224                         rschema, eid)
   217                     session.system_sql(sql)
   225                     session.system_sql(sql)
   218         cursor = session.system_sql('SELECT eid_to FROM %s_relation;' % rschema)
   226         cursor = session.system_sql('SELECT eid_to FROM %s_relation;' % rschema)
   219         for row in cursor.fetchall():
   227         for row in cursor.fetchall():
   220             eid = row[0]
   228             eid = row[0]
   221             if not has_eid(cursor, eid, eids):
   229             if not has_eid(session, cursor, eid, eids):
   222                 bad_related_msg(rschema, 'object', eid, fix)
   230                 bad_related_msg(rschema, 'object', eid, fix)
   223                 if fix:
   231                 if fix:
   224                     sql = 'DELETE FROM %s_relation WHERE eid_to=%s;' % (
   232                     sql = 'DELETE FROM %s_relation WHERE eid_to=%s;' % (
   225                         rschema, eid)
   233                         rschema, eid)
   226                     session.system_sql(sql)
   234                     session.system_sql(sql)