add migration script for the new cwuri metadata attribute
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Fri, 24 Jul 2009 09:30:04 +0200
changeset 2463 5200c0f7d2d5
parent 2462 9e670072884d
child 2464 926696e38502
add migration script for the new cwuri metadata attribute
misc/migration/bootstrapmigration_repository.py
server/hooks.py
--- a/misc/migration/bootstrapmigration_repository.py	Fri Jul 24 09:28:59 2009 +0200
+++ b/misc/migration/bootstrapmigration_repository.py	Fri Jul 24 09:30:04 2009 +0200
@@ -10,6 +10,22 @@
 
 applcubicwebversion, cubicwebversion = versions_map['cubicweb']
 
+if applcubicwebversion < (3, 4, 0) and cubicwebversion >= (3, 4, 0):
+    from cubicweb.server.hooks import uniquecstrcheck_before_modification
+    session.set_shared_data('do-not-insert-cwuri', True)
+    repo.hm.unregister_hook(uniquecstrcheck_before_modification, 'before_add_entity', '')
+    repo.hm.unregister_hook(uniquecstrcheck_before_modification, 'before_update_entity', '')
+    add_relation_type('cwuri')
+    base_url = session.base_url()
+    # use an internal session since some entity might forbid modifications to admin
+    isession = repo.internal_session()
+    for eid, in rql('Any X', ask_confirm=False):
+        isession.execute('SET X cwuri %(u)s WHERE X eid %(x)s',
+                         {'x': eid, 'u': base_url + u'eid/%s' % eid})
+    isession.commit()
+    repo.hm.register_hook(uniquecstrcheck_before_modification, 'before_add_entity', '')
+    repo.hm.register_hook(uniquecstrcheck_before_modification, 'before_update_entity', '')
+
 if applcubicwebversion < (3, 2, 2) and cubicwebversion >= (3, 2, 1):
     from base64 import b64encode
     for table in ('entities', 'deleted_entities'):
--- a/server/hooks.py	Fri Jul 24 09:28:59 2009 +0200
+++ b/server/hooks.py	Fri Jul 24 09:30:04 2009 +0200
@@ -34,7 +34,8 @@
     if not 'modification_date' in entity:
         entity['modification_date'] = datetime.now()
     if not 'cwuri' in entity:
-        entity['cwuri'] = session.base_url() + u'eid/%s' % entity.eid
+        if not session.get_shared_data('do-not-insert-cwuri'):
+            entity['cwuri'] = session.base_url() + u'eid/%s' % entity.eid
 
 def setmtime_before_update_entity(session, entity):
     """update an entity -> set modification date"""