# HG changeset patch # User Philippe Pepiot # Date 1552322603 -3600 # Node ID 661dd0436c015bb7244018358dd344b54a498701 # Parent db81a99e9dd11981ab831d85dc0c5fdc4e2e1550 [schema] drop monkeypatch "name_for" on BaseConstraint BaseConstraint doesn't have "name_for" method, this is just an addition for cubicweb needs. Implement this as a function instead of a monkeypatch to avoid having "name_for" not defined because cubicweb.schema hasn't been imported. In particular this fix running cubicweb/server/test/unittest_schema2sql.py::SQLSchemaTC::test_known_values alone. diff -r db81a99e9dd1 -r 661dd0436c01 cubicweb/entities/adapters.py --- a/cubicweb/entities/adapters.py Wed Mar 13 10:21:39 2019 +0100 +++ b/cubicweb/entities/adapters.py Mon Mar 11 17:43:23 2019 +0100 @@ -27,6 +27,7 @@ from cubicweb import (Unauthorized, ValidationError, view, ViolatedConstraint, UniqueTogetherError) +from cubicweb.schema import constraint_name_for from cubicweb.predicates import is_instance, relation_possible, match_exception @@ -474,7 +475,7 @@ for rschema, attrschema in eschema.attribute_definitions(): rdef = rschema.rdef(eschema, attrschema) for constraint in rdef.constraints: - if cstrname == constraint.name_for(rdef): + if cstrname == constraint_name_for(constraint, rdef): break else: continue diff -r db81a99e9dd1 -r 661dd0436c01 cubicweb/hooks/syncschema.py --- a/cubicweb/hooks/syncschema.py Wed Mar 13 10:21:39 2019 +0100 +++ b/cubicweb/hooks/syncschema.py Mon Mar 11 17:43:23 2019 +0100 @@ -37,6 +37,7 @@ from cubicweb.predicates import is_instance from cubicweb.schema import (SCHEMA_TYPES, META_RTYPES, VIRTUAL_RTYPES, CONSTRAINTS, UNIQUE_CONSTRAINTS, ETYPE_NAME_MAP) +from cubicweb.schema import constraint_name_for from cubicweb.server import hook, schemaserial as ss, schema2sql as y2sql from cubicweb.server.sqlutils import SQL_PREFIX from cubicweb.hooks.synccomputed import RecomputeAttributeOperation @@ -758,7 +759,7 @@ 'IntervalBoundConstraint', 'StaticVocabularyConstraint'): cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s' - % (SQL_PREFIX, rdef.subject, self.oldcstr.name_for(rdef))) + % (SQL_PREFIX, rdef.subject, constraint_name_for(self.oldcstr, rdef))) def revertprecommit_event(self): # revert changes on in memory schema @@ -812,7 +813,7 @@ # oldcstr is the new constraint when the attribute is being added in the same # transaction or when constraint value is updated. So we've to take care... if oldcstr is not None: - oldcstrname = self.oldcstr.name_for(rdef) + oldcstrname = constraint_name_for(self.oldcstr, rdef) if oldcstrname != cstrname: cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s' % (SQL_PREFIX, rdef.subject, oldcstrname)) diff -r db81a99e9dd1 -r 661dd0436c01 cubicweb/schema.py --- a/cubicweb/schema.py Wed Mar 13 10:21:39 2019 +0100 +++ b/cubicweb/schema.py Mon Mar 11 17:43:23 2019 +0100 @@ -1177,14 +1177,13 @@ # additional cw specific constraints ########################################### -@monkeypatch(BaseConstraint) -def name_for(self, rdef): +def constraint_name_for(constraint, rdef): """Return a unique, size controlled, name for this constraint applied to given `rdef`. This name may be used as name for the constraint in the database. """ - return 'cstr' + md5((rdef.subject.type + rdef.rtype.type + self.type() - + (self.serialize() or '')).encode('ascii')).hexdigest() + return 'cstr' + md5((rdef.subject.type + rdef.rtype.type + constraint.type() + + (constraint.serialize() or '')).encode('ascii')).hexdigest() class BaseRQLConstraint(RRQLExpression, BaseConstraint): diff -r db81a99e9dd1 -r 661dd0436c01 cubicweb/server/schema2sql.py --- a/cubicweb/server/schema2sql.py Wed Mar 13 10:21:39 2019 +0100 +++ b/cubicweb/server/schema2sql.py Mon Mar 11 17:43:23 2019 +0100 @@ -27,6 +27,8 @@ from logilab import database from logilab.common.decorators import monkeypatch +from cubicweb.schema import constraint_name_for + # default are usually not handled at the sql level. If you want them, set # SET_DEFAULT to True SET_DEFAULT = False @@ -187,7 +189,7 @@ constraint. Maybe (None, None) if the constraint is not handled in the backend. """ attr = rdef.rtype.type - cstrname = constraint.name_for(rdef) + cstrname = constraint_name_for(constraint, rdef) if constraint.type() == 'BoundaryConstraint': value = constraint_value_as_sql(constraint.boundary, dbhelper, prefix) return cstrname, '%s%s %s %s' % (prefix, attr, constraint.operator, value) diff -r db81a99e9dd1 -r 661dd0436c01 cubicweb/server/test/unittest_migractions.py --- a/cubicweb/server/test/unittest_migractions.py Wed Mar 13 10:21:39 2019 +0100 +++ b/cubicweb/server/test/unittest_migractions.py Mon Mar 11 17:43:23 2019 +0100 @@ -31,6 +31,7 @@ ExecutionError, Binary) from cubicweb.devtools import startpgcluster, stoppgcluster from cubicweb.devtools.testlib import CubicWebTC, TemporaryDirectory +from cubicweb.schema import constraint_name_for from cubicweb.server.sqlutils import SQL_PREFIX from cubicweb.server.migractions import ServerMigrationHelper from cubicweb.server.sources import storages @@ -616,7 +617,7 @@ self.assertEqual(len(constraints), 1, constraints) rdef = migrschema['promo'].rdefs['Personne', 'String'] cstr = rdef.constraint_by_type('StaticVocabularyConstraint') - self.assertIn(cstr.name_for(rdef), constraints) + self.assertIn(constraint_name_for(cstr, rdef), constraints) def _erqlexpr_rset(self, cnx, action, ertype): rql = 'RQLExpression X WHERE ET is CWEType, ET %s_permission X, ET name %%(name)s' % action