[fti migration] test and fix reindexation of some specific entity types stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 12 Jul 2010 10:36:28 +0200
branchstable
changeset 5954 987086484876
parent 5942 f7768f44b4ac
child 5958 9bfa823735e0
[fti migration] test and fix reindexation of some specific entity types
server/checkintegrity.py
server/test/unittest_checkintegrity.py
--- a/server/checkintegrity.py	Thu Jul 08 15:30:53 2010 +0200
+++ b/server/checkintegrity.py	Mon Jul 12 10:36:28 2010 +0200
@@ -94,11 +94,15 @@
     # to be updated due to the reindexation
     repo = session.repo
     cursor = session.pool['system']
-    if not repo.system_source.dbhelper.has_fti_table(cursor):
+    dbhelper = session.repo.system_source.dbhelper
+    if not dbhelper.has_fti_table(cursor):
         print 'no text index table'
-        repo.system_source.dbhelper.init_fti(cursor)
+        dbhelper.init_fti(cursor)
     repo.system_source.do_fti = True  # ensure full-text indexation is activated
+    if withpb:
+        pb = ProgressBar(len(etypes) + 1)
     if etypes is None:
+        print 'Reindexing entities'
         etypes = set()
         for eschema in schema.entities():
             if eschema.final:
@@ -108,13 +112,16 @@
                 continue
             for container in etype_fti_containers(eschema):
                 etypes.add(container)
-    print 'Reindexing entities of type %s' % \
-          ', '.join(sorted(str(e) for e in etypes))
-    if withpb:
-        pb = ProgressBar(len(etypes) + 1)
-    # first monkey patch Entity.check to disable validation
-    # clear fti table first
-    session.system_sql('DELETE FROM %s' % session.repo.system_source.dbhelper.fti_table)
+        # clear fti table first
+        session.system_sql('DELETE FROM %s' % dbhelper.fti_table)
+    else:
+        print 'Reindexing entities of type %s' % \
+              ', '.join(sorted(str(e) for e in etypes))
+        # clear fti table first. Use subquery for sql compatibility
+        session.system_sql("DELETE FROM %s WHERE EXISTS(SELECT 1 FROM ENTITIES "
+                           "WHERE eid=%s AND type IN (%s))" % (
+                               dbhelper.fti_table, dbhelper.fti_uid_attr,
+                               ','.join("'%s'" % etype for etype in etypes)))
     if withpb:
         pb.update()
     # reindex entities by generating rql queries which set all indexable
--- a/server/test/unittest_checkintegrity.py	Thu Jul 08 15:30:53 2010 +0200
+++ b/server/test/unittest_checkintegrity.py	Mon Jul 12 10:36:28 2010 +0200
@@ -15,28 +15,47 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
-"""
 
-"""
 import sys
 from StringIO import StringIO
 from logilab.common.testlib import TestCase, unittest_main
 from cubicweb.devtools import init_test_database
 
 
-from cubicweb.server.checkintegrity import check
+from cubicweb.server.checkintegrity import check, reindex_entities
 
 class CheckIntegrityTC(TestCase):
-    def test(self):
-        repo, cnx = init_test_database()
+    def setUp(self):
+        self.repo, self.cnx = init_test_database()
+        self.execute = self.cnx.cursor().execute
+        self.session = self.repo._sessions[self.cnx.sessionid]
         sys.stderr = sys.stdout = StringIO()
-        try:
-            check(repo, cnx, ('entities', 'relations', 'text_index', 'metadata'),
-                  reindex=True, fix=True, withpb=False)
-        finally:
-            sys.stderr = sys.__stderr__
-            sys.stdout = sys.__stdout__
-        repo.shutdown()
+
+    def tearDown(self):
+        sys.stderr = sys.__stderr__
+        sys.stdout = sys.__stdout__
+        self.cnx.close()
+        self.repo.shutdown()
+
+    def test_checks(self):
+        check(self.repo, self.cnx, ('entities', 'relations', 'text_index', 'metadata'),
+              reindex=False, fix=True, withpb=False)
+
+    def test_reindex_all(self):
+        self.execute('INSERT Personne X: X nom "toto", X prenom "tutu"')
+        self.session.commit(False)
+        self.failUnless(self.execute('Any X WHERE X has_text "tutu"'))
+        reindex_entities(self.repo.schema, self.session, withpb=False)
+        self.failUnless(self.execute('Any X WHERE X has_text "tutu"'))
+
+    def test_reindex_etype(self):
+        self.execute('INSERT Personne X: X nom "toto", X prenom "tutu"')
+        self.execute('INSERT Affaire X: X ref "toto"')
+        self.session.commit(False)
+        reindex_entities(self.repo.schema, self.session, withpb=False,
+                         etypes=('Personne',))
+        self.failUnless(self.execute('Any X WHERE X has_text "tutu"'))
+        self.failUnless(self.execute('Any X WHERE X has_text "toto"'))
 
 if __name__ == '__main__':
     unittest_main()