[massive store] Follow configuration of the metadata generator
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 28 Sep 2016 09:02:14 +0200
changeset 11780 307d96c0ab5a
parent 11779 9a3663ec4ead
child 11781 4ebd968f364c
[massive store] Follow configuration of the metadata generator Don't drop constraints and indexes for tables that are ignored by the metadata generator given to the store. One may now easily disable insertion of e.g. created_by / owned_by by removing them from the MetadataGenerator.META_RELATIONS set, in which case indexes for associated table won't be removed by the massive store.
cubicweb/dataimport/massive_store.py
cubicweb/dataimport/stores.py
cubicweb/dataimport/test/test_massive_store.py
--- a/cubicweb/dataimport/massive_store.py	Mon Oct 17 16:53:28 2016 +0200
+++ b/cubicweb/dataimport/massive_store.py	Wed Sep 28 09:02:14 2016 +0200
@@ -119,10 +119,12 @@
 
         They will be recreated by the `finish` method.
         """
-        for tablename in ('created_by_relation', 'owned_by_relation',
-                          'is_instance_of_relation', 'is_relation'):
-             self._dbh.drop_constraints(tablename)
-            self._dbh.drop_indexes(tablename)
+        rtypes = [rtype for rtype in self.metagen.meta_relations
+                  if not self.schema.rschema(rtype).final]
+        rtypes += ('is_instance_of', 'is', 'cw_source')
+        for rtype in rtypes:
+            self._dbh.drop_constraints(rtype + '_relation')
+            self._dbh.drop_indexes(rtype + '_relation')
         # don't drop constraints for the entities table, the only one is the primary key's index on
         # eid and we want to keep it
         self._dbh.drop_indexes('entities')
--- a/cubicweb/dataimport/stores.py	Mon Oct 17 16:53:28 2016 +0200
+++ b/cubicweb/dataimport/stores.py	Wed Sep 28 09:02:14 2016 +0200
@@ -416,10 +416,7 @@
     """
     __deprecation_warning__ = '[3.23] this class is deprecated, use MetadataGenerator instead'
 
-    META_RELATIONS = (META_RTYPES
-                      - VIRTUAL_RTYPES
-                      - set(('eid', 'cwuri',
-                             'is', 'is_instance_of', 'cw_source')))
+    META_RELATIONS = MetadataGenerator.META_RELATIONS
 
     def __init__(self, cnx, baseurl=None, source=None):
         self._cnx = cnx
--- a/cubicweb/dataimport/test/test_massive_store.py	Mon Oct 17 16:53:28 2016 +0200
+++ b/cubicweb/dataimport/test/test_massive_store.py	Wed Sep 28 09:02:14 2016 +0200
@@ -154,6 +154,19 @@
             self.assertIn(build_index_name('owned_by_relation', ['eid_from'], 'idx_'),
                           indexes)
 
+    def test_consider_metagen(self):
+        """Ensure index on owned_by is not deleted if we don't consider this metadata."""
+        with self.admin_access.repo_cnx() as cnx:
+            metagen = stores.MetadataGenerator(cnx, meta_skipped=('owned_by',))
+            store = MassiveObjectStore(cnx, metagen=metagen)
+
+            store._drop_constraints()
+            indexes = all_indexes(cnx)
+            self.assertIn(build_index_name('owned_by_relation', ['eid_from', 'eid_to'], 'key_'),
+                          indexes)
+            self.assertIn(build_index_name('owned_by_relation', ['eid_from'], 'idx_'),
+                          indexes)
+
     def test_eids_seq_range(self):
         with self.admin_access.repo_cnx() as cnx:
             store = MassiveObjectStore(cnx, eids_seq_range=1000)