[hooks] base64-encode extid before inserting it into moved_entities

If we insert the wrong value we're never going to find these entities again... Closes #5741105.

authorJulien Cristau <julien.cristau@logilab.fr>
changeset5fc21bf2684f
branchdefault
phasepublic
hiddenno
parent revision#23545d4aa022 [tox] Add missing dependency on webtest for devtools tests
child revision#d4bd28d5fca8 [server/sources] make sure entity._cw is a Connection before calling Storages
files modified by this revision
hooks/metadata.py
sobjects/test/unittest_cwxmlparser.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1436186081 -7200
# Mon Jul 06 14:34:41 2015 +0200
# Node ID 5fc21bf2684f494e7ade9bcc5e653a411dc53b6c
# Parent 23545d4aa022fd419c54924f226fe95e3d7edbc8
[hooks] base64-encode extid before inserting it into moved_entities

If we insert the wrong value we're never going to find these entities
again... Closes #5741105.

diff --git a/hooks/metadata.py b/hooks/metadata.py
@@ -18,10 +18,11 @@
1  """Core hooks: set generic metadata"""
2 
3  __docformat__ = "restructuredtext en"
4 
5  from datetime import datetime
6 +from base64 import b64encode
7 
8  from cubicweb.predicates import is_instance
9  from cubicweb.server import hook
10  from cubicweb.server.edition import EditedEntity
11 
@@ -201,11 +202,13 @@
12              # we don't want the moved entity to be reimported later.  To
13              # distinguish this state, move the record from the 'entities' table
14              # to 'moved_entities'.  External source will then have consider
15              # case where `extid2eid` returns a negative eid as 'this entity was
16              # known but has been moved, ignore it'.
17 -            attrs = {'eid': entity.eid, 'extid': self._cw.entity_metas(entity.eid)['extid']}
18 +            extid = self._cw.entity_metas(entity.eid)['extid']
19 +            assert extid is not None
20 +            attrs = {'eid': entity.eid, 'extid': b64encode(extid).decode('ascii')}
21              self._cw.system_sql(syssource.sqlgen.insert('moved_entities', attrs), attrs)
22              attrs = {'type': entity.cw_etype, 'eid': entity.eid, 'extid': None,
23                       'asource': 'system'}
24              self._cw.system_sql(syssource.sqlgen.update('entities', attrs, ['eid']), attrs)
25              # register an operation to update repository/sources caches
diff --git a/sobjects/test/unittest_cwxmlparser.py b/sobjects/test/unittest_cwxmlparser.py
@@ -265,11 +265,15 @@
26                                                        'type': 'EmailAddress',
27                                                        'extid': None})
28              self.assertEqual(e.cw_source[0].name, 'system')
29              self.assertEqual(e.reverse_use_email[0].login, 'sthenault')
30              # test everything is still fine after source synchronization
31 +            # clear caches to make sure we look at the moved_entities table
32 +            self.repo._type_source_cache.clear()
33 +            self.repo._extid_cache.clear()
34              stats = dfsource.pull_data(cnx, force=True, raise_on_error=True)
35 +            self.assertEqual(stats['updated'], set((email.eid,)))
36              rset = cnx.execute('EmailAddress X WHERE X address "syt@logilab.fr"')
37              self.assertEqual(len(rset), 1)
38              e = rset.get_entity(0, 0)
39              self.assertEqual(e.eid, email.eid)
40              self.assertEqual(e.cw_metainformation(), {'source': {'type': u'native', 'uri': u'system',