server/checkintegrity.py
branchstable
changeset 4675 9233a8350420
parent 4252 6c4f109c2b03
child 4691 ae468fae9965
equal deleted inserted replaced
4674:3d509dbb473a 4675:9233a8350420
    62                 for container in etype_fti_containers(targeteschema, _done):
    62                 for container in etype_fti_containers(targeteschema, _done):
    63                     yield container
    63                     yield container
    64     else:
    64     else:
    65         yield eschema
    65         yield eschema
    66 
    66 
    67 def reindex_entities(schema, session):
    67 def reindex_entities(schema, session, withpb=True):
    68     """reindex all entities in the repository"""
    68     """reindex all entities in the repository"""
    69     # deactivate modification_date hook since we don't want them
    69     # deactivate modification_date hook since we don't want them
    70     # to be updated due to the reindexation
    70     # to be updated due to the reindexation
    71     from cubicweb.server.repository import FTIndexEntityOp
    71     from cubicweb.server.repository import FTIndexEntityOp
    72     repo = session.repo
    72     repo = session.repo
    90             continue
    90             continue
    91         for container in etype_fti_containers(eschema):
    91         for container in etype_fti_containers(eschema):
    92             etypes.add(container)
    92             etypes.add(container)
    93     print 'Reindexing entities of type %s' % \
    93     print 'Reindexing entities of type %s' % \
    94           ', '.join(sorted(str(e) for e in etypes))
    94           ', '.join(sorted(str(e) for e in etypes))
    95     pb = ProgressBar(len(etypes) + 1)
    95     if withpb:
       
    96         pb = ProgressBar(len(etypes) + 1)
    96     # first monkey patch Entity.check to disable validation
    97     # first monkey patch Entity.check to disable validation
    97     from cubicweb.entity import Entity
    98     from cubicweb.entity import Entity
    98     _check = Entity.check
    99     _check = Entity.check
    99     Entity.check = lambda self, creation=False: True
   100     Entity.check = lambda self, creation=False: True
   100     # clear fti table first
   101     # clear fti table first
   101     session.system_sql('DELETE FROM %s' % session.repo.system_source.dbhelper.fti_table)
   102     session.system_sql('DELETE FROM %s' % session.repo.system_source.dbhelper.fti_table)
   102     pb.update()
   103     if withpb:
       
   104         pb.update()
   103     # reindex entities by generating rql queries which set all indexable
   105     # reindex entities by generating rql queries which set all indexable
   104     # attribute to their current value
   106     # attribute to their current value
   105     for eschema in etypes:
   107     for eschema in etypes:
   106         for entity in session.execute('Any X WHERE X is %s' % eschema).entities():
   108         for entity in session.execute('Any X WHERE X is %s' % eschema).entities():
   107             FTIndexEntityOp(session, entity=entity)
   109             FTIndexEntityOp(session, entity=entity)
   108         pb.update()
   110         if withpb:
       
   111             pb.update()
   109     # restore Entity.check
   112     # restore Entity.check
   110     Entity.check = _check
   113     Entity.check = _check
   111 
   114 
   112 
   115 
   113 def check_schema(schema, session, eids, fix=1):
   116 def check_schema(schema, session, eids, fix=1):
   274                 print >> sys.stderr, ' [FIXED]'
   277                 print >> sys.stderr, ' [FIXED]'
   275             else:
   278             else:
   276                 print >> sys.stderr
   279                 print >> sys.stderr
   277 
   280 
   278 
   281 
   279 def check(repo, cnx, checks, reindex, fix):
   282 def check(repo, cnx, checks, reindex, fix, withpb=True):
   280     """check integrity of instance's repository,
   283     """check integrity of instance's repository,
   281     using given user and password to locally connect to the repository
   284     using given user and password to locally connect to the repository
   282     (no running cubicweb server needed)
   285     (no running cubicweb server needed)
   283     """
   286     """
   284     session = repo._get_session(cnx.sessionid, setpool=True)
   287     session = repo._get_session(cnx.sessionid, setpool=True)
   295         if not fix:
   298         if not fix:
   296             print 'WARNING: Diagnostic run, nothing has been corrected'
   299             print 'WARNING: Diagnostic run, nothing has been corrected'
   297     if reindex:
   300     if reindex:
   298         cnx.rollback()
   301         cnx.rollback()
   299         session.set_pool()
   302         session.set_pool()
   300         reindex_entities(repo.schema, session)
   303         reindex_entities(repo.schema, session, withpb=withpb)
   301         cnx.commit()
   304         cnx.commit()