cubicweb/misc/migration/3.23.0_Any.py
changeset 11363 e5fe836df6f1
parent 11360 49aca289134f
child 11364 a702d31ddd8f
equal deleted inserted replaced
11362:ebe75d73acdd 11363:e5fe836df6f1
     7 from cubicweb.server.schema2sql import build_index_name, check_constraint
     7 from cubicweb.server.schema2sql import build_index_name, check_constraint
     8 
     8 
     9 sql = partial(sql, ask_confirm=False)
     9 sql = partial(sql, ask_confirm=False)
    10 
    10 
    11 source = repo.system_source
    11 source = repo.system_source
       
    12 helper = source.dbhelper
    12 
    13 
    13 for rschema in schema.relations():
    14 for rschema in schema.relations():
    14     if rschema.rule or rschema in PURE_VIRTUAL_RTYPES:
    15     if rschema.rule or rschema in PURE_VIRTUAL_RTYPES:
    15         continue
    16         continue
    16     if rschema.final or rschema.inlined:
    17     if rschema.final or rschema.inlined:
    17         for rdef in rschema.rdefs.values():
    18         for rdef in rschema.rdefs.values():
    18             table = 'cw_{0}'.format(rdef.subject)
    19             table = 'cw_{0}'.format(rdef.subject)
    19             column = 'cw_{0}'.format(rdef.rtype)
    20             column = 'cw_{0}'.format(rdef.rtype)
    20             if any(isinstance(cstr, UniqueConstraint) for cstr in rdef.constraints):
    21             if any(isinstance(cstr, UniqueConstraint) for cstr in rdef.constraints):
    21                 old_name = '%s_%s_key' % (table.lower(), column.lower())
    22                 old_name = '%s_%s_key' % (table.lower(), column.lower())
    22                 sql('ALTER TABLE %s DROP CONSTRAINT %s' % (table, old_name))
    23                 sql('ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s' % (table, old_name))
    23                 source.create_index(cnx, table, column, unique=True)
    24                 source.create_index(cnx, table, column, unique=True)
    24             if rschema.inlined or rdef.indexed:
    25             if rschema.inlined or rdef.indexed:
    25                 old_name = '%s_%s_idx' % (table.lower(), column.lower())
    26                 old_name = '%s_%s_idx' % (table.lower(), column.lower())
    26                 sql('DROP INDEX IF EXISTS %s' % old_name)
    27                 sql('DROP INDEX IF EXISTS %s' % old_name)
    27                 source.create_index(cnx, table, column)
    28                 source.create_index(cnx, table, column)
    36                 % (build_index_name(table, ['eid_' + column], 'idx_'), table, column))
    37                 % (build_index_name(table, ['eid_' + column], 'idx_'), table, column))
    37 
    38 
    38 
    39 
    39 # we changed constraint serialization, which also changes their name
    40 # we changed constraint serialization, which also changes their name
    40 
    41 
    41 helper = repo.system_source.dbhelper
       
    42 
       
    43 for table, cstr in sql("""
    42 for table, cstr in sql("""
    44     SELECT table_name, constraint_name FROM information_schema.constraint_column_usage
    43     SELECT table_name, constraint_name FROM information_schema.constraint_column_usage
    45     WHERE constraint_name LIKE 'cstr%'"""):
    44     WHERE constraint_name LIKE 'cstr%'"""):
    46     sql("ALTER TABLE %(table)s DROP CONSTRAINT %(cstr)s" % locals())
    45     sql("ALTER TABLE %(table)s DROP CONSTRAINT IF EXISTS %(cstr)s" % locals())
    47 
    46 
    48 for cwconstraint in rql('Any C WHERE R constrained_by C').entities():
    47 for cwconstraint in rql('Any C WHERE R constrained_by C').entities():
    49     cwrdef = cwconstraint.reverse_constrained_by[0]
    48     cwrdef = cwconstraint.reverse_constrained_by[0]
    50     rdef = cwrdef.yams_schema()
    49     rdef = cwrdef.yams_schema()
    51     cstr = rdef.constraint_by_eid(cwconstraint.eid)
    50     cstr = rdef.constraint_by_eid(cwconstraint.eid)