fix entities cleanup: source entities may be used in some relations
authorsylvain.thenault@logilab.fr
Fri, 09 Jan 2009 17:00:01 +0100
changeset 382 03964dd370e7
parent 381 e51deabc9b6a
child 384 f117f6cefad0
fix entities cleanup: source entities may be used in some relations
server/sources/__init__.py
--- a/server/sources/__init__.py	Fri Jan 09 16:59:59 2009 +0100
+++ b/server/sources/__init__.py	Fri Jan 09 17:00:01 2009 +0100
@@ -134,8 +134,36 @@
         this source. This should be called when a source is removed to
         properly cleanup the database
         """
+        cu = session.system_sql('SELECT eid FROM entities WHERE source=%(uri)s',
+                           {'uri': self.uri})
+        myeids = ','.join(str(r[0]) for r in cu.fetchall())
         # fti / entities tables cleanup
         dbhelper = session.pool.source('system').dbhelper
+        # delete relations referencing one of those eids
+        for rschema in self.schema.relations():
+            if rschema.is_final() or rschema.type == 'identity':
+                continue
+            if rschema.inlined:
+                for subjtype in rschema.subjects():
+                    for objtype in rschema.objects(subjtype):
+                        if self.support_entity(objtype):
+                            sql = 'UPDATE %s SET %s = NULL WHERE eid IN (%s);' % (
+                                subjtype, rschema.type, myeids)
+                            session.system_sql(sql)
+                            break
+                continue
+            for etype in rschema.subjects():
+                if self.support_entity(etype):
+                    sql = 'DELETE FROM %s_relation WHERE eid_from IN (%s);' % (
+                        rschema.type, myeids)
+                    session.system_sql(sql)
+                    break
+            for etype in rschema.objects():
+                if self.support_entity(etype):
+                    sql = 'DELETE FROM %s_relation WHERE eid_to IN (%s);' % (
+                        rschema.type, eid)
+                    session.system_sql(sql)
+                    break
         # sqlite doesn't support DELETE FROM xxx USING yyy
         session.system_sql('DELETE FROM %s WHERE %s.%s IN (SELECT eid FROM '
                            'entities WHERE entities.source=%%(uri)s)'
@@ -144,8 +172,8 @@
                            {'uri': self.uri})
         session.system_sql('DELETE FROM entities WHERE source=%(uri)s',
                            {'uri': self.uri})
-
-    # abstract methods to overide (at least) in concrete source classes #######
+        
+    # abstract methods to override (at least) in concrete source classes #######
     
     def get_connection(self):
         """open and return a connection to the source"""