author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 01 Oct 2010 18:49:47 +0200 | |
branch | stable |
changeset 6385 | 9f91d09ee5fa |
parent 6185 | 229006accd26 |
child 6624 | b30e5428048b |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6127
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
18 |
"""Integrity checking tool for instances: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
19 |
|
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
20 |
* integrity of a CubicWeb repository. Hum actually only the system database is |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
21 |
checked. |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
22 |
|
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
23 |
* consistency of multi-sources instance mapping file |
5999
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5954
diff
changeset
|
24 |
""" |
0 | 25 |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
26 |
from __future__ import with_statement |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
27 |
|
0 | 28 |
__docformat__ = "restructuredtext en" |
29 |
||
30 |
import sys |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
31 |
from datetime import datetime |
0 | 32 |
|
33 |
from logilab.common.shellutils import ProgressBar |
|
34 |
||
6127
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
35 |
from cubicweb.schema import META_RTYPES, VIRTUAL_RTYPES, PURE_VIRTUAL_RTYPES |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
36 |
from cubicweb.server.sqlutils import SQL_PREFIX |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
37 |
from cubicweb.server.session import security_enabled |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
38 |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
39 |
def has_eid(session, sqlcursor, eid, eids): |
0 | 40 |
"""return true if the eid is a valid eid""" |
5341
0de53140bd29
[db-check] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5340
diff
changeset
|
41 |
if eid in eids: |
0 | 42 |
return eids[eid] |
43 |
sqlcursor.execute('SELECT type, source FROM entities WHERE eid=%s' % eid) |
|
44 |
try: |
|
45 |
etype, source = sqlcursor.fetchone() |
|
46 |
except: |
|
47 |
eids[eid] = False |
|
48 |
return False |
|
49 |
if source and source != 'system': |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
50 |
try: |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
51 |
# insert eid *and* etype to attempt checking entity has not been |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
52 |
# replaced by another subsquently to a restore of an old dump |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
53 |
if session.execute('Any X WHERE X is %s, X eid %%(x)s' % etype, |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
54 |
{'x': eid}): |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
55 |
eids[eid] = True |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
56 |
return True |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
57 |
except: # TypeResolverError, Unauthorized... |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
58 |
pass |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
59 |
eids[eid] = False |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
60 |
return False |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
61 |
sqlcursor.execute('SELECT * FROM %s%s WHERE %seid=%s' % (SQL_PREFIX, etype, |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
62 |
SQL_PREFIX, eid)) |
0 | 63 |
result = sqlcursor.fetchall() |
64 |
if len(result) == 0: |
|
65 |
eids[eid] = False |
|
66 |
return False |
|
67 |
elif len(result) > 1: |
|
68 |
msg = ' More than one entity with eid %s exists in source !' |
|
69 |
print >> sys.stderr, msg % eid |
|
70 |
print >> sys.stderr, ' WARNING : Unable to fix this, do it yourself !' |
|
71 |
eids[eid] = True |
|
72 |
return True |
|
73 |
||
74 |
# XXX move to yams? |
|
75 |
def etype_fti_containers(eschema, _done=None): |
|
76 |
if _done is None: |
|
77 |
_done = set() |
|
78 |
_done.add(eschema) |
|
79 |
containers = tuple(eschema.fulltext_containers()) |
|
80 |
if containers: |
|
81 |
for rschema, target in containers: |
|
82 |
if target == 'object': |
|
83 |
targets = rschema.objects(eschema) |
|
84 |
else: |
|
85 |
targets = rschema.subjects(eschema) |
|
86 |
for targeteschema in targets: |
|
87 |
if targeteschema in _done: |
|
88 |
continue |
|
89 |
_done.add(targeteschema) |
|
90 |
for container in etype_fti_containers(targeteschema, _done): |
|
91 |
yield container |
|
92 |
else: |
|
93 |
yield eschema |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
94 |
|
5850
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
95 |
def reindex_entities(schema, session, withpb=True, etypes=None): |
0 | 96 |
"""reindex all entities in the repository""" |
97 |
# deactivate modification_date hook since we don't want them |
|
98 |
# to be updated due to the reindexation |
|
99 |
repo = session.repo |
|
2248
cbf043a2134a
try to create fti table if not existant on rebuild-fti
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
100 |
cursor = session.pool['system'] |
5954
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
101 |
dbhelper = session.repo.system_source.dbhelper |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
102 |
if not dbhelper.has_fti_table(cursor): |
2248
cbf043a2134a
try to create fti table if not existant on rebuild-fti
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
103 |
print 'no text index table' |
5954
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
104 |
dbhelper.init_fti(cursor) |
4806
4f12f59b1a13
[fti] refactor and fix full text indexation handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4691
diff
changeset
|
105 |
repo.system_source.do_fti = True # ensure full-text indexation is activated |
5850
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
106 |
if etypes is None: |
5954
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
107 |
print 'Reindexing entities' |
5850
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
108 |
etypes = set() |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
109 |
for eschema in schema.entities(): |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
110 |
if eschema.final: |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
111 |
continue |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
112 |
indexable_attrs = tuple(eschema.indexable_attributes()) # generator |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
113 |
if not indexable_attrs: |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
114 |
continue |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
115 |
for container in etype_fti_containers(eschema): |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
116 |
etypes.add(container) |
5954
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
117 |
# clear fti table first |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
118 |
session.system_sql('DELETE FROM %s' % dbhelper.fti_table) |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
119 |
else: |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
120 |
print 'Reindexing entities of type %s' % \ |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
121 |
', '.join(sorted(str(e) for e in etypes)) |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
122 |
# clear fti table first. Use subquery for sql compatibility |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
123 |
session.system_sql("DELETE FROM %s WHERE EXISTS(SELECT 1 FROM ENTITIES " |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
124 |
"WHERE eid=%s AND type IN (%s))" % ( |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
125 |
dbhelper.fti_table, dbhelper.fti_uid_attr, |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
126 |
','.join("'%s'" % etype for etype in etypes))) |
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
127 |
if withpb: |
6112
913979c79244
[db-fti-index] simple fix fpr progressbar-related crash when etypes is None in reindex_entities()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5999
diff
changeset
|
128 |
pb = ProgressBar(len(etypes) + 1) |
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
129 |
pb.update() |
0 | 130 |
# reindex entities by generating rql queries which set all indexable |
131 |
# attribute to their current value |
|
4816
c02583cb80a9
repair stuff broken by fti handling changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4806
diff
changeset
|
132 |
source = repo.system_source |
0 | 133 |
for eschema in etypes: |
134 |
for entity in session.execute('Any X WHERE X is %s' % eschema).entities(): |
|
4816
c02583cb80a9
repair stuff broken by fti handling changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4806
diff
changeset
|
135 |
source.fti_index_entity(session, entity) |
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
136 |
if withpb: |
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
137 |
pb.update() |
0 | 138 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
139 |
|
380 | 140 |
def check_schema(schema, session, eids, fix=1): |
0 | 141 |
"""check serialized schema""" |
142 |
print 'Checking serialized schema' |
|
143 |
unique_constraints = ('SizeConstraint', 'FormatConstraint', |
|
5523
4bf975c049a6
[db-check] RQLConstraint is not a 'unique' constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
144 |
'VocabularyConstraint', |
0 | 145 |
'RQLVocabularyConstraint') |
5338
3e5a256d17ba
[db-check] fix duplicated schema constraint detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
146 |
rql = ('Any COUNT(X),RN,SN,ON,CTN GROUPBY RN,SN,ON,CTN ORDERBY 1 ' |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
147 |
'WHERE X is CWConstraint, R constrained_by X, ' |
5338
3e5a256d17ba
[db-check] fix duplicated schema constraint detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
148 |
'R relation_type RT, RT name RN, R from_entity ST, ST name SN, ' |
3e5a256d17ba
[db-check] fix duplicated schema constraint detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
149 |
'R to_entity OT, OT name ON, X cstrtype CT, CT name CTN') |
3e5a256d17ba
[db-check] fix duplicated schema constraint detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
150 |
for count, rn, sn, on, cstrname in session.execute(rql): |
0 | 151 |
if count == 1: |
152 |
continue |
|
153 |
if cstrname in unique_constraints: |
|
5338
3e5a256d17ba
[db-check] fix duplicated schema constraint detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
154 |
print "ERROR: got %s %r constraints on relation %s.%s.%s" % ( |
3e5a256d17ba
[db-check] fix duplicated schema constraint detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
155 |
count, cstrname, sn, rn, on) |
5523
4bf975c049a6
[db-check] RQLConstraint is not a 'unique' constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
156 |
if fix: |
4bf975c049a6
[db-check] RQLConstraint is not a 'unique' constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
157 |
print 'dunno how to fix, do it yourself' |
0 | 158 |
|
159 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
160 |
|
0 | 161 |
def check_text_index(schema, session, eids, fix=1): |
162 |
"""check all entities registered in the text index""" |
|
163 |
print 'Checking text index' |
|
164 |
cursor = session.system_sql('SELECT uid FROM appears;') |
|
165 |
for row in cursor.fetchall(): |
|
166 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
167 |
if not has_eid(session, cursor, eid, eids): |
0 | 168 |
msg = ' Entity with eid %s exists in the text index but in no source' |
169 |
print >> sys.stderr, msg % eid, |
|
170 |
if fix: |
|
171 |
session.system_sql('DELETE FROM appears WHERE uid=%s;' % eid) |
|
172 |
print >> sys.stderr, ' [FIXED]' |
|
173 |
else: |
|
174 |
print >> sys.stderr |
|
175 |
||
176 |
||
177 |
def check_entities(schema, session, eids, fix=1): |
|
178 |
"""check all entities registered in the repo system table""" |
|
179 |
print 'Checking entities system table' |
|
180 |
cursor = session.system_sql('SELECT eid FROM entities;') |
|
181 |
for row in cursor.fetchall(): |
|
182 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
183 |
if not has_eid(session, cursor, eid, eids): |
0 | 184 |
msg = ' Entity with eid %s exists in the system table but in no source' |
185 |
print >> sys.stderr, msg % eid, |
|
186 |
if fix: |
|
187 |
session.system_sql('DELETE FROM entities WHERE eid=%s;' % eid) |
|
188 |
print >> sys.stderr, ' [FIXED]' |
|
189 |
else: |
|
190 |
print >> sys.stderr |
|
191 |
print 'Checking entities tables' |
|
192 |
for eschema in schema.entities(): |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3374
diff
changeset
|
193 |
if eschema.final: |
0 | 194 |
continue |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
195 |
table = SQL_PREFIX + eschema.type |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
196 |
column = SQL_PREFIX + 'eid' |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
197 |
cursor = session.system_sql('SELECT %s FROM %s;' % (column, table)) |
0 | 198 |
for row in cursor.fetchall(): |
199 |
eid = row[0] |
|
5341
0de53140bd29
[db-check] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5340
diff
changeset
|
200 |
# eids is full since we have fetched everything from the entities table, |
0 | 201 |
# no need to call has_eid |
202 |
if not eid in eids or not eids[eid]: |
|
203 |
msg = ' Entity with eid %s exists in the %s table but not in the system table' |
|
204 |
print >> sys.stderr, msg % (eid, eschema.type), |
|
205 |
if fix: |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
206 |
session.system_sql('DELETE FROM %s WHERE %s=%s;' % (table, column, eid)) |
0 | 207 |
print >> sys.stderr, ' [FIXED]' |
208 |
else: |
|
209 |
print >> sys.stderr |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
210 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
211 |
|
0 | 212 |
def bad_related_msg(rtype, target, eid, fix): |
213 |
msg = ' A relation %s with %s eid %s exists but no such entity in sources' |
|
214 |
print >> sys.stderr, msg % (rtype, target, eid), |
|
215 |
if fix: |
|
216 |
print >> sys.stderr, ' [FIXED]' |
|
217 |
else: |
|
218 |
print >> sys.stderr |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
219 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
220 |
|
0 | 221 |
def check_relations(schema, session, eids, fix=1): |
222 |
"""check all relations registered in the repo system table""" |
|
223 |
print 'Checking relations' |
|
224 |
for rschema in schema.relations(): |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3374
diff
changeset
|
225 |
if rschema.final or rschema in PURE_VIRTUAL_RTYPES: |
0 | 226 |
continue |
227 |
if rschema.inlined: |
|
228 |
for subjtype in rschema.subjects(): |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
229 |
table = SQL_PREFIX + str(subjtype) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
230 |
column = SQL_PREFIX + str(rschema) |
380 | 231 |
sql = 'SELECT %s FROM %s WHERE %s IS NOT NULL;' % ( |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
232 |
column, table, column) |
380 | 233 |
cursor = session.system_sql(sql) |
0 | 234 |
for row in cursor.fetchall(): |
235 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
236 |
if not has_eid(session, cursor, eid, eids): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
237 |
bad_related_msg(rschema, 'object', eid, fix) |
0 | 238 |
if fix: |
3374
d5bd1b659ce8
[db-check] fix sql to fix bad eid referenced by inlined relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
239 |
sql = 'UPDATE %s SET %s=NULL WHERE %s=%s;' % ( |
d5bd1b659ce8
[db-check] fix sql to fix bad eid referenced by inlined relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
240 |
table, column, column, eid) |
381 | 241 |
session.system_sql(sql) |
0 | 242 |
continue |
6185
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
243 |
try: |
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
244 |
cursor = session.system_sql('SELECT eid_from FROM %s_relation;' % rschema) |
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
245 |
except Exception, ex: |
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
246 |
# usually because table doesn't exist |
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
247 |
print 'ERROR', ex |
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
248 |
continue |
0 | 249 |
for row in cursor.fetchall(): |
250 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
251 |
if not has_eid(session, cursor, eid, eids): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
252 |
bad_related_msg(rschema, 'subject', eid, fix) |
0 | 253 |
if fix: |
380 | 254 |
sql = 'DELETE FROM %s_relation WHERE eid_from=%s;' % ( |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
255 |
rschema, eid) |
380 | 256 |
session.system_sql(sql) |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
257 |
cursor = session.system_sql('SELECT eid_to FROM %s_relation;' % rschema) |
0 | 258 |
for row in cursor.fetchall(): |
259 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
260 |
if not has_eid(session, cursor, eid, eids): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
261 |
bad_related_msg(rschema, 'object', eid, fix) |
0 | 262 |
if fix: |
380 | 263 |
sql = 'DELETE FROM %s_relation WHERE eid_to=%s;' % ( |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
264 |
rschema, eid) |
380 | 265 |
session.system_sql(sql) |
0 | 266 |
|
267 |
||
268 |
def check_metadata(schema, session, eids, fix=1): |
|
269 |
"""check entities has required metadata |
|
270 |
||
271 |
FIXME: rewrite using RQL queries ? |
|
272 |
""" |
|
273 |
print 'Checking metadata' |
|
274 |
cursor = session.system_sql("SELECT DISTINCT type FROM entities;") |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
275 |
eidcolumn = SQL_PREFIX + 'eid' |
0 | 276 |
for etype, in cursor.fetchall(): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
277 |
table = SQL_PREFIX + etype |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
278 |
for rel, default in ( ('creation_date', datetime.now()), |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
279 |
('modification_date', datetime.now()), ): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
280 |
column = SQL_PREFIX + rel |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
281 |
cursor = session.system_sql("SELECT %s FROM %s WHERE %s is NULL" |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
282 |
% (eidcolumn, table, column)) |
0 | 283 |
for eid, in cursor.fetchall(): |
284 |
msg = ' %s with eid %s has no %s' |
|
285 |
print >> sys.stderr, msg % (etype, eid, rel), |
|
286 |
if fix: |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
287 |
session.system_sql("UPDATE %s SET %s=%%(v)s WHERE %s=%s ;" |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
288 |
% (table, column, eidcolumn, eid), |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
289 |
{'v': default}) |
0 | 290 |
print >> sys.stderr, ' [FIXED]' |
291 |
else: |
|
292 |
print >> sys.stderr |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
293 |
cursor = session.system_sql('SELECT MIN(%s) FROM %sCWUser;' % (eidcolumn, |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
294 |
SQL_PREFIX)) |
0 | 295 |
default_user_eid = cursor.fetchone()[0] |
296 |
assert default_user_eid is not None, 'no user defined !' |
|
297 |
for rel, default in ( ('owned_by', default_user_eid), ): |
|
298 |
cursor = session.system_sql("SELECT eid, type FROM entities " |
|
5340
4de474016568
[db-check] don't check entities from external sources have owned_by
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5339
diff
changeset
|
299 |
"WHERE source='system' AND NOT EXISTS " |
0 | 300 |
"(SELECT 1 FROM %s_relation WHERE eid_from=eid);" |
301 |
% rel) |
|
302 |
for eid, etype in cursor.fetchall(): |
|
303 |
msg = ' %s with eid %s has no %s relation' |
|
304 |
print >> sys.stderr, msg % (etype, eid, rel), |
|
305 |
if fix: |
|
306 |
session.system_sql('INSERT INTO %s_relation VALUES (%s, %s) ;' |
|
307 |
% (rel, eid, default)) |
|
308 |
print >> sys.stderr, ' [FIXED]' |
|
309 |
else: |
|
310 |
print >> sys.stderr |
|
311 |
||
312 |
||
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
313 |
def check(repo, cnx, checks, reindex, fix, withpb=True): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2248
diff
changeset
|
314 |
"""check integrity of instance's repository, |
0 | 315 |
using given user and password to locally connect to the repository |
316 |
(no running cubicweb server needed) |
|
317 |
""" |
|
318 |
session = repo._get_session(cnx.sessionid, setpool=True) |
|
319 |
# yo, launch checks |
|
320 |
if checks: |
|
321 |
eids_cache = {} |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
322 |
with security_enabled(session, read=False): # ensure no read security |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
323 |
for check in checks: |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
324 |
check_func = globals()['check_%s' % check] |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
325 |
check_func(repo.schema, session, eids_cache, fix=fix) |
0 | 326 |
if fix: |
327 |
cnx.commit() |
|
328 |
else: |
|
329 |
print |
|
330 |
if not fix: |
|
331 |
print 'WARNING: Diagnostic run, nothing has been corrected' |
|
332 |
if reindex: |
|
333 |
cnx.rollback() |
|
334 |
session.set_pool() |
|
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
335 |
reindex_entities(repo.schema, session, withpb=withpb) |
0 | 336 |
cnx.commit() |
6127
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
337 |
|
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
338 |
|
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
339 |
def warning(msg, *args): |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
340 |
if args: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
341 |
msg = msg % args |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
342 |
print 'WARNING: %s' % msg |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
343 |
|
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
344 |
def error(msg, *args): |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
345 |
if args: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
346 |
msg = msg % args |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
347 |
print 'ERROR: %s' % msg |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
348 |
|
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
349 |
def check_mapping(schema, mapping, warning=warning, error=error): |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
350 |
# first check stuff found in mapping file exists in the schema |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
351 |
for attr in ('support_entities', 'support_relations'): |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
352 |
for ertype in mapping[attr].keys(): |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
353 |
try: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
354 |
mapping[attr][ertype] = erschema = schema[ertype] |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
355 |
except KeyError: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
356 |
error('reference to unknown type %s in %s', ertype, attr) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
357 |
del mapping[attr][ertype] |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
358 |
else: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
359 |
if erschema.final or erschema in META_RTYPES: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
360 |
error('type %s should not be mapped in %s', ertype, attr) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
361 |
del mapping[attr][ertype] |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
362 |
for attr in ('dont_cross_relations', 'cross_relations'): |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
363 |
for rtype in list(mapping[attr]): |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
364 |
try: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
365 |
rschema = schema.rschema(rtype) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
366 |
except KeyError: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
367 |
error('reference to unknown relation type %s in %s', rtype, attr) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
368 |
mapping[attr].remove(rtype) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
369 |
else: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
370 |
if rschema.final or rschema in VIRTUAL_RTYPES: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
371 |
error('relation type %s should not be mapped in %s', |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
372 |
rtype, attr) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
373 |
mapping[attr].remove(rtype) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
374 |
# check relation in dont_cross_relations aren't in support_relations |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
375 |
for rschema in mapping['dont_cross_relations']: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
376 |
if rschema in mapping['support_relations']: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
377 |
warning('relation %s is in dont_cross_relations and in support_relations', |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
378 |
rschema) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
379 |
# check relation in cross_relations are in support_relations |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
380 |
for rschema in mapping['cross_relations']: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
381 |
if rschema not in mapping['support_relations']: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
382 |
warning('relation %s is in cross_relations but not in support_relations', |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
383 |
rschema) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
384 |
# check for relation in both cross_relations and dont_cross_relations |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
385 |
for rschema in mapping['cross_relations'] & mapping['dont_cross_relations']: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
386 |
error('relation %s is in both cross_relations and dont_cross_relations', |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
387 |
rschema) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
388 |
# now check for more handy things |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
389 |
seen = set() |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
390 |
for eschema in mapping['support_entities'].values(): |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
391 |
for rschema, ttypes, role in eschema.relation_definitions(): |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
392 |
if rschema in META_RTYPES: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
393 |
continue |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
394 |
ttypes = [ttype for ttype in ttypes if ttype in mapping['support_entities']] |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
395 |
if not rschema in mapping['support_relations']: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
396 |
somethingprinted = False |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
397 |
for ttype in ttypes: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
398 |
rdef = rschema.role_rdef(eschema, ttype, role) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
399 |
seen.add(rdef) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
400 |
if rdef.role_cardinality(role) in '1+': |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
401 |
error('relation %s with %s as %s and target type %s is ' |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
402 |
'mandatory but not supported', |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
403 |
rschema, eschema, role, ttype) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
404 |
somethingprinted = True |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
405 |
elif ttype in mapping['support_entities']: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
406 |
if rdef not in seen: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
407 |
warning('%s could be supported', rdef) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
408 |
somethingprinted = True |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
409 |
if rschema not in mapping['dont_cross_relations']: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
410 |
if role == 'subject' and rschema.inlined: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
411 |
error('inlined relation %s of %s should be supported', |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
412 |
rschema, eschema) |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
413 |
elif not somethingprinted and rschema not in seen: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
414 |
print 'you may want to specify something for %s' % rschema |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
415 |
seen.add(rschema) |
6132
440df442d705
[c-c check-mapping] fix dumb name error and add a warning about inlined crossed relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6127
diff
changeset
|
416 |
else: |
440df442d705
[c-c check-mapping] fix dumb name error and add a warning about inlined crossed relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6127
diff
changeset
|
417 |
if not ttypes: |
440df442d705
[c-c check-mapping] fix dumb name error and add a warning about inlined crossed relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6127
diff
changeset
|
418 |
warning('relation %s with %s as %s is supported but no target ' |
440df442d705
[c-c check-mapping] fix dumb name error and add a warning about inlined crossed relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6127
diff
changeset
|
419 |
'type supported', rschema, role, eschema) |
440df442d705
[c-c check-mapping] fix dumb name error and add a warning about inlined crossed relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6127
diff
changeset
|
420 |
if rschema in mapping['cross_relations'] and rschema.inlined: |
440df442d705
[c-c check-mapping] fix dumb name error and add a warning about inlined crossed relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6127
diff
changeset
|
421 |
error('you should unline relation %s which is supported and ' |
440df442d705
[c-c check-mapping] fix dumb name error and add a warning about inlined crossed relation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6127
diff
changeset
|
422 |
'may be crossed ', rschema) |
6127
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
423 |
for rschema in mapping['support_relations'].values(): |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
424 |
if rschema in META_RTYPES: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
425 |
continue |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
426 |
for subj, obj in rschema.rdefs: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
427 |
if subj in mapping['support_entities'] and obj in mapping['support_entities']: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
428 |
break |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
429 |
else: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
430 |
error('relation %s is supported but none if its definitions ' |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
431 |
'matches supported entities', rschema) |