author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> |
Fri, 09 Oct 2009 15:53:02 +0200 | |
branch | stable |
changeset 3626 | 017869a514c3 |
parent 3374 | d5bd1b659ce8 |
child 3417 | fb17a54b358c |
child 3689 | deb13e88e037 |
permissions | -rw-r--r-- |
0 | 1 |
"""Check integrity of a CubicWeb repository. Hum actually only the system database |
2 |
is checked. |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
11 |
import sys |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
12 |
from datetime import datetime |
0 | 13 |
|
14 |
from logilab.common.shellutils import ProgressBar |
|
15 |
||
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
16 |
from cubicweb.schema import 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
|
17 |
from cubicweb.server.sqlutils import SQL_PREFIX |
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
|
18 |
|
0 | 19 |
def has_eid(sqlcursor, eid, eids): |
20 |
"""return true if the eid is a valid eid""" |
|
21 |
if eids.has_key(eid): |
|
22 |
return eids[eid] |
|
23 |
sqlcursor.execute('SELECT type, source FROM entities WHERE eid=%s' % eid) |
|
24 |
try: |
|
25 |
etype, source = sqlcursor.fetchone() |
|
26 |
except: |
|
27 |
eids[eid] = False |
|
28 |
return False |
|
29 |
if source and source != 'system': |
|
30 |
# XXX what to do... |
|
31 |
eids[eid] = True |
|
32 |
return True |
|
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
|
33 |
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
|
34 |
SQL_PREFIX, eid)) |
0 | 35 |
result = sqlcursor.fetchall() |
36 |
if len(result) == 0: |
|
37 |
eids[eid] = False |
|
38 |
return False |
|
39 |
elif len(result) > 1: |
|
40 |
msg = ' More than one entity with eid %s exists in source !' |
|
41 |
print >> sys.stderr, msg % eid |
|
42 |
print >> sys.stderr, ' WARNING : Unable to fix this, do it yourself !' |
|
43 |
eids[eid] = True |
|
44 |
return True |
|
45 |
||
46 |
# XXX move to yams? |
|
47 |
def etype_fti_containers(eschema, _done=None): |
|
48 |
if _done is None: |
|
49 |
_done = set() |
|
50 |
_done.add(eschema) |
|
51 |
containers = tuple(eschema.fulltext_containers()) |
|
52 |
if containers: |
|
53 |
for rschema, target in containers: |
|
54 |
if target == 'object': |
|
55 |
targets = rschema.objects(eschema) |
|
56 |
else: |
|
57 |
targets = rschema.subjects(eschema) |
|
58 |
for targeteschema in targets: |
|
59 |
if targeteschema in _done: |
|
60 |
continue |
|
61 |
_done.add(targeteschema) |
|
62 |
for container in etype_fti_containers(targeteschema, _done): |
|
63 |
yield container |
|
64 |
else: |
|
65 |
yield eschema |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
66 |
|
0 | 67 |
def reindex_entities(schema, session): |
68 |
"""reindex all entities in the repository""" |
|
69 |
# deactivate modification_date hook since we don't want them |
|
70 |
# to be updated due to the reindexation |
|
71 |
from cubicweb.server.hooks import (setmtime_before_update_entity, |
|
72 |
uniquecstrcheck_before_modification) |
|
73 |
from cubicweb.server.repository import FTIndexEntityOp |
|
74 |
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
|
75 |
cursor = session.pool['system'] |
cbf043a2134a
try to create fti table if not existant on rebuild-fti
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
76 |
if not repo.system_source.indexer.has_fti_table(cursor): |
cbf043a2134a
try to create fti table if not existant on rebuild-fti
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
77 |
from indexer import get_indexer |
cbf043a2134a
try to create fti table if not existant on rebuild-fti
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
78 |
print 'no text index table' |
cbf043a2134a
try to create fti table if not existant on rebuild-fti
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
79 |
indexer = get_indexer(repo.system_source.dbdriver) |
cbf043a2134a
try to create fti table if not existant on rebuild-fti
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
80 |
# XXX indexer.init_fti(cursor) once index 0.7 is out |
cbf043a2134a
try to create fti table if not existant on rebuild-fti
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
81 |
indexer.init_extensions(cursor) |
cbf043a2134a
try to create fti table if not existant on rebuild-fti
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
82 |
cursor.execute(indexer.sql_init_fti()) |
0 | 83 |
repo.hm.unregister_hook(setmtime_before_update_entity, |
84 |
'before_update_entity', '') |
|
85 |
repo.hm.unregister_hook(uniquecstrcheck_before_modification, |
|
86 |
'before_update_entity', '') |
|
1161
936c311010fc
ensure do_fti is true in reindex_entities
sylvain.thenault@logilab.fr
parents:
381
diff
changeset
|
87 |
repo.do_fti = True # ensure full-text indexation is activated |
0 | 88 |
etypes = set() |
89 |
for eschema in schema.entities(): |
|
90 |
if eschema.is_final(): |
|
91 |
continue |
|
92 |
indexable_attrs = tuple(eschema.indexable_attributes()) # generator |
|
93 |
if not indexable_attrs: |
|
94 |
continue |
|
95 |
for container in etype_fti_containers(eschema): |
|
96 |
etypes.add(container) |
|
97 |
print 'Reindexing entities of type %s' % \ |
|
98 |
', '.join(sorted(str(e) for e in etypes)) |
|
99 |
pb = ProgressBar(len(etypes) + 1) |
|
100 |
# first monkey patch Entity.check to disable validation |
|
713
5adb6d8e5fa7
update imports of "cubicweb.common.entity" and use the new module path "cubicweb.entity"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
381
diff
changeset
|
101 |
from cubicweb.entity import Entity |
0 | 102 |
_check = Entity.check |
103 |
Entity.check = lambda self, creation=False: True |
|
104 |
# clear fti table first |
|
105 |
session.system_sql('DELETE FROM %s' % session.repo.system_source.dbhelper.fti_table) |
|
106 |
pb.update() |
|
107 |
# reindex entities by generating rql queries which set all indexable |
|
108 |
# attribute to their current value |
|
109 |
for eschema in etypes: |
|
110 |
for entity in session.execute('Any X WHERE X is %s' % eschema).entities(): |
|
111 |
FTIndexEntityOp(session, entity=entity) |
|
112 |
pb.update() |
|
113 |
# restore Entity.check |
|
114 |
Entity.check = _check |
|
115 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
116 |
|
380 | 117 |
def check_schema(schema, session, eids, fix=1): |
0 | 118 |
"""check serialized schema""" |
119 |
print 'Checking serialized schema' |
|
120 |
unique_constraints = ('SizeConstraint', 'FormatConstraint', |
|
121 |
'VocabularyConstraint', 'RQLConstraint', |
|
122 |
'RQLVocabularyConstraint') |
|
123 |
rql = ('Any COUNT(X),RN,EN,ECTN GROUPBY RN,EN,ECTN ORDERBY 1 ' |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
124 |
'WHERE X is CWConstraint, R constrained_by X, ' |
0 | 125 |
'R relation_type RT, R from_entity ET, RT name RN, ' |
126 |
'ET name EN, X cstrtype ECT, ECT name ECTN') |
|
127 |
for count, rn, en, cstrname in session.execute(rql): |
|
128 |
if count == 1: |
|
129 |
continue |
|
130 |
if cstrname in unique_constraints: |
|
131 |
print "ERROR: got %s %r constraints on relation %s.%s" % ( |
|
132 |
count, cstrname, en, rn) |
|
133 |
||
134 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
135 |
|
0 | 136 |
def check_text_index(schema, session, eids, fix=1): |
137 |
"""check all entities registered in the text index""" |
|
138 |
print 'Checking text index' |
|
139 |
cursor = session.system_sql('SELECT uid FROM appears;') |
|
140 |
for row in cursor.fetchall(): |
|
141 |
eid = row[0] |
|
142 |
if not has_eid(cursor, eid, eids): |
|
143 |
msg = ' Entity with eid %s exists in the text index but in no source' |
|
144 |
print >> sys.stderr, msg % eid, |
|
145 |
if fix: |
|
146 |
session.system_sql('DELETE FROM appears WHERE uid=%s;' % eid) |
|
147 |
print >> sys.stderr, ' [FIXED]' |
|
148 |
else: |
|
149 |
print >> sys.stderr |
|
150 |
||
151 |
||
152 |
def check_entities(schema, session, eids, fix=1): |
|
153 |
"""check all entities registered in the repo system table""" |
|
154 |
print 'Checking entities system table' |
|
155 |
cursor = session.system_sql('SELECT eid FROM entities;') |
|
156 |
for row in cursor.fetchall(): |
|
157 |
eid = row[0] |
|
158 |
if not has_eid(cursor, eid, eids): |
|
159 |
msg = ' Entity with eid %s exists in the system table but in no source' |
|
160 |
print >> sys.stderr, msg % eid, |
|
161 |
if fix: |
|
162 |
session.system_sql('DELETE FROM entities WHERE eid=%s;' % eid) |
|
163 |
print >> sys.stderr, ' [FIXED]' |
|
164 |
else: |
|
165 |
print >> sys.stderr |
|
166 |
print 'Checking entities tables' |
|
167 |
for eschema in schema.entities(): |
|
168 |
if eschema.is_final(): |
|
169 |
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
|
170 |
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
|
171 |
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
|
172 |
cursor = session.system_sql('SELECT %s FROM %s;' % (column, table)) |
0 | 173 |
for row in cursor.fetchall(): |
174 |
eid = row[0] |
|
175 |
# eids is full since we have fetched everyting from the entities table, |
|
176 |
# no need to call has_eid |
|
177 |
if not eid in eids or not eids[eid]: |
|
178 |
msg = ' Entity with eid %s exists in the %s table but not in the system table' |
|
179 |
print >> sys.stderr, msg % (eid, eschema.type), |
|
180 |
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
|
181 |
session.system_sql('DELETE FROM %s WHERE %s=%s;' % (table, column, eid)) |
0 | 182 |
print >> sys.stderr, ' [FIXED]' |
183 |
else: |
|
184 |
print >> sys.stderr |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
185 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
186 |
|
0 | 187 |
def bad_related_msg(rtype, target, eid, fix): |
188 |
msg = ' A relation %s with %s eid %s exists but no such entity in sources' |
|
189 |
print >> sys.stderr, msg % (rtype, target, eid), |
|
190 |
if fix: |
|
191 |
print >> sys.stderr, ' [FIXED]' |
|
192 |
else: |
|
193 |
print >> sys.stderr |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
194 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
195 |
|
0 | 196 |
def check_relations(schema, session, eids, fix=1): |
197 |
"""check all relations registered in the repo system table""" |
|
198 |
print 'Checking relations' |
|
199 |
for rschema in schema.relations(): |
|
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
200 |
if rschema.is_final() or rschema in PURE_VIRTUAL_RTYPES: |
0 | 201 |
continue |
202 |
if rschema.inlined: |
|
203 |
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
|
204 |
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
|
205 |
column = SQL_PREFIX + str(rschema) |
380 | 206 |
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
|
207 |
column, table, column) |
380 | 208 |
cursor = session.system_sql(sql) |
0 | 209 |
for row in cursor.fetchall(): |
210 |
eid = row[0] |
|
211 |
if not has_eid(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
|
212 |
bad_related_msg(rschema, 'object', eid, fix) |
0 | 213 |
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
|
214 |
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
|
215 |
table, column, column, eid) |
381 | 216 |
session.system_sql(sql) |
0 | 217 |
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
|
218 |
cursor = session.system_sql('SELECT eid_from FROM %s_relation;' % rschema) |
0 | 219 |
for row in cursor.fetchall(): |
220 |
eid = row[0] |
|
221 |
if not has_eid(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
|
222 |
bad_related_msg(rschema, 'subject', eid, fix) |
0 | 223 |
if fix: |
380 | 224 |
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
|
225 |
rschema, eid) |
380 | 226 |
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
|
227 |
cursor = session.system_sql('SELECT eid_to FROM %s_relation;' % rschema) |
0 | 228 |
for row in cursor.fetchall(): |
229 |
eid = row[0] |
|
230 |
if not has_eid(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
|
231 |
bad_related_msg(rschema, 'object', eid, fix) |
0 | 232 |
if fix: |
380 | 233 |
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
|
234 |
rschema, eid) |
380 | 235 |
session.system_sql(sql) |
0 | 236 |
|
237 |
||
238 |
def check_metadata(schema, session, eids, fix=1): |
|
239 |
"""check entities has required metadata |
|
240 |
||
241 |
FIXME: rewrite using RQL queries ? |
|
242 |
""" |
|
243 |
print 'Checking metadata' |
|
244 |
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
|
245 |
eidcolumn = SQL_PREFIX + 'eid' |
0 | 246 |
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
|
247 |
table = SQL_PREFIX + etype |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
248 |
for rel, default in ( ('creation_date', datetime.now()), |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
249 |
('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
|
250 |
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
|
251 |
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
|
252 |
% (eidcolumn, table, column)) |
0 | 253 |
for eid, in cursor.fetchall(): |
254 |
msg = ' %s with eid %s has no %s' |
|
255 |
print >> sys.stderr, msg % (etype, eid, rel), |
|
256 |
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
|
257 |
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
|
258 |
% (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
|
259 |
{'v': default}) |
0 | 260 |
print >> sys.stderr, ' [FIXED]' |
261 |
else: |
|
262 |
print >> sys.stderr |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
263 |
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
|
264 |
SQL_PREFIX)) |
0 | 265 |
default_user_eid = cursor.fetchone()[0] |
266 |
assert default_user_eid is not None, 'no user defined !' |
|
267 |
for rel, default in ( ('owned_by', default_user_eid), ): |
|
268 |
cursor = session.system_sql("SELECT eid, type FROM entities " |
|
269 |
"WHERE NOT EXISTS " |
|
270 |
"(SELECT 1 FROM %s_relation WHERE eid_from=eid);" |
|
271 |
% rel) |
|
272 |
for eid, etype in cursor.fetchall(): |
|
273 |
msg = ' %s with eid %s has no %s relation' |
|
274 |
print >> sys.stderr, msg % (etype, eid, rel), |
|
275 |
if fix: |
|
276 |
session.system_sql('INSERT INTO %s_relation VALUES (%s, %s) ;' |
|
277 |
% (rel, eid, default)) |
|
278 |
print >> sys.stderr, ' [FIXED]' |
|
279 |
else: |
|
280 |
print >> sys.stderr |
|
281 |
||
282 |
||
283 |
def check(repo, cnx, checks, reindex, fix): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2248
diff
changeset
|
284 |
"""check integrity of instance's repository, |
0 | 285 |
using given user and password to locally connect to the repository |
286 |
(no running cubicweb server needed) |
|
287 |
""" |
|
288 |
session = repo._get_session(cnx.sessionid, setpool=True) |
|
289 |
# yo, launch checks |
|
290 |
if checks: |
|
291 |
eids_cache = {} |
|
292 |
for check in checks: |
|
293 |
check_func = globals()['check_%s' % check] |
|
294 |
check_func(repo.schema, session, eids_cache, fix=fix) |
|
295 |
if fix: |
|
296 |
cnx.commit() |
|
297 |
else: |
|
298 |
print |
|
299 |
if not fix: |
|
300 |
print 'WARNING: Diagnostic run, nothing has been corrected' |
|
301 |
if reindex: |
|
302 |
cnx.rollback() |
|
303 |
session.set_pool() |
|
304 |
reindex_entities(repo.schema, session) |
|
305 |
cnx.commit() |