# HG changeset patch # User Sylvain Thénault # Date 1467285943 -7200 # Node ID 8ed625765a5cab49a03f18172f5aad675241b88a # Parent eccb5c1e58c2b1af2d1a2a7904294f93875efc28 [schema2sql] Give a rdef to check_constraint This simplifies the API. Related to #14050899 diff -r eccb5c1e58c2 -r 8ed625765a5c cubicweb/hooks/syncschema.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)) diff -r eccb5c1e58c2 -r 8ed625765a5c cubicweb/misc/migration/3.21.0_Any.py --- 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) diff -r eccb5c1e58c2 -r 8ed625765a5c cubicweb/misc/migration/3.23.0_Any.py --- 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) diff -r eccb5c1e58c2 -r 8ed625765a5c cubicweb/server/schema2sql.py --- 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()