[migration] Fix migration to deduplicate agents
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 13 Apr 2017 12:28:06 +0200
changeset 3233 99589d2ecaf8
parent 3232 dc010accae1f
child 3234 7e1a8f1c1102
[migration] Fix migration to deduplicate agents This migration script introduce a unique together constraint on Agent(authority, name), but it appears that e.g. there are several agent with the same name within the same authority in our demo instance's database (historically due to the way EAC import was working, but this shouldn't happen anymore). So prior to synchronize Agent type, we should handle this else attempt to add unique constraint on (authority, name) will fail. Deduplicate is done naivly by keeping one arbitrarily and remove all others, because test instances should not care that much and we don't want to spend more time on this.
cubicweb_saem_ref/migration/0.15.1_Any.py
--- a/cubicweb_saem_ref/migration/0.15.1_Any.py	Fri Apr 14 10:35:48 2017 +0200
+++ b/cubicweb_saem_ref/migration/0.15.1_Any.py	Thu Apr 13 12:28:06 2017 +0200
@@ -1,3 +1,10 @@
+# quick and dirty deduplicate of agent with duplicated name within an authority
+for agent in rql('DISTINCT Any X WHERE X is Agent, X name XN, X authority A, '
+                 'Y is Agent, Y name XN, Y authority A, '
+                 'Y eid YE, X eid >YE').entities():
+    agent.cw_delete()
+commit()
+
 for ertype in ('generated', 'used', 'associated_with', 'place_address', 'new_version_of',
                'authority_record',
                'OrganizationUnit', 'Agent', 'ArkNameAssigningAuthority'):