[migration] add sql constraints on upgrade
authorJulien Cristau <julien.cristau@logilab.fr>
Fri, 15 May 2015 23:05:54 +0200
changeset 10448 de5be53e2ea8
parent 10447 d309c020d9fb
child 10456 e7ee508a8b2f
[migration] add sql constraints on upgrade Related to #5154406.
misc/migration/3.21.0_Any.py
--- a/misc/migration/3.21.0_Any.py	Sun Mar 22 19:10:13 2015 +0100
+++ b/misc/migration/3.21.0_Any.py	Fri May 15 23:05:54 2015 +0200
@@ -21,3 +21,16 @@
 sync_schema_props_perms('CWEType')
 
 sync_schema_props_perms('cwuri')
+
+from cubicweb.server.schema2sql import check_constraint
+
+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'):
+        continue
+    cstrname, check = check_constraint(rdef.subject, rdef.object, rdef.rtype.type,
+            cstr, helper, prefix='cw_')
+    sql('ALTER TABLE %s%s ADD CONSTRAINT %s CHECK(%s)' % ('cw_', rdef.subject.type, cstrname, check))
+commit()