cubicweb/misc/migration/3.23.0_Any.py
changeset 11215 4e79acdc36a6
child 11360 49aca289134f
equal deleted inserted replaced
11212:3309ddb97059 11215:4e79acdc36a6
       
     1 # we changed constraint serialization, which also changes their name
       
     2 from cubicweb.server.schema2sql import check_constraint
       
     3 
       
     4 helper = repo.system_source.dbhelper
       
     5 
       
     6 for table, cstr in sql("""
       
     7     SELECT table_name, constraint_name FROM information_schema.constraint_column_usage
       
     8     WHERE constraint_name LIKE 'cstr%'"""):
       
     9     sql("ALTER TABLE %(table)s DROP CONSTRAINT %(cstr)s" % locals())
       
    10 
       
    11 for cwconstraint in rql('Any C WHERE R constrained_by C').entities():
       
    12     cwrdef = cwconstraint.reverse_constrained_by[0]
       
    13     rdef = cwrdef.yams_schema()
       
    14     cstr = rdef.constraint_by_eid(cwconstraint.eid)
       
    15     if cstr.type() not in ('BoundaryConstraint', 'IntervalBoundConstraint',
       
    16                            'StaticVocabularyConstraint'):
       
    17         # These cannot be translate into backend CHECK.
       
    18         continue
       
    19     cstrname, check = check_constraint(rdef.subject, rdef.object, rdef.rtype.type,
       
    20                                        cstr, helper, prefix='cw_')
       
    21     args = {'e': rdef.subject.type, 'c': cstrname, 'v': check}
       
    22     sql('ALTER TABLE cw_%(e)s ADD CONSTRAINT %(c)s CHECK(%(v)s)' % args)
       
    23 
       
    24 commit()