[migration] hackish black magic to bootstrap addition of formula attr during migration
authorJulien Cristau <julien.cristau@logilab.fr>
Thu, 13 Nov 2014 10:37:44 +0100
changeset 10025 7b72ecc3f4d2
parent 10024 2a08247b57fb
child 10039 1d1afe3b5081
[migration] hackish black magic to bootstrap addition of formula attr during migration Turns out we can't add an attribute to CWAttribute before the attribute exists. add_attribute generates rql with a 'formula' relation, which CW doesn't know about yet, so things get unhappy. Once that is fixed, we need to make the CWAttribute addition operation deal with CWAttribute entities that don't have a formula yet. Finally, move the migration to bootstrapmigration_repository so it happens early on, and use add_entity_type rather than add_relation_type for CWComputedRType.
hooks/syncschema.py
misc/migration/3.20.0_Any.py
misc/migration/bootstrapmigration_repository.py
server/schemaserial.py
--- a/hooks/syncschema.py	Tue Nov 04 14:10:02 2014 +0100
+++ b/hooks/syncschema.py	Thu Nov 13 10:37:44 2014 +0100
@@ -449,9 +449,11 @@
             default = default.unzpickle()
         props = {'default': default,
                  'indexed': entity.indexed,
-                 'formula': entity.formula,
                  'fulltextindexed': entity.fulltextindexed,
                  'internationalizable': entity.internationalizable}
+        # entity.formula may not exist yet if we're migrating to 3.20
+        if hasattr(entity, 'formula'):
+            props['formula'] = entity.formula
         # update the in-memory schema first
         rdefdef = self.init_rdef(**props)
         # then make necessary changes to the system source database
@@ -505,7 +507,7 @@
             cnx.system_sql('UPDATE %s SET %s=%%(default)s' % (table, column),
                                {'default': default})
         # if attribute is computed, compute it
-        if entity.formula:
+        if getattr(entity, 'formula', None):
             # add rtype attribute for RelationDefinitionSchema api compat, this
             # is what RecomputeAttributeOperation expect
             rdefdef.rtype = rdefdef.name
--- a/misc/migration/3.20.0_Any.py	Tue Nov 04 14:10:02 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,2 +0,0 @@
-add_relation_type('CWComputedRType')
-add_attribute('CWAttribute', 'formula')
--- a/misc/migration/bootstrapmigration_repository.py	Tue Nov 04 14:10:02 2014 +0100
+++ b/misc/migration/bootstrapmigration_repository.py	Thu Nov 13 10:37:44 2014 +0100
@@ -57,6 +57,14 @@
 
     replace_eid_sequence_with_eid_numrange(session)
 
+if applcubicwebversion < (3, 20, 0) and cubicwebversion >= (3, 20, 0):
+    ss._IGNORED_PROPS.append('formula')
+    add_attribute('CWAttribute', 'formula', commit=False)
+    ss._IGNORED_PROPS.remove('formula')
+    commit()
+    add_entity_type('CWComputedRType')
+    commit()
+
 if applcubicwebversion < (3, 17, 0) and cubicwebversion >= (3, 17, 0):
     try:
         add_cube('sioc', update_database=False)
--- a/server/schemaserial.py	Tue Nov 04 14:10:02 2014 +0100
+++ b/server/schemaserial.py	Thu Nov 13 10:37:44 2014 +0100
@@ -556,12 +556,14 @@
         for rql, args in _erperms2rql(rdef, groupmap):
             yield rql, args
 
+_IGNORED_PROPS = ['eid', 'constraints', 'uid', 'infered', 'permissions']
+
 def _rdef_values(rdef):
     amap = {'order': 'ordernum', 'default': 'defaultval'}
     values = {}
     extra = {}
     for prop in rdef.rproperty_defs(rdef.object):
-        if prop in ('eid', 'constraints', 'uid', 'infered', 'permissions'):
+        if prop in _IGNORED_PROPS:
             continue
         value = getattr(rdef, prop)
         if prop not in KNOWN_RPROPERTIES: