[schema2sql] Give a rdef to check_constraint
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 30 Jun 2016 13:25:43 +0200
changeset 11406 8ed625765a5c
parent 11397 eccb5c1e58c2
child 11407 e6acdea9616c
[schema2sql] Give a rdef to check_constraint This simplifies the API. Related to #14050899
cubicweb/hooks/syncschema.py
cubicweb/misc/migration/3.21.0_Any.py
cubicweb/misc/migration/3.23.0_Any.py
cubicweb/server/schema2sql.py
--- a/cubicweb/hooks/syncschema.py	Wed Jun 29 15:07:55 2016 +0200
+++ b/cubicweb/hooks/syncschema.py	Thu Jun 30 13:25:43 2016 +0200
@@ -815,8 +815,8 @@
                                             (self.oldcstr.serialize() or '')).encode('ascii')).hexdigest()
                 cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s' %
                                (SQL_PREFIX, rdef.subject.type, oldcstrname))
-            cstrname, check = y2sql.check_constraint(rdef.subject, rdef.object, rdef.rtype.type,
-                    newcstr, syssource.dbhelper, prefix=SQL_PREFIX)
+            cstrname, check = y2sql.check_constraint(rdef, newcstr, syssource.dbhelper,
+                                                     prefix=SQL_PREFIX)
             cnx.system_sql('ALTER TABLE %s%s ADD CONSTRAINT %s CHECK(%s)' %
                            (SQL_PREFIX, rdef.subject.type, cstrname, check))
 
--- a/cubicweb/misc/migration/3.21.0_Any.py	Wed Jun 29 15:07:55 2016 +0200
+++ b/cubicweb/misc/migration/3.21.0_Any.py	Thu Jun 30 13:25:43 2016 +0200
@@ -162,8 +162,7 @@
     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_')
+    cstrname, check = check_constraint(rdef, cstr, helper, prefix='cw_')
     args = {'e': rdef.subject.type, 'c': cstrname, 'v': check}
     if repo.system_source.dbdriver == 'postgres':
         sql('ALTER TABLE cw_%(e)s DROP CONSTRAINT IF EXISTS %(c)s' % args, ask_confirm=False)
--- a/cubicweb/misc/migration/3.23.0_Any.py	Wed Jun 29 15:07:55 2016 +0200
+++ b/cubicweb/misc/migration/3.23.0_Any.py	Thu Jun 30 13:25:43 2016 +0200
@@ -54,8 +54,7 @@
                            'StaticVocabularyConstraint'):
         # These cannot be translate into backend CHECK.
         continue
-    cstrname, check = check_constraint(rdef.subject, rdef.object, rdef.rtype.type,
-                                       cstr, helper, prefix='cw_')
+    cstrname, check = check_constraint(rdef, 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)
 
--- a/cubicweb/server/schema2sql.py	Wed Jun 29 15:07:55 2016 +0200
+++ b/cubicweb/server/schema2sql.py	Thu Jun 30 13:25:43 2016 +0200
@@ -160,11 +160,9 @@
     for rschema, aschema in attrs:
         if aschema is None:  # inline relation
             continue
-        attr = rschema.type
         rdef = rschema.rdef(eschema.type, aschema.type)
         for constraint in rdef.constraints:
-            cstrname, check = check_constraint(eschema, aschema, attr, constraint, dbhelper,
-                                               prefix=prefix)
+            cstrname, check = check_constraint(rdef, constraint, dbhelper, prefix=prefix)
             if cstrname is not None:
                 w(', CONSTRAINT %s CHECK(%s)' % (cstrname, check))
     w(');')
@@ -197,7 +195,12 @@
         return value
 
 
-def check_constraint(eschema, aschema, attr, constraint, dbhelper, prefix=''):
+def check_constraint(rdef, constraint, dbhelper, prefix=''):
+    """Return (constraint name, constraint SQL definition) for the given relation definition's
+    constraint. Maybe (None, None) if the constraint is not handled in the backend.
+    """
+    eschema = rdef.subject
+    attr = rdef.rtype.type
     # XXX should find a better name
     cstrname = 'cstr' + md5((eschema.type + attr + constraint.type() +
                              (constraint.serialize() or '')).encode('ascii')).hexdigest()