[schema sync] Update repo._type_source_cache when renaming an entity (closes #2094470)
authorJulien Cristau <julien.cristau@logilab.fr>
Mon, 05 Dec 2011 13:37:40 +0100
changeset 8105 7980b36fb1aa
parent 8104 71d9fb78b772
child 8109 67d540b7f8c8
[schema sync] Update repo._type_source_cache when renaming an entity (closes #2094470) When an entity type is renamed as part of a migration, it leaves stale entries in repo._type_source_cache, which makes subsequent operations on those entities fail (they look for the old entity name instead of the new one).
hooks/syncschema.py
server/test/data/migratedapp/schema.py
server/test/data/schema.py
server/test/unittest_migractions.py
--- a/hooks/syncschema.py	Thu Dec 01 12:48:46 2011 +0100
+++ b/hooks/syncschema.py	Mon Dec 05 13:37:40 2011 +0100
@@ -300,6 +300,9 @@
         self.info('renamed table %s to %s', oldname, newname)
         sqlexec('UPDATE entities SET type=%(newname)s WHERE type=%(oldname)s',
                 {'newname': newname, 'oldname': oldname})
+        for eid, (etype, uri, extid, auri) in self.session.repo._type_source_cache.items():
+            if etype == oldname:
+                self.session.repo._type_source_cache[eid] = (newname, uri, extid, auri)
         sqlexec('UPDATE deleted_entities SET type=%(newname)s WHERE type=%(oldname)s',
                 {'newname': newname, 'oldname': oldname})
         # XXX transaction records
--- a/server/test/data/migratedapp/schema.py	Thu Dec 01 12:48:46 2011 +0100
+++ b/server/test/data/migratedapp/schema.py	Mon Dec 05 13:37:40 2011 +0100
@@ -120,6 +120,10 @@
     concerne2 = SubjectRelation(('Affaire', 'Note'), cardinality='1*')
     connait = SubjectRelation('Personne', symmetric=True)
 
+
+class New(EntityType):
+    new_name = String()
+
 class Societe(WorkflowableEntityType):
     __permissions__ = {
         'read': ('managers', 'users', 'guests'),
--- a/server/test/data/schema.py	Thu Dec 01 12:48:46 2011 +0100
+++ b/server/test/data/schema.py	Mon Dec 05 13:37:40 2011 +0100
@@ -128,6 +128,9 @@
     inline2 = SubjectRelation('Affaire', inlined=True, cardinality='?*')
 
 
+class Old(EntityType):
+    name = String()
+
 
 class connait(RelationType):
     symmetric = True
--- a/server/test/unittest_migractions.py	Thu Dec 01 12:48:46 2011 +0100
+++ b/server/test/unittest_migractions.py	Mon Dec 05 13:37:40 2011 +0100
@@ -234,6 +234,12 @@
         self.assertFalse(self.execute('State X WHERE NOT X state_of WF'))
         self.assertFalse(self.execute('Transition X WHERE NOT X transition_of WF'))
 
+    def test_rename_entity_type(self):
+        entity = self.mh.create_entity('Old', name=u'old')
+        self.repo.type_and_source_from_eid(entity.eid)
+        self.mh.cmd_rename_entity_type('Old', 'New')
+        self.mh.cmd_rename_attribute('New', 'name', 'new_name')
+
     def test_add_drop_relation_type(self):
         self.mh.cmd_add_entity_type('Folder2', auto=False)
         self.mh.cmd_add_relation_type('filed_under2')