misc/migration/3.18.0_Any.py
changeset 9478 2d7521881d3d
parent 9460 a2a0bc984863
parent 9414 d8c0784038f4
child 9543 39f981482e34
equal deleted inserted replaced
9475:7863c546b135 9478:2d7521881d3d
     1 driver = config.system_source_config['db-driver']
     1 driver = config.system_source_config['db-driver']
     2 if not (driver == 'postgres' or driver.startswith('sqlserver')):
     2 if not (driver == 'postgres' or driver.startswith('sqlserver')):
     3     import sys
     3     import sys
     4     print >>sys.stderr, 'This migration is not supported for backends other than sqlserver or postgres (yet).'
     4     print >>sys.stderr, 'This migration is not supported for backends other than sqlserver or postgres (yet).'
     5     sys.exit(1)
     5     sys.exit(1)
       
     6 
       
     7 add_relation_definition('CWAttribute', 'add_permission', 'CWGroup')
       
     8 add_relation_definition('CWAttribute', 'add_permission', 'RQLExpression')
       
     9 
       
    10 # a bad defaultval in 3.13.8 schema was fixed in 3.13.9, but the migration was missed
       
    11 rql('SET ATTR defaultval NULL WHERE ATTR from_entity E, E name "CWSource", ATTR relation_type T, T name "in_synchronization"')
       
    12 
       
    13 # the migration gets confused when we change rdefs out from under it.  So
       
    14 # explicitly remove this size constraint so it doesn't stick around and break
       
    15 # things later.
       
    16 rdefeid = schema['defaultval'].rdefs.values()[0].eid
       
    17 rql('DELETE CWConstraint C WHERE C cstrtype T, T name "SizeConstraint", R constrained_by C, R eid %(eid)s', {'eid': rdefeid})
     6 
    18 
     7 sync_schema_props_perms('defaultval')
    19 sync_schema_props_perms('defaultval')
     8 
    20 
     9 def convert_defaultval(cwattr, default):
    21 def convert_defaultval(cwattr, default):
    10     from decimal import Decimal
    22     from decimal import Decimal
    11     import yams
    23     import yams
    12     from cubicweb import Binary
    24     from cubicweb import Binary
    13     if default is None:
    25     if default is None:
    14         return
    26         return
       
    27     if isinstance(default, Binary):
       
    28         # partially migrated instance, try to be idempotent
       
    29         return default
    15     atype = cwattr.to_entity[0].name
    30     atype = cwattr.to_entity[0].name
    16     if atype == 'Boolean':
    31     if atype == 'Boolean':
    17         # boolean attributes with default=False were stored as ''
    32         # boolean attributes with default=False were stored as ''
    18         assert default in ('True', 'False', ''), repr(default)
    33         assert default in ('True', 'False', ''), repr(default)
    19         default = default == 'True'
    34         default = default == 'True'
    55     sql("sp_rename 'cw_cwattribute.new_defaultval', 'cw_defaultval', 'COLUMN'")
    70     sql("sp_rename 'cw_cwattribute.new_defaultval', 'cw_defaultval', 'COLUMN'")
    56 
    71 
    57 
    72 
    58 # Set object type to "Bytes" for CWAttribute's "defaultval" attribute
    73 # Set object type to "Bytes" for CWAttribute's "defaultval" attribute
    59 rql('SET X to_entity B WHERE X is CWAttribute, X from_entity Y, Y name "CWAttribute", '
    74 rql('SET X to_entity B WHERE X is CWAttribute, X from_entity Y, Y name "CWAttribute", '
    60     'X relation_type Z, Z name "defaultval", B name "Bytes"')
    75     'X relation_type Z, Z name "defaultval", B name "Bytes", NOT X to_entity B')
    61 
    76 
    62 from yams import buildobjs as ybo
    77 schema['defaultval'].rdefs.values()[0].object = schema['Bytes']
    63 schema.add_relation_def(ybo.RelationDefinition('CWAttribute', 'defaultval', 'Bytes'))
       
    64 schema.del_relation_def('CWAttribute', 'defaultval', 'String')
       
    65 
    78 
    66 commit()
    79 commit()
       
    80 
       
    81 sync_schema_props_perms('defaultval')
    67 
    82 
    68 for rschema in schema.relations():
    83 for rschema in schema.relations():
    69     if rschema.symmetric:
    84     if rschema.symmetric:
    70         subjects = set(repr(e.type) for e in rschema.subjects())
    85         subjects = set(repr(e.type) for e in rschema.subjects())
    71         objects = set(repr(e.type) for e in rschema.objects())
    86         objects = set(repr(e.type) for e in rschema.objects())
    78             martians = ','.join(martians)
    93             martians = ','.join(martians)
    79             print 'deleting broken relations %s for eids %s' % (rschema.type, martians)
    94             print 'deleting broken relations %s for eids %s' % (rschema.type, martians)
    80             sql('DELETE FROM %s_relation WHERE eid_from IN (%s) OR eid_to IN (%s)' % (rschema.type, martians, martians))
    95             sql('DELETE FROM %s_relation WHERE eid_from IN (%s) OR eid_to IN (%s)' % (rschema.type, martians, martians))
    81         with session.deny_all_hooks_but():
    96         with session.deny_all_hooks_but():
    82             rql('SET X %(r)s Y WHERE Y %(r)s X, NOT X %(r)s Y' % {'r': rschema.type})
    97             rql('SET X %(r)s Y WHERE Y %(r)s X, NOT X %(r)s Y' % {'r': rschema.type})
    83     commit()
    98         commit()
    84 
    99 
    85 
   100 
    86 # multi columns unique constraints regeneration
   101 # multi columns unique constraints regeneration
    87 from cubicweb.server import schemaserial
   102 from cubicweb.server import schemaserial
    88 
   103 
   118             args['x'] = eschema.eid
   133             args['x'] = eschema.eid
   119             session.execute(rql, args)
   134             session.execute(rql, args)
   120         commit()
   135         commit()
   121 
   136 
   122 
   137 
   123 add_relation_definition('CWAttribute', 'add_permission', 'CWGroup')
       
   124 add_relation_definition('CWAttribute', 'add_permission', 'RQLExpression')
       
   125 
       
   126 # all attributes perms have to be refreshed ...
   138 # all attributes perms have to be refreshed ...
   127 for rschema in schema.relations():
   139 for rschema in schema.relations():
   128     if relation.final:
   140     if rschema.final:
   129         sync_schema_props_perms(rschema.type, syncprops=False)
   141         sync_schema_props_perms(rschema.type, syncprops=False)