author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Sat, 18 Jun 2011 23:27:23 +0200 | |
branch | stable |
changeset 7537 | 1af162bd78b8 |
parent 7468 | c202aaf71489 |
child 7473 | a164fdf3de5d |
permissions | -rw-r--r-- |
7468
c202aaf71489
[server] fix NameError (missing VIRTUAL_RTYPES import) in checkintegrity.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7040
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5341
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
6127
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
18 |
"""Integrity checking tool for instances: |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
19 |
|
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
20 |
* integrity of a CubicWeb repository. Hum actually only the system database is |
747e423093fc
[ms, c-c] new command checking for consistency / potentian flaws and enhancements of mapping file of a multi-sources instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6112
diff
changeset
|
21 |
checked. |
5999
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5954
diff
changeset
|
22 |
""" |
0 | 23 |
|
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
|
24 |
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
|
25 |
|
0 | 26 |
__docformat__ = "restructuredtext en" |
27 |
||
28 |
import sys |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
29 |
from datetime import datetime |
0 | 30 |
|
31 |
from logilab.common.shellutils import ProgressBar |
|
32 |
||
7468
c202aaf71489
[server] fix NameError (missing VIRTUAL_RTYPES import) in checkintegrity.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7040
diff
changeset
|
33 |
from cubicweb.schema import PURE_VIRTUAL_RTYPES, 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
|
34 |
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
|
35 |
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
|
36 |
|
7035
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
37 |
def notify_fixed(fix): |
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
38 |
if fix: |
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
39 |
print >> sys.stderr, ' [FIXED]' |
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
40 |
else: |
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
41 |
print >> sys.stderr |
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
42 |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
43 |
def has_eid(session, sqlcursor, eid, eids): |
0 | 44 |
"""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
|
45 |
if eid in eids: |
0 | 46 |
return eids[eid] |
47 |
sqlcursor.execute('SELECT type, source FROM entities WHERE eid=%s' % eid) |
|
48 |
try: |
|
49 |
etype, source = sqlcursor.fetchone() |
|
50 |
except: |
|
51 |
eids[eid] = False |
|
52 |
return False |
|
53 |
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
|
54 |
try: |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
55 |
# 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
|
56 |
# 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
|
57 |
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
|
58 |
{'x': eid}): |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
59 |
eids[eid] = True |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
60 |
return True |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
61 |
except: # TypeResolverError, Unauthorized... |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
62 |
pass |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
63 |
eids[eid] = False |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
SQL_PREFIX, eid)) |
0 | 67 |
result = sqlcursor.fetchall() |
68 |
if len(result) == 0: |
|
69 |
eids[eid] = False |
|
70 |
return False |
|
71 |
elif len(result) > 1: |
|
72 |
msg = ' More than one entity with eid %s exists in source !' |
|
73 |
print >> sys.stderr, msg % eid |
|
74 |
print >> sys.stderr, ' WARNING : Unable to fix this, do it yourself !' |
|
75 |
eids[eid] = True |
|
76 |
return True |
|
77 |
||
78 |
# XXX move to yams? |
|
79 |
def etype_fti_containers(eschema, _done=None): |
|
80 |
if _done is None: |
|
81 |
_done = set() |
|
82 |
_done.add(eschema) |
|
83 |
containers = tuple(eschema.fulltext_containers()) |
|
84 |
if containers: |
|
85 |
for rschema, target in containers: |
|
86 |
if target == 'object': |
|
87 |
targets = rschema.objects(eschema) |
|
88 |
else: |
|
89 |
targets = rschema.subjects(eschema) |
|
90 |
for targeteschema in targets: |
|
91 |
if targeteschema in _done: |
|
92 |
continue |
|
93 |
_done.add(targeteschema) |
|
94 |
for container in etype_fti_containers(targeteschema, _done): |
|
95 |
yield container |
|
96 |
else: |
|
97 |
yield eschema |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
98 |
|
5850
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
99 |
def reindex_entities(schema, session, withpb=True, etypes=None): |
0 | 100 |
"""reindex all entities in the repository""" |
101 |
# deactivate modification_date hook since we don't want them |
|
102 |
# to be updated due to the reindexation |
|
103 |
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
|
104 |
cursor = session.pool['system'] |
5954
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
105 |
dbhelper = session.repo.system_source.dbhelper |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
106 |
if not dbhelper.has_fti_table(cursor): |
2248
cbf043a2134a
try to create fti table if not existant on rebuild-fti
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
107 |
print 'no text index table' |
5954
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
108 |
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
|
109 |
repo.system_source.do_fti = True # ensure full-text indexation is activated |
5850
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
110 |
if etypes is None: |
5954
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
111 |
print 'Reindexing entities' |
5850
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
112 |
etypes = set() |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
113 |
for eschema in schema.entities(): |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
114 |
if eschema.final: |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
115 |
continue |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
116 |
indexable_attrs = tuple(eschema.indexable_attributes()) # generator |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
117 |
if not indexable_attrs: |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
118 |
continue |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
119 |
for container in etype_fti_containers(eschema): |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
120 |
etypes.add(container) |
5954
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
121 |
# clear fti table first |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
122 |
session.system_sql('DELETE FROM %s' % dbhelper.fti_table) |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
123 |
else: |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
124 |
print 'Reindexing entities of type %s' % \ |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
125 |
', '.join(sorted(str(e) for e in etypes)) |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
126 |
# clear fti table first. Use subquery for sql compatibility |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
127 |
session.system_sql("DELETE FROM %s WHERE EXISTS(SELECT 1 FROM ENTITIES " |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
128 |
"WHERE eid=%s AND type IN (%s))" % ( |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
129 |
dbhelper.fti_table, dbhelper.fti_uid_attr, |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
130 |
','.join("'%s'" % etype for etype in etypes))) |
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
131 |
if withpb: |
6112
913979c79244
[db-fti-index] simple fix fpr progressbar-related crash when etypes is None in reindex_entities()
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5999
diff
changeset
|
132 |
pb = ProgressBar(len(etypes) + 1) |
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
133 |
pb.update() |
0 | 134 |
# reindex entities by generating rql queries which set all indexable |
135 |
# 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
|
136 |
source = repo.system_source |
0 | 137 |
for eschema in etypes: |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6624
diff
changeset
|
138 |
rset = session.execute('Any X WHERE X is %s' % eschema) |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6624
diff
changeset
|
139 |
source.fti_index_entities(session, rset.entities()) |
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
140 |
if withpb: |
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
141 |
pb.update() |
0 | 142 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
143 |
|
380 | 144 |
def check_schema(schema, session, eids, fix=1): |
0 | 145 |
"""check serialized schema""" |
146 |
print 'Checking serialized schema' |
|
147 |
unique_constraints = ('SizeConstraint', 'FormatConstraint', |
|
5523
4bf975c049a6
[db-check] RQLConstraint is not a 'unique' constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
148 |
'VocabularyConstraint', |
0 | 149 |
'RQLVocabularyConstraint') |
5338
3e5a256d17ba
[db-check] fix duplicated schema constraint detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
150 |
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
|
151 |
'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
|
152 |
'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
|
153 |
'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
|
154 |
for count, rn, sn, on, cstrname in session.execute(rql): |
0 | 155 |
if count == 1: |
156 |
continue |
|
157 |
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
|
158 |
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
|
159 |
count, cstrname, sn, rn, on) |
5523
4bf975c049a6
[db-check] RQLConstraint is not a 'unique' constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
160 |
if fix: |
4bf975c049a6
[db-check] RQLConstraint is not a 'unique' constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
161 |
print 'dunno how to fix, do it yourself' |
0 | 162 |
|
163 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
164 |
|
0 | 165 |
def check_text_index(schema, session, eids, fix=1): |
166 |
"""check all entities registered in the text index""" |
|
167 |
print 'Checking text index' |
|
168 |
cursor = session.system_sql('SELECT uid FROM appears;') |
|
169 |
for row in cursor.fetchall(): |
|
170 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
171 |
if not has_eid(session, cursor, eid, eids): |
0 | 172 |
msg = ' Entity with eid %s exists in the text index but in no source' |
173 |
print >> sys.stderr, msg % eid, |
|
174 |
if fix: |
|
175 |
session.system_sql('DELETE FROM appears WHERE uid=%s;' % eid) |
|
7035
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
176 |
notify_fixed(fix) |
0 | 177 |
|
178 |
||
179 |
def check_entities(schema, session, eids, fix=1): |
|
180 |
"""check all entities registered in the repo system table""" |
|
181 |
print 'Checking entities system table' |
|
182 |
cursor = session.system_sql('SELECT eid FROM entities;') |
|
183 |
for row in cursor.fetchall(): |
|
184 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
185 |
if not has_eid(session, cursor, eid, eids): |
0 | 186 |
msg = ' Entity with eid %s exists in the system table but in no source' |
187 |
print >> sys.stderr, msg % eid, |
|
188 |
if fix: |
|
189 |
session.system_sql('DELETE FROM entities WHERE eid=%s;' % eid) |
|
7035
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
190 |
notify_fixed(fix) |
0 | 191 |
print 'Checking entities tables' |
192 |
for eschema in schema.entities(): |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3374
diff
changeset
|
193 |
if eschema.final: |
0 | 194 |
continue |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
195 |
table = SQL_PREFIX + eschema.type |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
196 |
column = SQL_PREFIX + 'eid' |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
197 |
cursor = session.system_sql('SELECT %s FROM %s;' % (column, table)) |
0 | 198 |
for row in cursor.fetchall(): |
199 |
eid = row[0] |
|
5341
0de53140bd29
[db-check] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5340
diff
changeset
|
200 |
# eids is full since we have fetched everything from the entities table, |
0 | 201 |
# no need to call has_eid |
202 |
if not eid in eids or not eids[eid]: |
|
203 |
msg = ' Entity with eid %s exists in the %s table but not in the system table' |
|
204 |
print >> sys.stderr, msg % (eid, eschema.type), |
|
205 |
if fix: |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1161
diff
changeset
|
206 |
session.system_sql('DELETE FROM %s WHERE %s=%s;' % (table, column, eid)) |
7035
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
207 |
notify_fixed(fix) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
208 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
209 |
|
0 | 210 |
def bad_related_msg(rtype, target, eid, fix): |
211 |
msg = ' A relation %s with %s eid %s exists but no such entity in sources' |
|
212 |
print >> sys.stderr, msg % (rtype, target, eid), |
|
7035
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
213 |
notify_fixed(fix) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
214 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
215 |
|
0 | 216 |
def check_relations(schema, session, eids, fix=1): |
7035
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
217 |
"""check that eids referenced by relations are registered in the repo system |
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
218 |
table |
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
219 |
""" |
0 | 220 |
print 'Checking relations' |
221 |
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
|
222 |
if rschema.final or rschema in PURE_VIRTUAL_RTYPES: |
0 | 223 |
continue |
224 |
if rschema.inlined: |
|
225 |
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
|
226 |
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
|
227 |
column = SQL_PREFIX + str(rschema) |
380 | 228 |
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
|
229 |
column, table, column) |
380 | 230 |
cursor = session.system_sql(sql) |
0 | 231 |
for row in cursor.fetchall(): |
232 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
233 |
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
|
234 |
bad_related_msg(rschema, 'object', eid, fix) |
0 | 235 |
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
|
236 |
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
|
237 |
table, column, column, eid) |
381 | 238 |
session.system_sql(sql) |
0 | 239 |
continue |
6185
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
240 |
try: |
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
241 |
cursor = session.system_sql('SELECT eid_from FROM %s_relation;' % rschema) |
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
242 |
except Exception, ex: |
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
243 |
# usually because table doesn't exist |
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
244 |
print 'ERROR', ex |
229006accd26
[c-c db-check] skip error while checking relation, useful when analyzing really broken database (after a migration failure for instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6132
diff
changeset
|
245 |
continue |
0 | 246 |
for row in cursor.fetchall(): |
247 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
248 |
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
|
249 |
bad_related_msg(rschema, 'subject', eid, fix) |
0 | 250 |
if fix: |
380 | 251 |
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
|
252 |
rschema, eid) |
380 | 253 |
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
|
254 |
cursor = session.system_sql('SELECT eid_to FROM %s_relation;' % rschema) |
0 | 255 |
for row in cursor.fetchall(): |
256 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
257 |
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
|
258 |
bad_related_msg(rschema, 'object', eid, fix) |
0 | 259 |
if fix: |
380 | 260 |
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
|
261 |
rschema, eid) |
380 | 262 |
session.system_sql(sql) |
0 | 263 |
|
264 |
||
7036
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
265 |
def check_mandatory_relations(schema, session, eids, fix=1): |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
266 |
"""check entities missing some mandatory relation""" |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
267 |
print 'Checking mandatory relations' |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
268 |
for rschema in schema.relations(): |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
269 |
if rschema.final or rschema in PURE_VIRTUAL_RTYPES: |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
270 |
continue |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
271 |
smandatory = set() |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
272 |
omandatory = set() |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
273 |
for rdef in rschema.rdefs.values(): |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
274 |
if rdef.cardinality[0] in '1+': |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
275 |
smandatory.add(rdef.subject) |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
276 |
if rdef.cardinality[1] in '1+': |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
277 |
omandatory.add(rdef.object) |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
278 |
for role, etypes in (('subject', smandatory), ('object', omandatory)): |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
279 |
for etype in etypes: |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
280 |
if role == 'subject': |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
281 |
rql = 'Any X WHERE NOT X %s Y, X is %s' % (rschema, etype) |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
282 |
else: |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
283 |
rql = 'Any X WHERE NOT Y %s X, X is %s' % (rschema, etype) |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
284 |
for entity in session.execute(rql).entities(): |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
285 |
print >> sys.stderr, '%s #%s is missing mandatory %s relation %s' % ( |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
286 |
entity.__regid__, entity.eid, role, rschema) |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
287 |
if fix: |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
288 |
#if entity.cw_describe()['source']['uri'] == 'system': XXX |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
289 |
entity.delete() |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
290 |
notify_fixed(fix) |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
291 |
|
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
292 |
|
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
293 |
def check_mandatory_attributes(schema, session, eids, fix=1): |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
294 |
"""check for entities stored in the system source missing some mandatory |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
295 |
attribute |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
296 |
""" |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
297 |
print 'Checking mandatory attributes' |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
298 |
for rschema in schema.relations(): |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
299 |
if not rschema.final or rschema in VIRTUAL_RTYPES: |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
300 |
continue |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
301 |
for rdef in rschema.rdefs.values(): |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
302 |
if rdef.cardinality[0] in '1+': |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
303 |
rql = 'Any X WHERE X %s NULL, X is %s, X cw_source S, S name "system"' % ( |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
304 |
rschema, rdef.subject) |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
305 |
for entity in session.execute(rql).entities(): |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
306 |
print >> sys.stderr, '%s #%s is missing mandatory attribute %s' % ( |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
307 |
entity.__regid__, entity.eid, rschema) |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
308 |
if fix: |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
309 |
entity.delete() |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
310 |
notify_fixed(fix) |
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
311 |
|
63386b35ec69
[c-c db-check] new checks for entities missing a mandatory relation/attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7035
diff
changeset
|
312 |
|
0 | 313 |
def check_metadata(schema, session, eids, fix=1): |
314 |
"""check entities has required metadata |
|
315 |
||
316 |
FIXME: rewrite using RQL queries ? |
|
317 |
""" |
|
318 |
print 'Checking metadata' |
|
319 |
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
|
320 |
eidcolumn = SQL_PREFIX + 'eid' |
0 | 321 |
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
|
322 |
table = SQL_PREFIX + etype |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
323 |
for rel, default in ( ('creation_date', datetime.now()), |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
324 |
('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
|
325 |
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
|
326 |
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
|
327 |
% (eidcolumn, table, column)) |
0 | 328 |
for eid, in cursor.fetchall(): |
329 |
msg = ' %s with eid %s has no %s' |
|
330 |
print >> sys.stderr, msg % (etype, eid, rel), |
|
331 |
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
|
332 |
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
|
333 |
% (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
|
334 |
{'v': default}) |
7035
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
335 |
notify_fixed(fix) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
336 |
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
|
337 |
SQL_PREFIX)) |
0 | 338 |
default_user_eid = cursor.fetchone()[0] |
339 |
assert default_user_eid is not None, 'no user defined !' |
|
340 |
for rel, default in ( ('owned_by', default_user_eid), ): |
|
341 |
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
|
342 |
"WHERE source='system' AND NOT EXISTS " |
0 | 343 |
"(SELECT 1 FROM %s_relation WHERE eid_from=eid);" |
344 |
% rel) |
|
345 |
for eid, etype in cursor.fetchall(): |
|
346 |
msg = ' %s with eid %s has no %s relation' |
|
347 |
print >> sys.stderr, msg % (etype, eid, rel), |
|
348 |
if fix: |
|
349 |
session.system_sql('INSERT INTO %s_relation VALUES (%s, %s) ;' |
|
350 |
% (rel, eid, default)) |
|
7035
8d2cf36bd79d
[c-c db-check] factorize code by introducing notify_fixed dumb function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
351 |
notify_fixed(fix) |
0 | 352 |
|
353 |
||
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
354 |
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
|
355 |
"""check integrity of instance's repository, |
0 | 356 |
using given user and password to locally connect to the repository |
357 |
(no running cubicweb server needed) |
|
358 |
""" |
|
359 |
session = repo._get_session(cnx.sessionid, setpool=True) |
|
360 |
# yo, launch checks |
|
361 |
if checks: |
|
362 |
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
|
363 |
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
|
364 |
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
|
365 |
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
|
366 |
check_func(repo.schema, session, eids_cache, fix=fix) |
0 | 367 |
if fix: |
368 |
cnx.commit() |
|
369 |
else: |
|
370 |
print |
|
371 |
if not fix: |
|
372 |
print 'WARNING: Diagnostic run, nothing has been corrected' |
|
373 |
if reindex: |
|
374 |
cnx.rollback() |
|
375 |
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
|
376 |
reindex_entities(repo.schema, session, withpb=withpb) |
0 | 377 |
cnx.commit() |