[WIP] [massive store] allow stores not to drop constraints

Provide a PGHelper alternative class that doesn't drop constraints nor indexes

TODO: drop=False won't actually work since we insert into cw_<etype> table before inserting in the entities table and therefore it will violate foreign key constraints

authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
changeset03b8919de73d
branchdefault
phasedraft
hiddenno
parent revision#a4d465a3e77d fix(ci): manually remove the .tox/doc directory
child revision<not specified>
files modified by this revision
cubicweb/dataimport/massive_store.py
# HG changeset patch
# User Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
# Date 1476365971 -7200
# Thu Oct 13 15:39:31 2016 +0200
# Node ID 03b8919de73d1863b70a4cf824a2bd38195e2c3d
# Parent a4d465a3e77d07cf6a79c121c10b2d6484cd7468
[WIP] [massive store] allow stores not to drop constraints

Provide a PGHelper alternative class that doesn't drop
constraints nor indexes

TODO: drop=False won't actually work since we insert into
cw_<etype> table before inserting in the ``entities`` table
and therefore it will violate foreign key constraints

diff --git a/cubicweb/dataimport/massive_store.py b/cubicweb/dataimport/massive_store.py
@@ -58,11 +58,13 @@
1 
2      Full-text indexation is not handled, you'll have to reindex the proper entity types by yourself
3      if desired.
4      """
5 
6 -    def __init__(self, cnx, slave_mode=False, eids_seq_range=10000, metagen=None):
7 +    def __init__(self, cnx, slave_mode=False,
8 +                 eids_seq_range=10000, metagen=None,
9 +                 drop=True):
10          """Create a MassiveObject store, with the following arguments:
11 
12          - `cnx`, a connection to the repository
13          - `metagen`, optional :class:`MetadataGenerator` instance
14          - `eids_seq_range`: size of eid range reserved by the store for each batch
@@ -79,11 +81,14 @@
15          self.sql = cnx.system_sql
16          self.schema = cnx.vreg.schema
17          self.default_values = get_default_values(self.schema)
18          self.get_next_eid = lambda g=self._get_eid_gen(eids_seq_range): next(g)
19          self._source_dbhelper = cnx.repo.system_source.dbhelper
20 -        self._dbh = PGHelper(cnx)
21 +        if drop:
22 +            self._dbh = PGHelper(cnx)
23 +        else:
24 +            self._dbh = NoDropPGHelper(cnx)
25 
26          self._data_entities = defaultdict(list)
27          self._data_relations = defaultdict(list)
28          self._initialized = {}
29 
@@ -444,5 +449,20 @@
30          """Return the SQL to be used to recreate the constraint."""
31          return self.sql('SELECT pg_get_constraintdef(c.oid) FROM pg_catalog.pg_constraint c '
32                          'LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.connamespace '
33                          'WHERE c.conname = %(r)s AND n.nspname=%(n)s',
34                          {'r': name, 'n': self.pg_schema}).fetchone()[0]
35 +
36 +
37 +class NoDropPGHelper(PGHelper):
38 +    """Custom PGHelper that doesn't drop any index nor constraint."""
39 +
40 +    def drop_indexes(self, tablename):
41 +        """Drop indexes and constraints, storing them in a table for later restore."""
42 +        pass
43 +
44 +    def drop_constraints(self, tablename):
45 +        pass
46 +
47 +    def restore_indexes_and_constraints(self):
48 +        """Restore indexes and constraints."""
49 +        pass