author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 21 Oct 2011 14:32:37 +0200 | |
changeset 7997 | e6eb86233e6f |
parent 7965 | d45c0eb39e72 |
child 8447 | 4fb20c09017a |
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: |
7896
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7815
diff
changeset
|
39 |
sys.stderr.write(' [FIXED]') |
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7815
diff
changeset
|
40 |
sys.stderr.write('\n') |
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
|
41 |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
42 |
def has_eid(session, sqlcursor, eid, eids): |
0 | 43 |
"""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
|
44 |
if eid in eids: |
0 | 45 |
return eids[eid] |
46 |
sqlcursor.execute('SELECT type, source FROM entities WHERE eid=%s' % eid) |
|
47 |
try: |
|
48 |
etype, source = sqlcursor.fetchone() |
|
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7687
diff
changeset
|
49 |
except Exception: |
0 | 50 |
eids[eid] = False |
51 |
return False |
|
52 |
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
|
53 |
try: |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
54 |
# 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
|
55 |
# 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
|
56 |
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
|
57 |
{'x': eid}): |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
58 |
eids[eid] = True |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
59 |
return True |
7815
2a164a9cf81c
[exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7687
diff
changeset
|
60 |
except Exception: # TypeResolverError, Unauthorized... |
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
61 |
pass |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
62 |
eids[eid] = False |
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
63 |
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
|
64 |
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
|
65 |
SQL_PREFIX, eid)) |
0 | 66 |
result = sqlcursor.fetchall() |
67 |
if len(result) == 0: |
|
68 |
eids[eid] = False |
|
69 |
return False |
|
70 |
elif len(result) > 1: |
|
7896
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7815
diff
changeset
|
71 |
msg = (' More than one entity with eid %s exists in source !\n' |
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7815
diff
changeset
|
72 |
' WARNING : Unable to fix this, do it yourself !\n') |
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7815
diff
changeset
|
73 |
sys.stderr.write(msg % eid) |
0 | 74 |
eids[eid] = True |
75 |
return True |
|
76 |
||
77 |
# XXX move to yams? |
|
78 |
def etype_fti_containers(eschema, _done=None): |
|
79 |
if _done is None: |
|
80 |
_done = set() |
|
81 |
_done.add(eschema) |
|
82 |
containers = tuple(eschema.fulltext_containers()) |
|
83 |
if containers: |
|
84 |
for rschema, target in containers: |
|
85 |
if target == 'object': |
|
86 |
targets = rschema.objects(eschema) |
|
87 |
else: |
|
88 |
targets = rschema.subjects(eschema) |
|
89 |
for targeteschema in targets: |
|
90 |
if targeteschema in _done: |
|
91 |
continue |
|
92 |
_done.add(targeteschema) |
|
93 |
for container in etype_fti_containers(targeteschema, _done): |
|
94 |
yield container |
|
95 |
else: |
|
96 |
yield eschema |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
97 |
|
5850
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
98 |
def reindex_entities(schema, session, withpb=True, etypes=None): |
0 | 99 |
"""reindex all entities in the repository""" |
100 |
# deactivate modification_date hook since we don't want them |
|
101 |
# to be updated due to the reindexation |
|
102 |
repo = session.repo |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7040
diff
changeset
|
103 |
cursor = session.cnxset['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
|
104 |
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
|
105 |
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
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
print 'Reindexing entities' |
5850
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
111 |
etypes = set() |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
112 |
for eschema in schema.entities(): |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
113 |
if eschema.final: |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
114 |
continue |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
115 |
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
|
116 |
if not indexable_attrs: |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
117 |
continue |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5693
diff
changeset
|
118 |
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
|
119 |
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
|
120 |
# 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
|
121 |
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
|
122 |
else: |
987086484876
[fti migration] test and fix reindexation of some specific entity types
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5850
diff
changeset
|
123 |
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
|
124 |
', '.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
|
125 |
# 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
|
126 |
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
|
127 |
"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
|
128 |
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
|
129 |
','.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
|
130 |
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
|
131 |
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
|
132 |
pb.update() |
0 | 133 |
# reindex entities by generating rql queries which set all indexable |
134 |
# 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
|
135 |
source = repo.system_source |
0 | 136 |
for eschema in etypes: |
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6624
diff
changeset
|
137 |
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
|
138 |
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
|
139 |
if withpb: |
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
140 |
pb.update() |
0 | 141 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
142 |
|
380 | 143 |
def check_schema(schema, session, eids, fix=1): |
0 | 144 |
"""check serialized schema""" |
145 |
print 'Checking serialized schema' |
|
146 |
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
|
147 |
'VocabularyConstraint', |
0 | 148 |
'RQLVocabularyConstraint') |
5338
3e5a256d17ba
[db-check] fix duplicated schema constraint detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
149 |
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
|
150 |
'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
|
151 |
'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
|
152 |
'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
|
153 |
for count, rn, sn, on, cstrname in session.execute(rql): |
0 | 154 |
if count == 1: |
155 |
continue |
|
156 |
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
|
157 |
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
|
158 |
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
|
159 |
if fix: |
4bf975c049a6
[db-check] RQLConstraint is not a 'unique' constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
160 |
print 'dunno how to fix, do it yourself' |
0 | 161 |
|
162 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
163 |
|
0 | 164 |
def check_text_index(schema, session, eids, fix=1): |
165 |
"""check all entities registered in the text index""" |
|
166 |
print 'Checking text index' |
|
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
167 |
msg = ' Entity with eid %s exists in the text index but in no source (autofix will remove from text index)' |
0 | 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): |
7896
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7815
diff
changeset
|
172 |
sys.stderr.write(msg % eid) |
0 | 173 |
if fix: |
174 |
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
|
175 |
notify_fixed(fix) |
0 | 176 |
|
177 |
||
178 |
def check_entities(schema, session, eids, fix=1): |
|
179 |
"""check all entities registered in the repo system table""" |
|
180 |
print 'Checking entities system table' |
|
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
181 |
# system table but no source |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
182 |
msg = ' Entity with eid %s exists in the system table but in no source (autofix will delete the entity)' |
0 | 183 |
cursor = session.system_sql('SELECT eid FROM entities;') |
184 |
for row in cursor.fetchall(): |
|
185 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
186 |
if not has_eid(session, cursor, eid, eids): |
7896
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7815
diff
changeset
|
187 |
sys.stderr.write(msg % eid) |
0 | 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) |
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
191 |
# source in entities, but no relation cw_source |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
192 |
applcwversion = session.repo.get_versions().get('cubicweb') |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
193 |
if applcwversion >= (3,13,1): # entities.asource appeared in 3.13.1 |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
194 |
cursor = session.system_sql('SELECT e.eid FROM entities as e, cw_CWSource as s ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
195 |
'WHERE s.cw_name=e.asource AND ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
196 |
'NOT EXISTS(SELECT 1 FROM cw_source_relation as cs ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
197 |
' WHERE cs.eid_from=e.eid AND cs.eid_to=s.cw_eid) ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
198 |
'ORDER BY e.eid') |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
199 |
msg = (' Entity with eid %s refers to source in entities table, ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
200 |
'but is missing relation cw_source (autofix will create the relation)\n') |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
201 |
for row in cursor.fetchall(): |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
202 |
sys.stderr.write(msg % row[0]) |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
203 |
if fix: |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
204 |
session.system_sql('INSERT INTO cw_source_relation (eid_from, eid_to) ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
205 |
'SELECT e.eid, s.cw_eid FROM entities as e, cw_CWSource as s ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
206 |
'WHERE s.cw_name=e.asource AND NOT EXISTS(SELECT 1 FROM cw_source_relation as cs ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
207 |
' WHERE cs.eid_from=e.eid AND cs.eid_to=s.cw_eid)') |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
208 |
notify_fixed(True) |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
209 |
# inconsistencies for 'is' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
210 |
msg = ' %s #%s is missing relation "is" (autofix will create the relation)\n' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
211 |
cursor = session.system_sql('SELECT e.type, e.eid FROM entities as e, cw_CWEType as s ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
212 |
'WHERE s.cw_name=e.type AND NOT EXISTS(SELECT 1 FROM is_relation as cs ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
213 |
' WHERE cs.eid_from=e.eid AND cs.eid_to=s.cw_eid) ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
214 |
'ORDER BY e.eid') |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
215 |
for row in cursor.fetchall(): |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
216 |
sys.stderr.write(msg % row) |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
217 |
if fix: |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
218 |
session.system_sql('INSERT INTO is_relation (eid_from, eid_to) ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
219 |
'SELECT e.eid, s.cw_eid FROM entities as e, cw_CWEType as s ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
220 |
'WHERE s.cw_name=e.type AND NOT EXISTS(SELECT 1 FROM is_relation as cs ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
221 |
' WHERE cs.eid_from=e.eid AND cs.eid_to=s.cw_eid)') |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
222 |
notify_fixed(True) |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
223 |
# inconsistencies for 'is_instance_of' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
224 |
msg = ' %s #%s is missing relation "is_instance_of" (autofix will create the relation)\n' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
225 |
cursor = session.system_sql('SELECT e.type, e.eid FROM entities as e, cw_CWEType as s ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
226 |
'WHERE s.cw_name=e.type AND NOT EXISTS(SELECT 1 FROM is_instance_of_relation as cs ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
227 |
' WHERE cs.eid_from=e.eid AND cs.eid_to=s.cw_eid) ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
228 |
'ORDER BY e.eid') |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
229 |
for row in cursor.fetchall(): |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
230 |
sys.stderr.write(msg % row) |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
231 |
if fix: |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
232 |
session.system_sql('INSERT INTO is_instance_of_relation (eid_from, eid_to) ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
233 |
'SELECT e.eid, s.cw_eid FROM entities as e, cw_CWEType as s ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
234 |
'WHERE s.cw_name=e.type AND NOT EXISTS(SELECT 1 FROM is_instance_of_relation as cs ' |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
235 |
' WHERE cs.eid_from=e.eid AND cs.eid_to=s.cw_eid)') |
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
236 |
notify_fixed(True) |
0 | 237 |
print 'Checking entities tables' |
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
238 |
msg = ' Entity with eid %s exists in the %s table but not in the system table (autofix will delete the entity)' |
0 | 239 |
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
|
240 |
if eschema.final: |
0 | 241 |
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
|
242 |
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
|
243 |
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
|
244 |
cursor = session.system_sql('SELECT %s FROM %s;' % (column, table)) |
0 | 245 |
for row in cursor.fetchall(): |
246 |
eid = row[0] |
|
5341
0de53140bd29
[db-check] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5340
diff
changeset
|
247 |
# eids is full since we have fetched everything from the entities table, |
0 | 248 |
# no need to call has_eid |
249 |
if not eid in eids or not eids[eid]: |
|
7896
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7815
diff
changeset
|
250 |
sys.stderr.write(msg % (eid, eschema.type)) |
0 | 251 |
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
|
252 |
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
|
253 |
notify_fixed(fix) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
254 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
255 |
|
0 | 256 |
def bad_related_msg(rtype, target, eid, fix): |
257 |
msg = ' A relation %s with %s eid %s exists but no such entity in sources' |
|
7896
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7815
diff
changeset
|
258 |
sys.stderr.write(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
|
259 |
notify_fixed(fix) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
260 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
261 |
|
0 | 262 |
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
|
263 |
"""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
|
264 |
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
|
265 |
""" |
0 | 266 |
print 'Checking relations' |
267 |
for rschema in schema.relations(): |
|
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
268 |
if rschema.final or rschema.type in PURE_VIRTUAL_RTYPES: |
0 | 269 |
continue |
270 |
if rschema.inlined: |
|
271 |
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
|
272 |
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
|
273 |
column = SQL_PREFIX + str(rschema) |
380 | 274 |
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
|
275 |
column, table, column) |
380 | 276 |
cursor = session.system_sql(sql) |
0 | 277 |
for row in cursor.fetchall(): |
278 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
279 |
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
|
280 |
bad_related_msg(rschema, 'object', eid, fix) |
0 | 281 |
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
|
282 |
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
|
283 |
table, column, column, eid) |
381 | 284 |
session.system_sql(sql) |
0 | 285 |
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
|
286 |
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
|
287 |
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
|
288 |
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
|
289 |
# 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
|
290 |
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
|
291 |
continue |
0 | 292 |
for row in cursor.fetchall(): |
293 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
294 |
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
|
295 |
bad_related_msg(rschema, 'subject', eid, fix) |
0 | 296 |
if fix: |
380 | 297 |
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
|
298 |
rschema, eid) |
380 | 299 |
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
|
300 |
cursor = session.system_sql('SELECT eid_to FROM %s_relation;' % rschema) |
0 | 301 |
for row in cursor.fetchall(): |
302 |
eid = row[0] |
|
5339
b83327846450
[db-check] fix unexistent multisource entity detection
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5338
diff
changeset
|
303 |
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
|
304 |
bad_related_msg(rschema, 'object', eid, fix) |
0 | 305 |
if fix: |
380 | 306 |
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
|
307 |
rschema, eid) |
380 | 308 |
session.system_sql(sql) |
0 | 309 |
|
310 |
||
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
|
311 |
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
|
312 |
"""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
|
313 |
print 'Checking mandatory relations' |
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
314 |
msg = '%s #%s is missing mandatory %s relation %s (autofix will delete the entity)' |
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
|
315 |
for rschema in schema.relations(): |
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
316 |
if rschema.final or rschema.type in PURE_VIRTUAL_RTYPES: |
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
|
317 |
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
|
318 |
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
|
319 |
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
|
320 |
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
|
321 |
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
|
322 |
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
|
323 |
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
|
324 |
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
|
325 |
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
|
326 |
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
|
327 |
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
|
328 |
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
|
329 |
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
|
330 |
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
|
331 |
for entity in session.execute(rql).entities(): |
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
332 |
sys.stderr.write(msg % (entity.__regid__, entity.eid, role, rschema)) |
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
|
333 |
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
|
334 |
#if entity.cw_describe()['source']['uri'] == 'system': XXX |
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
335 |
entity.cw_delete() # XXX this is BRUTAL! |
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
|
336 |
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
|
337 |
|
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
|
338 |
|
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
|
339 |
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
|
340 |
"""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
|
341 |
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
|
342 |
""" |
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
|
343 |
print 'Checking mandatory attributes' |
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
344 |
msg = '%s #%s is missing mandatory attribute %s (autofix will delete the entity)' |
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
|
345 |
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
|
346 |
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
|
347 |
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
|
348 |
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
|
349 |
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
|
350 |
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
|
351 |
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
|
352 |
for entity in session.execute(rql).entities(): |
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
353 |
sys.stderr.write(msg % (entity.__regid__, entity.eid, rschema)) |
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
|
354 |
if fix: |
7682
07b592a62be3
[c-c db-check] drop some deprecation warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7473
diff
changeset
|
355 |
entity.cw_delete() |
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
|
356 |
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
|
357 |
|
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
|
358 |
|
0 | 359 |
def check_metadata(schema, session, eids, fix=1): |
360 |
"""check entities has required metadata |
|
361 |
||
362 |
FIXME: rewrite using RQL queries ? |
|
363 |
""" |
|
364 |
print 'Checking metadata' |
|
365 |
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
|
366 |
eidcolumn = SQL_PREFIX + 'eid' |
7965
d45c0eb39e72
[server] fix integrity checks that destroy data (closes #1972842)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7896
diff
changeset
|
367 |
msg = ' %s with eid %s has no %s (autofix will set it to now)' |
0 | 368 |
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
|
369 |
table = SQL_PREFIX + etype |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
370 |
for rel, default in ( ('creation_date', datetime.now()), |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
713
diff
changeset
|
371 |
('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
|
372 |
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
|
373 |
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
|
374 |
% (eidcolumn, table, column)) |
0 | 375 |
for eid, in cursor.fetchall(): |
7896
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7815
diff
changeset
|
376 |
sys.stderr.write(msg % (etype, eid, rel)) |
0 | 377 |
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
|
378 |
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
|
379 |
% (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
|
380 |
{'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
|
381 |
notify_fixed(fix) |
0 | 382 |
|
383 |
||
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
384 |
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
|
385 |
"""check integrity of instance's repository, |
0 | 386 |
using given user and password to locally connect to the repository |
387 |
(no running cubicweb server needed) |
|
388 |
""" |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7040
diff
changeset
|
389 |
session = repo._get_session(cnx.sessionid, setcnxset=True) |
0 | 390 |
# yo, launch checks |
391 |
if checks: |
|
392 |
eids_cache = {} |
|
7685
902909e5f733
[c-c db-check] disable write security as well so we can delete stuff when needed (e.g. orphan TrInfo)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7684
diff
changeset
|
393 |
with security_enabled(session, read=False, write=False): # ensure no read security |
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
|
394 |
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
|
395 |
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
|
396 |
check_func(repo.schema, session, eids_cache, fix=fix) |
0 | 397 |
if fix: |
398 |
cnx.commit() |
|
399 |
else: |
|
400 |
print |
|
401 |
if not fix: |
|
402 |
print 'WARNING: Diagnostic run, nothing has been corrected' |
|
403 |
if reindex: |
|
404 |
cnx.rollback() |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7040
diff
changeset
|
405 |
session.set_cnxset() |
4675
9233a8350420
[test] don't display progress bar when testing checkintegrity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
406 |
reindex_entities(repo.schema, session, withpb=withpb) |
0 | 407 |
cnx.commit() |