[migration] when adding a new attribute with a default value, set this value on existing entities (test and fix) stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 01 Oct 2009 14:50:10 +0200
branchstable
changeset 3543 ed152fe5aa8b
parent 3542 4c92cd09882f
child 3544 188d86631c45
[migration] when adding a new attribute with a default value, set this value on existing entities (test and fix)
server/schemahooks.py
server/test/data/migratedapp/schema.py
server/test/unittest_migractions.py
--- a/server/schemahooks.py	Thu Oct 01 09:41:41 2009 +0200
+++ b/server/schemahooks.py	Thu Oct 01 14:50:10 2009 +0200
@@ -349,6 +349,7 @@
             eschema = self.schema.eschema(rdef.subject)
         except KeyError:
             return # entity type currently being added
+        # propagate attribute to children classes
         rschema = self.schema.rschema(rdef.name)
         props.update({'constraints': rdef.constraints,
                       'description': rdef.description,
@@ -361,6 +362,11 @@
             for rql, args in ss.frdef2rql(rschema, str(specialization),
                                           rdef.object, props):
                 session.execute(rql, args)
+        # set default value, using sql for performance and to avoid
+        # modification_date update
+        if default:
+            session.system_sql('UPDATE %s SET %s=%%(default)s' % (table, column),
+                               {'default': default})
 
 
 class SourceDbCWRelationAdd(SourceDbCWAttributeAdd):
--- a/server/test/data/migratedapp/schema.py	Thu Oct 01 09:41:41 2009 +0200
+++ b/server/test/data/migratedapp/schema.py	Thu Oct 01 14:50:10 2009 +0200
@@ -50,7 +50,7 @@
                                              'PE require_permission P, P name "add_note", '
                                              'P require_group G'),)}
 
-    whatever = Int()  # keep it before `date` for unittest_migraction.test_add_attribute_int
+    whatever = Int(default=2)  # keep it before `date` for unittest_migraction.test_add_attribute_int
     date = Datetime()
     type = String(maxsize=1)
     mydate = Date(default='TODAY')
--- a/server/test/unittest_migractions.py	Thu Oct 01 09:41:41 2009 +0200
+++ b/server/test/unittest_migractions.py	Thu Oct 01 14:50:10 2009 +0200
@@ -48,12 +48,17 @@
 
     def test_add_attribute_int(self):
         self.failIf('whatever' in self.schema)
+        self.add_entity('Note')
+        self.commit()
         orderdict = dict(self.mh.rqlexec('Any RTN, O WHERE X name "Note", RDEF from_entity X, '
                                          'RDEF relation_type RT, RDEF ordernum O, RT name RTN'))
         self.mh.cmd_add_attribute('Note', 'whatever')
         self.failUnless('whatever' in self.schema)
         self.assertEquals(self.schema['whatever'].subjects(), ('Note',))
         self.assertEquals(self.schema['whatever'].objects(), ('Int',))
+        self.assertEquals(self.schema['Note'].default('whatever'), 2)
+        note = self.execute('Note X').get_entity(0, 0)
+        self.assertEquals(note.whatever, 2)
         orderdict2 = dict(self.mh.rqlexec('Any RTN, O WHERE X name "Note", RDEF from_entity X, '
                                           'RDEF relation_type RT, RDEF ordernum O, RT name RTN'))
         whateverorder = migrschema['whatever'].rproperty('Note', 'Int', 'order')