server/sources/native.py
changeset 10209 4c64a41c0a1d
parent 10208 249126034c0e
child 10221 17ad882a211f
--- a/server/sources/native.py	Fri Jan 16 12:43:32 2015 +0100
+++ b/server/sources/native.py	Fri Jan 16 12:35:46 2015 +0100
@@ -868,9 +868,10 @@
     def extid2eid(self, cnx, extid):
         """get eid from an external id. Return None if no record found."""
         assert isinstance(extid, str)
+        args = {'x': b64encode(extid)}
         cursor = self.doexec(cnx,
                              'SELECT eid FROM entities WHERE extid=%(x)s',
-                             {'x': b64encode(extid)})
+                             args)
         # XXX testing rowcount cause strange bug with sqlite, results are there
         #     but rowcount is 0
         #if cursor.rowcount > 0:
@@ -880,6 +881,17 @@
                 return result[0]
         except Exception:
             pass
+        cursor = self.doexec(cnx,
+                             'SELECT eid FROM moved_entities WHERE extid=%(x)s',
+                             args)
+        try:
+            result = cursor.fetchone()
+            if result:
+                # entity was moved to the system source, return negative
+                # number to tell the external source to ignore it
+                return -result[0]
+        except Exception:
+            pass
         return None
 
     def _handle_is_relation_sql(self, cnx, sql, attrs):
@@ -1390,6 +1402,10 @@
   extid VARCHAR(256) UNIQUE
 );;
 CREATE INDEX entities_type_idx ON entities(type);;
+CREATE TABLE moved_entities (
+  eid INTEGER PRIMARY KEY NOT NULL,
+  extid VARCHAR(256) UNIQUE
+);;
 
 CREATE TABLE transactions (
   tx_uuid CHAR(32) PRIMARY KEY NOT NULL,