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