author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 21 Apr 2010 16:53:47 +0200 | |
branch | stable |
changeset 5368 | d321e4b62a10 |
parent 5341 | 0de53140bd29 |
child 5421 | 8167de96c523 |
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 |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
22 |
def has_eid(session, sqlcursor, eid, eids): |
0 | 23 |
"""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
|
24 |
if eid in eids: |
0 | 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': |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
33 |
try: |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
34 |
# 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
|
35 |
# 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
|
36 |
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
|
37 |
{'x': eid}): |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
38 |
eids[eid] = True |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
39 |
return True |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
40 |
except: # TypeResolverError, Unauthorized... |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
41 |
pass |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
42 |
eids[eid] = False |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
43 |
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
|
44 |
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
|
45 |
SQL_PREFIX, eid)) |
0 | 46 |
result = sqlcursor.fetchall() |
47 |
if len(result) == 0: |
|
48 |
eids[eid] = False |
|
49 |
return False |
|
50 |
elif len(result) > 1: |
|
51 |
msg = ' More than one entity with eid %s exists in source !' |
|
52 |
print >> sys.stderr, msg % eid |
|
53 |
print >> sys.stderr, ' WARNING : Unable to fix this, do it yourself !' |
|
54 |
eids[eid] = True |
|
55 |
return True |
|
56 |
||
57 |
# XXX move to yams? |
|
58 |
def etype_fti_containers(eschema, _done=None): |
|
59 |
if _done is None: |
|
60 |
_done = set() |
|
61 |
_done.add(eschema) |
|
62 |
containers = tuple(eschema.fulltext_containers()) |
|
63 |
if containers: |
|
64 |
for rschema, target in containers: |
|
65 |
if target == 'object': |
|
66 |
targets = rschema.objects(eschema) |
|
67 |
else: |
|
68 |
targets = rschema.subjects(eschema) |
|
69 |
for targeteschema in targets: |
|
70 |
if targeteschema in _done: |
|
71 |
continue |
|
72 |
_done.add(targeteschema) |
|
73 |
for container in etype_fti_containers(targeteschema, _done): |
|
74 |
yield container |
|
75 |
else: |
|
76 |
yield eschema |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
77 |
|
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
78 |
def reindex_entities(schema, session, withpb=True): |
0 | 79 |
"""reindex all entities in the repository""" |
80 |
# deactivate modification_date hook since we don't want them |
|
81 |
# to be updated due to the reindexation |
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
repo.system_source.do_fti = True # ensure full-text indexation is activated |
0 | 88 |
etypes = set() |
89 |
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
|
90 |
if eschema.final: |
0 | 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)) |
|
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
99 |
if withpb: |
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
100 |
pb = ProgressBar(len(etypes) + 1) |
0 | 101 |
# first monkey patch Entity.check to disable validation |
102 |
# clear fti table first |
|
103 |
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
|
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 |
# reindex entities by generating rql queries which set all indexable |
107 |
# 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
|
108 |
source = repo.system_source |
0 | 109 |
for eschema in etypes: |
110 |
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
|
111 |
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
|
112 |
if withpb: |
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
113 |
pb.update() |
0 | 114 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
115 |
|
380 | 116 |
def check_schema(schema, session, eids, fix=1): |
0 | 117 |
"""check serialized schema""" |
118 |
print 'Checking serialized schema' |
|
119 |
unique_constraints = ('SizeConstraint', 'FormatConstraint', |
|
120 |
'VocabularyConstraint', 'RQLConstraint', |
|
121 |
'RQLVocabularyConstraint') |
|
5338
3e5a256d17ba
[db-check] fix duplicated schema constraint detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
122 |
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
|
123 |
'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
|
124 |
'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
|
125 |
'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
|
126 |
for count, rn, sn, on, cstrname in session.execute(rql): |
0 | 127 |
if count == 1: |
128 |
continue |
|
129 |
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
|
130 |
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
|
131 |
count, cstrname, sn, rn, on) |
0 | 132 |
|
133 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
134 |
|
0 | 135 |
def check_text_index(schema, session, eids, fix=1): |
136 |
"""check all entities registered in the text index""" |
|
137 |
print 'Checking text index' |
|
138 |
cursor = session.system_sql('SELECT uid FROM appears;') |
|
139 |
for row in cursor.fetchall(): |
|
140 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
141 |
if not has_eid(session, cursor, eid, eids): |
0 | 142 |
msg = ' Entity with eid %s exists in the text index but in no source' |
143 |
print >> sys.stderr, msg % eid, |
|
144 |
if fix: |
|
145 |
session.system_sql('DELETE FROM appears WHERE uid=%s;' % eid) |
|
146 |
print >> sys.stderr, ' [FIXED]' |
|
147 |
else: |
|
148 |
print >> sys.stderr |
|
149 |
||
150 |
||
151 |
def check_entities(schema, session, eids, fix=1): |
|
152 |
"""check all entities registered in the repo system table""" |
|
153 |
print 'Checking entities system table' |
|
154 |
cursor = session.system_sql('SELECT eid FROM entities;') |
|
155 |
for row in cursor.fetchall(): |
|
156 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
157 |
if not has_eid(session, cursor, eid, eids): |
0 | 158 |
msg = ' Entity with eid %s exists in the system table but in no source' |
159 |
print >> sys.stderr, msg % eid, |
|
160 |
if fix: |
|
161 |
session.system_sql('DELETE FROM entities WHERE eid=%s;' % eid) |
|
162 |
print >> sys.stderr, ' [FIXED]' |
|
163 |
else: |
|
164 |
print >> sys.stderr |
|
165 |
print 'Checking entities tables' |
|
166 |
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
|
167 |
if eschema.final: |
0 | 168 |
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
|
169 |
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
|
170 |
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
|
171 |
cursor = session.system_sql('SELECT %s FROM %s;' % (column, table)) |
0 | 172 |
for row in cursor.fetchall(): |
173 |
eid = row[0] |
|
5341
0de53140bd29
[db-check] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5340
diff
changeset
|
174 |
# eids is full since we have fetched everything from the entities table, |
0 | 175 |
# no need to call has_eid |
176 |
if not eid in eids or not eids[eid]: |
|
177 |
msg = ' Entity with eid %s exists in the %s table but not in the system table' |
|
178 |
print >> sys.stderr, msg % (eid, eschema.type), |
|
179 |
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
|
180 |
session.system_sql('DELETE FROM %s WHERE %s=%s;' % (table, column, eid)) |
0 | 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 bad_related_msg(rtype, target, eid, fix): |
187 |
msg = ' A relation %s with %s eid %s exists but no such entity in sources' |
|
188 |
print >> sys.stderr, msg % (rtype, target, eid), |
|
189 |
if fix: |
|
190 |
print >> sys.stderr, ' [FIXED]' |
|
191 |
else: |
|
192 |
print >> sys.stderr |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
193 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
194 |
|
0 | 195 |
def check_relations(schema, session, eids, fix=1): |
196 |
"""check all relations registered in the repo system table""" |
|
197 |
print 'Checking relations' |
|
198 |
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
|
199 |
if rschema.final or rschema in PURE_VIRTUAL_RTYPES: |
0 | 200 |
continue |
201 |
if rschema.inlined: |
|
202 |
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
|
203 |
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
|
204 |
column = SQL_PREFIX + str(rschema) |
380 | 205 |
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
|
206 |
column, table, column) |
380 | 207 |
cursor = session.system_sql(sql) |
0 | 208 |
for row in cursor.fetchall(): |
209 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
210 |
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
|
211 |
bad_related_msg(rschema, 'object', eid, fix) |
0 | 212 |
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
|
213 |
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
|
214 |
table, column, column, eid) |
381 | 215 |
session.system_sql(sql) |
0 | 216 |
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
|
217 |
cursor = session.system_sql('SELECT eid_from FROM %s_relation;' % rschema) |
0 | 218 |
for row in cursor.fetchall(): |
219 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
220 |
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
|
221 |
bad_related_msg(rschema, 'subject', eid, fix) |
0 | 222 |
if fix: |
380 | 223 |
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
|
224 |
rschema, eid) |
380 | 225 |
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
|
226 |
cursor = session.system_sql('SELECT eid_to FROM %s_relation;' % rschema) |
0 | 227 |
for row in cursor.fetchall(): |
228 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
229 |
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
|
230 |
bad_related_msg(rschema, 'object', eid, fix) |
0 | 231 |
if fix: |
380 | 232 |
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
|
233 |
rschema, eid) |
380 | 234 |
session.system_sql(sql) |
0 | 235 |
|
236 |
||
237 |
def check_metadata(schema, session, eids, fix=1): |
|
238 |
"""check entities has required metadata |
|
239 |
||
240 |
FIXME: rewrite using RQL queries ? |
|
241 |
""" |
|
242 |
print 'Checking metadata' |
|
243 |
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
|
244 |
eidcolumn = SQL_PREFIX + 'eid' |
0 | 245 |
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
|
246 |
table = SQL_PREFIX + etype |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
247 |
for rel, default in ( ('creation_date', datetime.now()), |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
248 |
('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
|
249 |
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
|
250 |
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
|
251 |
% (eidcolumn, table, column)) |
0 | 252 |
for eid, in cursor.fetchall(): |
253 |
msg = ' %s with eid %s has no %s' |
|
254 |
print >> sys.stderr, msg % (etype, eid, rel), |
|
255 |
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
|
256 |
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
|
257 |
% (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
|
258 |
{'v': default}) |
0 | 259 |
print >> sys.stderr, ' [FIXED]' |
260 |
else: |
|
261 |
print >> sys.stderr |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
262 |
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
|
263 |
SQL_PREFIX)) |
0 | 264 |
default_user_eid = cursor.fetchone()[0] |
265 |
assert default_user_eid is not None, 'no user defined !' |
|
266 |
for rel, default in ( ('owned_by', default_user_eid), ): |
|
267 |
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
|
268 |
"WHERE source='system' AND NOT EXISTS " |
0 | 269 |
"(SELECT 1 FROM %s_relation WHERE eid_from=eid);" |
270 |
% rel) |
|
271 |
for eid, etype in cursor.fetchall(): |
|
272 |
msg = ' %s with eid %s has no %s relation' |
|
273 |
print >> sys.stderr, msg % (etype, eid, rel), |
|
274 |
if fix: |
|
275 |
session.system_sql('INSERT INTO %s_relation VALUES (%s, %s) ;' |
|
276 |
% (rel, eid, default)) |
|
277 |
print >> sys.stderr, ' [FIXED]' |
|
278 |
else: |
|
279 |
print >> sys.stderr |
|
280 |
||
281 |
||
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
282 |
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
|
283 |
"""check integrity of instance's repository, |
0 | 284 |
using given user and password to locally connect to the repository |
285 |
(no running cubicweb server needed) |
|
286 |
""" |
|
287 |
session = repo._get_session(cnx.sessionid, setpool=True) |
|
288 |
# yo, launch checks |
|
289 |
if checks: |
|
290 |
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
|
291 |
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
|
292 |
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
|
293 |
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
|
294 |
check_func(repo.schema, session, eids_cache, fix=fix) |
0 | 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() |
|
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
304 |
reindex_entities(repo.schema, session, withpb=withpb) |
0 | 305 |
cnx.commit() |