cubicweb/misc/migration/3.23.0_Any.py
changeset 11360 49aca289134f
parent 11215 4e79acdc36a6
child 11363 e5fe836df6f1
equal deleted inserted replaced
11359:2da2dd60331c 11360:49aca289134f
       
     1 
       
     2 from functools import partial
       
     3 
       
     4 from yams.constraints import UniqueConstraint
       
     5 
       
     6 from cubicweb.schema import PURE_VIRTUAL_RTYPES
       
     7 from cubicweb.server.schema2sql import build_index_name, check_constraint
       
     8 
       
     9 sql = partial(sql, ask_confirm=False)
       
    10 
       
    11 source = repo.system_source
       
    12 
       
    13 for rschema in schema.relations():
       
    14     if rschema.rule or rschema in PURE_VIRTUAL_RTYPES:
       
    15         continue
       
    16     if rschema.final or rschema.inlined:
       
    17         for rdef in rschema.rdefs.values():
       
    18             table = 'cw_{0}'.format(rdef.subject)
       
    19             column = 'cw_{0}'.format(rdef.rtype)
       
    20             if any(isinstance(cstr, UniqueConstraint) for cstr in rdef.constraints):
       
    21                 old_name = '%s_%s_key' % (table.lower(), column.lower())
       
    22                 sql('ALTER TABLE %s DROP CONSTRAINT %s' % (table, old_name))
       
    23                 source.create_index(cnx, table, column, unique=True)
       
    24             if rschema.inlined or rdef.indexed:
       
    25                 old_name = '%s_%s_idx' % (table.lower(), column.lower())
       
    26                 sql('DROP INDEX IF EXISTS %s' % old_name)
       
    27                 source.create_index(cnx, table, column)
       
    28     else:
       
    29         table = '{0}_relation'.format(rschema)
       
    30         sql('ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s_p_key' % (table, table))
       
    31         sql('ALTER TABLE %s ADD CONSTRAINT %s PRIMARY KEY(eid_from, eid_to)'
       
    32             % (table, build_index_name(table, ['eid_from', 'eid_to'], 'key_')))
       
    33         for column in ('from', 'to'):
       
    34             sql('DROP INDEX IF EXISTS %s_%s_idx' % (table, column))
       
    35             sql('CREATE INDEX %s ON %s(eid_%s);'
       
    36                 % (build_index_name(table, ['eid_' + column], 'idx_'), table, column))
       
    37 
       
    38 
     1 # we changed constraint serialization, which also changes their name
    39 # we changed constraint serialization, which also changes their name
     2 from cubicweb.server.schema2sql import check_constraint
       
     3 
    40 
     4 helper = repo.system_source.dbhelper
    41 helper = repo.system_source.dbhelper
     5 
    42 
     6 for table, cstr in sql("""
    43 for table, cstr in sql("""
     7     SELECT table_name, constraint_name FROM information_schema.constraint_column_usage
    44     SELECT table_name, constraint_name FROM information_schema.constraint_column_usage
    20                                        cstr, helper, prefix='cw_')
    57                                        cstr, helper, prefix='cw_')
    21     args = {'e': rdef.subject.type, 'c': cstrname, 'v': check}
    58     args = {'e': rdef.subject.type, 'c': cstrname, 'v': check}
    22     sql('ALTER TABLE cw_%(e)s ADD CONSTRAINT %(c)s CHECK(%(v)s)' % args)
    59     sql('ALTER TABLE cw_%(e)s ADD CONSTRAINT %(c)s CHECK(%(v)s)' % args)
    23 
    60 
    24 commit()
    61 commit()
       
    62