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