Commit b3101354 authored by Philippe Pepiot's avatar Philippe Pepiot
Browse files

[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.
parent db81a99e9dd1
......@@ -27,6 +27,7 @@ from logilab.common.decorators import cached
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 @@ class IUserFriendlyCheckConstraint(IUserFriendlyError):
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
......
......@@ -37,6 +37,7 @@ from cubicweb import validation_error
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 @@ class CWConstraintDelOp(MemSchemaOperation):
'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 @@ class CWConstraintAddOp(hook.LateOperation, CWConstraintDelOp):
# 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))
......
......@@ -1177,14 +1177,13 @@ class CubicWebSchema(Schema):
# 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):
......
......@@ -27,6 +27,8 @@ from yams.constraints import (SizeConstraint, UniqueConstraint, Attribute,
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 @@ def check_constraint(rdef, constraint, dbhelper, prefix=''):
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)
......
......@@ -31,6 +31,7 @@ from cubicweb import (ConfigurationError, ValidationError,
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 @@ class MigrationCommandsTC(MigrationTC):
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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment