cubicweb/misc/migration/3.23.0_Any.py
author Julien Cristau <julien.cristau@logilab.fr>
Wed, 16 Mar 2016 11:56:32 +0100
changeset 11215 4e79acdc36a6
child 11360 49aca289134f
permissions -rw-r--r--
[schema] use json to serialize constraints Require yams 0.43: constraints are serialized to json, which means we need to recreate the actual checks in the database on upgrade. Temporary deps in tox.ini to pull respective changes in yams.

# we changed constraint serialization, which also changes their name
from cubicweb.server.schema2sql import check_constraint

helper = repo.system_source.dbhelper

for table, cstr in sql("""
    SELECT table_name, constraint_name FROM information_schema.constraint_column_usage
    WHERE constraint_name LIKE 'cstr%'"""):
    sql("ALTER TABLE %(table)s DROP CONSTRAINT %(cstr)s" % locals())

for cwconstraint in rql('Any C WHERE R constrained_by C').entities():
    cwrdef = cwconstraint.reverse_constrained_by[0]
    rdef = cwrdef.yams_schema()
    cstr = rdef.constraint_by_eid(cwconstraint.eid)
    if cstr.type() not in ('BoundaryConstraint', 'IntervalBoundConstraint',
                           'StaticVocabularyConstraint'):
        # These cannot be translate into backend CHECK.
        continue
    cstrname, check = check_constraint(rdef.subject, rdef.object, rdef.rtype.type,
                                       cstr, helper, prefix='cw_')
    args = {'e': rdef.subject.type, 'c': cstrname, 'v': check}
    sql('ALTER TABLE cw_%(e)s ADD CONSTRAINT %(c)s CHECK(%(v)s)' % args)

commit()