wip db-check rql constraints

authorJulien Cristau <julien.cristau@logilab.fr>
changeset914524af2d82
branchdefault
phasedraft
hiddenno
parent revision#f5221f6eebc4 Check constraints on upgrade to 3.22.0
child revision<not specified>
files modified by this revision
cubicweb/server/checkintegrity.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1445431138 -7200
# Wed Oct 21 14:38:58 2015 +0200
# Node ID 914524af2d82bcbdf86d0411224188143a2cd4c0
# Parent f5221f6eebc443e78eaecb55a4e929b4de8e2832
wip db-check rql constraints

diff --git a/cubicweb/server/checkintegrity.py b/cubicweb/server/checkintegrity.py
@@ -26,10 +26,11 @@
1 
2  from logilab.common.shellutils import ProgressBar
3 
4  from yams.constraints import UniqueConstraint
5 
6 +from cubicweb import ValidationError
7  from cubicweb.toolsutils import underline_title
8  from cubicweb.schema import PURE_VIRTUAL_RTYPES, VIRTUAL_RTYPES, UNIQUE_CONSTRAINTS
9  from cubicweb.server.sqlutils import SQL_PREFIX
10  from cubicweb.server.schema2sql import iter_unique_index_names, build_index_name
11 
@@ -407,10 +408,30 @@
12                                         % (table, column, eidcolumn, eid),
13                                         {'v': default})
14                  notify_fixed(fix)
15 
16 
17 +def check_rqlconstraints(schema, cnx, eids, fix=1):
18 +    print('Checking RQL constraints')
19 +    for rdefeid, cstreid in cnx.execute('Any RD, C WHERE RD constrained_by C, '
20 +            'C cstrtype CT, CT name IN ("RQLConstraint", "RQLUniqueConstraint")'):
21 +        rdef = schema.schema_by_eid(rdefeid)
22 +        constraint = rdef.constraint_by_eid(cstreid)
23 +        msg = '%s (autofix will remove the relation)\n'
24 +        for eidfrom, eidto in cnx.execute('Any S, O WHERE S is %(s)s, O is %(o)s, S %(r)s O' %
25 +                                          {'s': rdef.subject.type,
26 +                                           'o': rdef.object.type,
27 +                                           'r': rdef.rtype.type}):
28 +            try:
29 +                constraint.repo_check(cnx, eidfrom, rdef.rtype.type, eidto)
30 +            except ValidationError as exc:
31 +                sys.stderr.write(msg % exc)
32 +                if fix:
33 +                    cnx.delete_relation(eidfrom, rdef.rtype.type, eidto)
34 +                notify_fixed(fix)
35 +
36 +
37  def check(repo, cnx, checks, reindex, fix, withpb=True):
38      """check integrity of instance's repository,
39      using given user and password to locally connect to the repository
40      (no running cubicweb server needed)
41      """