[PATCH saem_ref] [migration] Fix "container" relation table and schema

Denis Laxalde denis.laxalde at logilab.fr
Fri Oct 19 09:24:58 CEST 2018

# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1539933331 -7200
#      Fri Oct 19 09:15:31 2018 +0200
# Node ID 802343c3d460266cfa8bbe61d39f472af02afe79
# Parent  b18f8b0019e0211b7ac54a2d5d0fbc4966125751
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 802343c3d460
[migration] Fix "container" relation table and schema

It has been noticed on running instances that the "container" relation
table (named container_relation) contains rows with their eid_from column
corresponding to entities of type Activity which is not supposed to be
subject of this relation.
This comes from changeset 03590369788c in which this entity type was
added to the entity types to be skipped when building the compound graph
of SEDAArchiveTransfer entity type (this graph is then used to
programmatically create relation definitions in cubicweb-seda). Yet, at
that time, not database migration was done, thus leading to the
inconsistent state we have now. (One noticeable and annoying issue is
that some entities cannot be deleted due to foreign keys from these
spurious rows).

So we fix the database by dropping the relation definitions from schema
and then deleting rows of the container_relation table. A manual
deletion, in SQL, is needed because deletion of relation definition
would not trigger data deletion (as RQL would apparently ignore rows not
matching know subject entity type).

diff --git a/cubicweb_saem_ref/migration/0.21.2_Any.py b/cubicweb_saem_ref/migration/0.21.2_Any.py
new file mode 100644
--- /dev/null
+++ b/cubicweb_saem_ref/migration/0.21.2_Any.py
@@ -0,0 +1,8 @@
+container_etypes = ('SEDAArchiveTransfer', 'SEDAArchiveUnit')
+for otype in container_etypes:
+    drop_relation_definition('Activity', 'container', otype)
+sql("DELETE FROM container_relation WHERE eid_from IN"
+    " (SELECT c.eid_from FROM container_relation AS c"
+    "   JOIN entities AS e ON e.eid=c.eid_from WHERE e.type='Activity');")

More information about the saem-devel mailing list