author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Thu, 04 Jun 2009 17:21:29 +0200 | |
changeset 2039 | 6c036e4d9ad4 |
parent 1977 | 606923dff11b |
child 2044 | d5589c5bc4e6 |
permissions | -rw-r--r-- |
0 | 1 |
"""functions for schema / permissions (de)serialization using RQL |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1913
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1913
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
10 |
import sys |
0 | 11 |
from itertools import chain |
12 |
||
13 |
from logilab.common.shellutils import ProgressBar |
|
14 |
||
15 |
from yams import schema as schemamod, buildobjs as ybo |
|
16 |
||
17 |
from cubicweb.schema import CONSTRAINTS, ETYPE_NAME_MAP |
|
1869
642a1a120a92
should use sql prefix here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1630
diff
changeset
|
18 |
from cubicweb.server import sqlutils |
0 | 19 |
|
20 |
def group_mapping(cursor, interactive=True): |
|
21 |
"""create a group mapping from an rql cursor |
|
22 |
||
23 |
A group mapping has standard group names as key (managers, owners at least) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
24 |
and the actual CWGroup entity's eid as associated value. |
0 | 25 |
In interactive mode (the default), missing groups'eid will be prompted |
26 |
from the user. |
|
27 |
""" |
|
28 |
res = {} |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
29 |
for eid, name in cursor.execute('Any G, N WHERE G is CWGroup, G name N'): |
0 | 30 |
res[name] = eid |
31 |
if not interactive: |
|
32 |
return res |
|
33 |
missing = [g for g in ('owners', 'managers', 'users', 'guests') if not g in res] |
|
34 |
if missing: |
|
35 |
print 'some native groups are missing but the following groups have been found:' |
|
36 |
print '\n'.join('* %s (%s)' % (n, eid) for n, eid in res.items()) |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
37 |
print |
0 | 38 |
print 'enter the eid of a to group to map to each missing native group' |
39 |
print 'or just type enter to skip permissions granted to a group' |
|
40 |
for group in missing: |
|
41 |
while True: |
|
42 |
value = raw_input('eid for group %s: ' % group).strip() |
|
43 |
if not value: |
|
44 |
continue |
|
45 |
try: |
|
46 |
res[group] = int(value) |
|
47 |
except ValueError: |
|
48 |
print 'eid should be an integer' |
|
49 |
continue |
|
50 |
return res |
|
51 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
52 |
def _set_sql_prefix(prefix): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
53 |
"""3.2.0 migration function: allow to unset/reset SQL_PREFIX""" |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
54 |
for module in ('checkintegrity', 'migractions', 'schemahooks', |
1913
7d7bfd30c7f5
should reset sqlutils.SQL_PREFIX as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1869
diff
changeset
|
55 |
'sources.rql2sql', 'sources.native', 'sqlutils'): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
56 |
try: |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
57 |
sys.modules['cubicweb.server.%s' % module].SQL_PREFIX = prefix |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
58 |
print 'changed SQL_PREFIX for %s' % module |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
59 |
except KeyError: |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
60 |
pass |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
61 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
62 |
def _update_database(schema, sqlcu): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
63 |
"""3.2.0 migration function: update database schema by adding SQL_PREFIX to |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
64 |
entity type tables and columns |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
65 |
""" |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
66 |
for etype in schema.entities(): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
67 |
if etype.is_final(): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
68 |
continue |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
69 |
try: |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
70 |
sql = 'ALTER TABLE %s RENAME TO cw_%s' % ( |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
71 |
etype, ETYPE_NAME_MAP.get(etype, etype)) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
72 |
print sql |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
73 |
sqlcu.execute(sql) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
74 |
except: |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
75 |
pass |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
76 |
for rschema in etype.subject_relations(): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
77 |
if rschema == 'has_text': |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
78 |
continue |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
79 |
if rschema.is_final() or rschema.inlined: |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
80 |
sql = 'ALTER TABLE cw_%s RENAME %s TO cw_%s' % ( |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
81 |
etype, rschema, rschema) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
82 |
print sql |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
83 |
sqlcu.execute(sql) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
84 |
|
0 | 85 |
# schema / perms deserialization ############################################## |
86 |
||
87 |
def deserialize_schema(schema, session): |
|
88 |
"""return a schema according to information stored in an rql database |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
89 |
as CWRType and CWEType entities |
0 | 90 |
""" |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
91 |
# |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
92 |
repo = session.repo |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
93 |
sqlcu = session.pool['system'] |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
94 |
_3_2_migration = False |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
95 |
tables = set(t.lower() for t in repo.system_source.dbhelper.list_tables(sqlcu)) |
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
96 |
if 'eetype' in tables: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
97 |
_3_2_migration = True |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
98 |
# 3.2 migration |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
99 |
_set_sql_prefix('') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
100 |
# first rename entity types whose name changed in 3.2 without adding the |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
101 |
# cw_ prefix |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
102 |
for etype in ('EFRDef', 'ENFRDef', 'ERType', 'EEType', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
103 |
'EConstraintType', 'EConstraint', 'EGroup', 'EUser', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
104 |
'ECache', 'EPermission', 'EProperty'): |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
105 |
if etype.lower() in tables: |
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
106 |
sql = 'ALTER TABLE %s RENAME TO %s' % (etype, |
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
107 |
ETYPE_NAME_MAP[etype]) |
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
108 |
print sql |
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
109 |
sqlcu.execute(sql) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
110 |
# other table renaming done once schema has been readen |
0 | 111 |
# print 'reading schema from the database...' |
112 |
index = {} |
|
113 |
permsdict = deserialize_ertype_permissions(session) |
|
114 |
schema.reading_from_database = True |
|
115 |
for eid, etype, desc, meta in session.execute('Any X, N, D, M WHERE ' |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
116 |
'X is CWEType, X name N, ' |
0 | 117 |
'X description D, X meta M', |
118 |
build_descr=False): |
|
119 |
# base types are already in the schema, skip them |
|
120 |
if etype in schemamod.BASE_TYPES: |
|
121 |
# just set the eid |
|
122 |
eschema = schema.eschema(etype) |
|
123 |
eschema.eid = eid |
|
124 |
index[eid] = eschema |
|
125 |
continue |
|
126 |
if etype in ETYPE_NAME_MAP: # XXX <2.45 bw compat |
|
127 |
print 'fixing etype name from %s to %s' % (etype, ETYPE_NAME_MAP[etype]) |
|
128 |
# can't use write rql queries at this point, use raw sql |
|
1869
642a1a120a92
should use sql prefix here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1630
diff
changeset
|
129 |
session.system_sql('UPDATE %(p)sCWEType SET %(p)sname=%%(n)s WHERE %(p)seid=%%(x)s' |
642a1a120a92
should use sql prefix here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1630
diff
changeset
|
130 |
% {'p': sqlutils.SQL_PREFIX}, |
0 | 131 |
{'x': eid, 'n': ETYPE_NAME_MAP[etype]}) |
132 |
session.system_sql('UPDATE entities SET type=%(n)s WHERE type=%(x)s', |
|
133 |
{'x': etype, 'n': ETYPE_NAME_MAP[etype]}) |
|
134 |
session.commit(False) |
|
135 |
try: |
|
136 |
session.system_sql('UPDATE deleted_entities SET type=%(n)s WHERE type=%(x)s', |
|
137 |
{'x': etype, 'n': ETYPE_NAME_MAP[etype]}) |
|
138 |
except: |
|
139 |
pass |
|
140 |
tocleanup = [eid] |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
141 |
tocleanup += (eid for eid, (eidetype, uri, extid) in repo._type_source_cache.items() |
0 | 142 |
if etype == eidetype) |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
143 |
repo.clear_caches(tocleanup) |
0 | 144 |
session.commit(False) |
145 |
etype = ETYPE_NAME_MAP[etype] |
|
146 |
etype = ybo.EntityType(name=etype, description=desc, meta=meta, eid=eid) |
|
147 |
eschema = schema.add_entity_type(etype) |
|
148 |
index[eid] = eschema |
|
149 |
set_perms(eschema, permsdict.get(eid, {})) |
|
150 |
try: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
151 |
rset = session.execute('Any XN, ETN WHERE X is CWEType, X name XN, ' |
0 | 152 |
'X specializes ET, ET name ETN') |
153 |
except: # `specializes` relation not available for versions prior to 2.50 |
|
154 |
session.rollback(False) |
|
155 |
else: |
|
156 |
for etype, stype in rset: |
|
157 |
eschema = schema.eschema(etype) |
|
158 |
seschema = schema.eschema(stype) |
|
159 |
eschema._specialized_type = stype |
|
160 |
seschema._specialized_by.append(etype) |
|
161 |
for eid, rtype, desc, meta, sym, il in session.execute( |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
162 |
'Any X,N,D,M,S,I WHERE X is CWRType, X name N, X description D, ' |
0 | 163 |
'X meta M, X symetric S, X inlined I', build_descr=False): |
164 |
try: |
|
165 |
# bw compat: fulltext_container added in 2.47 |
|
166 |
ft_container = session.execute('Any FTC WHERE X eid %(x)s, X fulltext_container FTC', |
|
167 |
{'x': eid}).rows[0][0] |
|
168 |
except: |
|
169 |
ft_container = None |
|
170 |
session.rollback(False) |
|
171 |
rtype = ybo.RelationType(name=rtype, description=desc, meta=bool(meta), |
|
172 |
symetric=bool(sym), inlined=bool(il), |
|
173 |
fulltext_container=ft_container, eid=eid) |
|
174 |
rschema = schema.add_relation_type(rtype) |
|
175 |
index[eid] = rschema |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
176 |
set_perms(rschema, permsdict.get(eid, {})) |
0 | 177 |
cstrsdict = deserialize_rdef_constraints(session) |
178 |
for values in session.execute( |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
179 |
'Any X,SE,RT,OE,CARD,ORD,DESC,IDX,FTIDX,I18N,DFLT WHERE X is CWAttribute,' |
0 | 180 |
'X relation_type RT, X cardinality CARD, X ordernum ORD, X indexed IDX,' |
181 |
'X description DESC, X internationalizable I18N, X defaultval DFLT,' |
|
182 |
'X fulltextindexed FTIDX, X from_entity SE, X to_entity OE', |
|
183 |
build_descr=False): |
|
184 |
rdefeid, seid, reid, teid, card, ord, desc, idx, ftidx, i18n, default = values |
|
185 |
constraints = cstrsdict.get(rdefeid, ()) |
|
186 |
frometype = index[seid].type |
|
187 |
rtype = index[reid].type |
|
188 |
toetype = index[teid].type |
|
189 |
rdef = ybo.RelationDefinition(frometype, rtype, toetype, cardinality=card, |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
190 |
order=ord, description=desc, |
0 | 191 |
constraints=constraints, |
192 |
indexed=idx, fulltextindexed=ftidx, |
|
193 |
internationalizable=i18n, |
|
194 |
default=default, eid=rdefeid) |
|
195 |
schema.add_relation_def(rdef) |
|
196 |
for values in session.execute( |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
197 |
'Any X,SE,RT,OE,CARD,ORD,DESC,C WHERE X is CWRelation, X relation_type RT,' |
0 | 198 |
'X cardinality CARD, X ordernum ORD, X description DESC, ' |
199 |
'X from_entity SE, X to_entity OE, X composite C', build_descr=False): |
|
200 |
rdefeid, seid, reid, teid, card, ord, desc, c = values |
|
201 |
frometype = index[seid].type |
|
202 |
rtype = index[reid].type |
|
203 |
toetype = index[teid].type |
|
204 |
constraints = cstrsdict.get(rdefeid, ()) |
|
205 |
rdef = ybo.RelationDefinition(frometype, rtype, toetype, cardinality=card, |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
206 |
order=ord, description=desc, |
0 | 207 |
composite=c, constraints=constraints, |
208 |
eid=rdefeid) |
|
209 |
schema.add_relation_def(rdef) |
|
210 |
schema.infer_specialization_rules() |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
211 |
if _3_2_migration: |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
212 |
_update_database(schema, sqlcu) |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
213 |
_set_sql_prefix('cw_') |
0 | 214 |
session.commit() |
215 |
schema.reading_from_database = False |
|
216 |
||
217 |
||
218 |
def deserialize_ertype_permissions(session): |
|
219 |
"""return sect action:groups associations for the given |
|
220 |
entity or relation schema with its eid, according to schema's |
|
221 |
permissions stored in the database as [read|add|delete|update]_permission |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
222 |
relations between CWEType/CWRType and CWGroup entities |
0 | 223 |
""" |
224 |
res = {} |
|
225 |
for action in ('read', 'add', 'update', 'delete'): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
226 |
rql = 'Any E,N WHERE G is CWGroup, G name N, E %s_permission G' % action |
0 | 227 |
for eid, gname in session.execute(rql, build_descr=False): |
228 |
res.setdefault(eid, {}).setdefault(action, []).append(gname) |
|
229 |
rql = ('Any E,X,EXPR,V WHERE X is RQLExpression, X expression EXPR, ' |
|
230 |
'E %s_permission X, X mainvars V' % action) |
|
231 |
for eid, expreid, expr, mainvars in session.execute(rql, build_descr=False): |
|
232 |
# we don't know yet if it's a rql expr for an entity or a relation, |
|
233 |
# so append a tuple to differentiate from groups and so we'll be |
|
234 |
# able to instantiate it later |
|
235 |
res.setdefault(eid, {}).setdefault(action, []).append( (expr, mainvars, expreid) ) |
|
236 |
return res |
|
237 |
||
238 |
def set_perms(erschema, permsdict): |
|
239 |
"""set permissions on the given erschema according to the permission |
|
240 |
definition dictionary as built by deserialize_ertype_permissions for a |
|
241 |
given erschema's eid |
|
242 |
""" |
|
243 |
for action in erschema.ACTIONS: |
|
244 |
actperms = [] |
|
245 |
for something in permsdict.get(action, ()): |
|
246 |
if isinstance(something, tuple): |
|
247 |
actperms.append(erschema.rql_expression(*something)) |
|
248 |
else: # group name |
|
249 |
actperms.append(something) |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
250 |
erschema.set_permissions(action, actperms) |
0 | 251 |
|
252 |
||
253 |
def deserialize_rdef_constraints(session): |
|
254 |
"""return the list of relation definition's constraints as instances""" |
|
255 |
res = {} |
|
256 |
for rdefeid, ceid, ct, val in session.execute( |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
257 |
'Any E, X,TN,V WHERE E constrained_by X, X is CWConstraint, ' |
0 | 258 |
'X cstrtype T, T name TN, X value V', build_descr=False): |
259 |
cstr = CONSTRAINTS[ct].deserialize(val) |
|
260 |
cstr.eid = ceid |
|
261 |
res.setdefault(rdefeid, []).append(cstr) |
|
262 |
return res |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
263 |
|
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
264 |
|
0 | 265 |
# schema / perms serialization ################################################ |
266 |
||
267 |
def serialize_schema(cursor, schema, verbose=False): |
|
268 |
"""synchronize schema and permissions in the database according to |
|
269 |
current schema |
|
270 |
""" |
|
271 |
print 'serializing the schema, this may take some time' |
|
272 |
eschemas = schema.entities() |
|
273 |
aller = eschemas + schema.relations() |
|
274 |
if not verbose: |
|
275 |
pb_size = len(aller) + len(CONSTRAINTS) + len([x for x in eschemas if x.specializes()]) |
|
276 |
pb = ProgressBar(pb_size) |
|
277 |
for cstrtype in CONSTRAINTS: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
278 |
rql = 'INSERT CWConstraintType X: X name "%s"' % cstrtype |
0 | 279 |
if verbose: |
280 |
print rql |
|
281 |
cursor.execute(rql) |
|
282 |
if not verbose: |
|
283 |
pb.update() |
|
284 |
groupmap = group_mapping(cursor, interactive=False) |
|
285 |
for ertype in aller: |
|
286 |
# skip eid and has_text relations |
|
287 |
if ertype in ('eid', 'identity', 'has_text',): |
|
288 |
pb.update() |
|
289 |
continue |
|
290 |
for rql, kwargs in erschema2rql(schema[ertype]): |
|
291 |
if verbose: |
|
292 |
print rql % kwargs |
|
293 |
cursor.execute(rql, kwargs) |
|
294 |
for rql, kwargs in erperms2rql(schema[ertype], groupmap): |
|
295 |
if verbose: |
|
296 |
print rql |
|
297 |
cursor.execute(rql, kwargs) |
|
298 |
if not verbose: |
|
299 |
pb.update() |
|
300 |
for rql, kwargs in specialize2rql(schema): |
|
301 |
if verbose: |
|
302 |
print rql % kwargs |
|
303 |
cursor.execute(rql, kwargs) |
|
304 |
if not verbose: |
|
305 |
pb.update() |
|
306 |
print |
|
307 |
||
308 |
||
309 |
def _ervalues(erschema): |
|
310 |
try: |
|
311 |
type_ = unicode(erschema.type) |
|
312 |
except UnicodeDecodeError, e: |
|
313 |
raise Exception("can't decode %s [was %s]" % (erschema.type, e)) |
|
314 |
try: |
|
315 |
desc = unicode(erschema.description) or u'' |
|
316 |
except UnicodeDecodeError, e: |
|
317 |
raise Exception("can't decode %s [was %s]" % (erschema.description, e)) |
|
318 |
return { |
|
319 |
'name': type_, |
|
320 |
'meta': erschema.meta, |
|
321 |
'final': erschema.is_final(), |
|
322 |
'description': desc, |
|
323 |
} |
|
324 |
||
325 |
def eschema_relations_values(eschema): |
|
326 |
values = _ervalues(eschema) |
|
327 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
328 |
return relations, values |
|
329 |
||
330 |
# XXX 2.47 migration |
|
331 |
HAS_FULLTEXT_CONTAINER = True |
|
332 |
||
333 |
def rschema_relations_values(rschema): |
|
334 |
values = _ervalues(rschema) |
|
335 |
values['final'] = rschema.is_final() |
|
336 |
values['symetric'] = rschema.symetric |
|
337 |
values['inlined'] = rschema.inlined |
|
338 |
if HAS_FULLTEXT_CONTAINER: |
|
339 |
if isinstance(rschema.fulltext_container, str): |
|
340 |
values['fulltext_container'] = unicode(rschema.fulltext_container) |
|
341 |
else: |
|
342 |
values['fulltext_container'] = rschema.fulltext_container |
|
343 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
344 |
return relations, values |
|
345 |
||
346 |
def _rdef_values(rschema, objtype, props): |
|
347 |
amap = {'order': 'ordernum'} |
|
348 |
values = {} |
|
349 |
for prop, default in rschema.rproperty_defs(objtype).iteritems(): |
|
350 |
if prop in ('eid', 'constraints', 'uid', 'infered'): |
|
351 |
continue |
|
352 |
value = props.get(prop, default) |
|
353 |
if prop in ('indexed', 'fulltextindexed', 'internationalizable'): |
|
354 |
value = bool(value) |
|
355 |
elif prop == 'ordernum': |
|
356 |
value = int(value) |
|
357 |
elif isinstance(value, str): |
|
358 |
value = unicode(value) |
|
359 |
values[amap.get(prop, prop)] = value |
|
360 |
return values |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
361 |
|
0 | 362 |
def nfrdef_relations_values(rschema, objtype, props): |
363 |
values = _rdef_values(rschema, objtype, props) |
|
364 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
365 |
return relations, values |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
366 |
|
0 | 367 |
def frdef_relations_values(rschema, objtype, props): |
368 |
values = _rdef_values(rschema, objtype, props) |
|
369 |
default = values['default'] |
|
370 |
del values['default'] |
|
371 |
if default is not None: |
|
372 |
if default is False: |
|
373 |
default = u'' |
|
374 |
elif not isinstance(default, unicode): |
|
375 |
default = unicode(default) |
|
376 |
values['defaultval'] = default |
|
377 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
378 |
return relations, values |
|
379 |
||
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
380 |
|
0 | 381 |
def __rdef2rql(genmap, rschema, subjtype=None, objtype=None, props=None): |
382 |
if subjtype is None: |
|
383 |
assert objtype is None |
|
384 |
assert props is None |
|
385 |
targets = rschema.iter_rdefs() |
|
386 |
else: |
|
387 |
assert not objtype is None |
|
388 |
targets = [(subjtype, objtype)] |
|
389 |
for subjtype, objtype in targets: |
|
390 |
if props is None: |
|
391 |
_props = rschema.rproperties(subjtype, objtype) |
|
392 |
else: |
|
393 |
_props = props |
|
394 |
# don't serialize infered relations |
|
395 |
if _props.get('infered'): |
|
396 |
continue |
|
397 |
gen = genmap[rschema.is_final()] |
|
398 |
for rql, values in gen(rschema, subjtype, objtype, _props): |
|
399 |
yield rql, values |
|
400 |
||
401 |
||
402 |
def schema2rql(schema, skip=None, allow=None): |
|
403 |
"""return a list of rql insert statements to enter the schema in the |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
404 |
database as CWRType and CWEType entities |
0 | 405 |
""" |
406 |
assert not (skip is not None and allow is not None), \ |
|
407 |
'can\'t use both skip and allow' |
|
408 |
all = schema.entities() + schema.relations() |
|
409 |
if skip is not None: |
|
410 |
return chain(*[erschema2rql(schema[t]) for t in all if not t in skip]) |
|
411 |
elif allow is not None: |
|
412 |
return chain(*[erschema2rql(schema[t]) for t in all if t in allow]) |
|
413 |
return chain(*[erschema2rql(schema[t]) for t in all]) |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
414 |
|
0 | 415 |
def erschema2rql(erschema): |
416 |
if isinstance(erschema, schemamod.EntitySchema): |
|
417 |
return eschema2rql(erschema) |
|
418 |
return rschema2rql(erschema) |
|
419 |
||
420 |
def eschema2rql(eschema): |
|
421 |
"""return a list of rql insert statements to enter an entity schema |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
422 |
in the database as an CWEType entity |
0 | 423 |
""" |
424 |
relations, values = eschema_relations_values(eschema) |
|
425 |
# NOTE: 'specializes' relation can't be inserted here since there's no |
|
426 |
# way to make sure the parent type is inserted before the child type |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
427 |
yield 'INSERT CWEType X: %s' % ','.join(relations) , values |
0 | 428 |
|
429 |
def specialize2rql(schema): |
|
430 |
for eschema in schema.entities(): |
|
431 |
for rql, kwargs in eschemaspecialize2rql(eschema): |
|
432 |
yield rql, kwargs |
|
433 |
||
434 |
def eschemaspecialize2rql(eschema): |
|
435 |
specialized_type = eschema.specializes() |
|
436 |
if specialized_type: |
|
437 |
values = {'x': eschema.type, 'et': specialized_type.type} |
|
438 |
yield 'SET X specializes ET WHERE X name %(x)s, ET name %(et)s', values |
|
439 |
||
440 |
def rschema2rql(rschema, addrdef=True): |
|
441 |
"""return a list of rql insert statements to enter a relation schema |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
442 |
in the database as an CWRType entity |
0 | 443 |
""" |
444 |
if rschema.type == 'has_text': |
|
445 |
return |
|
446 |
relations, values = rschema_relations_values(rschema) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
447 |
yield 'INSERT CWRType X: %s' % ','.join(relations), values |
0 | 448 |
if addrdef: |
449 |
for rql, values in rdef2rql(rschema): |
|
450 |
yield rql, values |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
451 |
|
0 | 452 |
def rdef2rql(rschema, subjtype=None, objtype=None, props=None): |
453 |
genmap = {True: frdef2rql, False: nfrdef2rql} |
|
454 |
return __rdef2rql(genmap, rschema, subjtype, objtype, props) |
|
455 |
||
456 |
||
457 |
_LOCATE_RDEF_RQL0 = 'X relation_type ER,X from_entity SE,X to_entity OE' |
|
458 |
_LOCATE_RDEF_RQL1 = 'SE name %(se)s,ER name %(rt)s,OE name %(oe)s' |
|
459 |
||
460 |
def frdef2rql(rschema, subjtype, objtype, props): |
|
461 |
relations, values = frdef_relations_values(rschema, objtype, props) |
|
462 |
relations.append(_LOCATE_RDEF_RQL0) |
|
463 |
values.update({'se': str(subjtype), 'rt': str(rschema), 'oe': str(objtype)}) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
464 |
yield 'INSERT CWAttribute X: %s WHERE %s' % (','.join(relations), _LOCATE_RDEF_RQL1), values |
0 | 465 |
for rql, values in rdefrelations2rql(rschema, subjtype, objtype, props): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
466 |
yield rql + ', EDEF is CWAttribute', values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
467 |
|
0 | 468 |
def nfrdef2rql(rschema, subjtype, objtype, props): |
469 |
relations, values = nfrdef_relations_values(rschema, objtype, props) |
|
470 |
relations.append(_LOCATE_RDEF_RQL0) |
|
471 |
values.update({'se': str(subjtype), 'rt': str(rschema), 'oe': str(objtype)}) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
472 |
yield 'INSERT CWRelation X: %s WHERE %s' % (','.join(relations), _LOCATE_RDEF_RQL1), values |
0 | 473 |
for rql, values in rdefrelations2rql(rschema, subjtype, objtype, props): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
474 |
yield rql + ', EDEF is CWRelation', values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
475 |
|
0 | 476 |
def rdefrelations2rql(rschema, subjtype, objtype, props): |
477 |
iterators = [] |
|
478 |
for constraint in props['constraints']: |
|
479 |
iterators.append(constraint2rql(rschema, subjtype, objtype, constraint)) |
|
480 |
return chain(*iterators) |
|
481 |
||
482 |
def constraint2rql(rschema, subjtype, objtype, constraint): |
|
483 |
values = {'ctname': unicode(constraint.type()), |
|
484 |
'value': unicode(constraint.serialize()), |
|
485 |
'rt': str(rschema), 'se': str(subjtype), 'oe': str(objtype)} |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
486 |
yield 'INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X WHERE \ |
0 | 487 |
CT name %(ctname)s, EDEF relation_type ER, EDEF from_entity SE, EDEF to_entity OE, \ |
488 |
ER name %(rt)s, SE name %(se)s, OE name %(oe)s', values |
|
489 |
||
490 |
def perms2rql(schema, groupmapping): |
|
491 |
"""return rql insert statements to enter the schema's permissions in |
|
492 |
the database as [read|add|delete|update]_permission relations between |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
493 |
CWEType/CWRType and CWGroup entities |
0 | 494 |
|
495 |
groupmapping is a dictionnary mapping standard group names to |
|
496 |
eids |
|
497 |
""" |
|
498 |
for etype in sorted(schema.entities()): |
|
499 |
yield erperms2rql(schema[etype], groupmapping) |
|
500 |
for rtype in sorted(schema.relations()): |
|
501 |
yield erperms2rql(schema[rtype], groupmapping) |
|
502 |
||
503 |
def erperms2rql(erschema, groupmapping): |
|
504 |
"""return rql insert statements to enter the entity or relation |
|
505 |
schema's permissions in the database as |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
506 |
[read|add|delete|update]_permission relations between CWEType/CWRType |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
507 |
and CWGroup entities |
0 | 508 |
""" |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
509 |
etype = isinstance(erschema, schemamod.EntitySchema) and 'CWEType' or 'CWRType' |
0 | 510 |
for action in erschema.ACTIONS: |
511 |
for group in sorted(erschema.get_groups(action)): |
|
512 |
try: |
|
513 |
yield ('SET X %s_permission Y WHERE X is %s, X name "%s", Y eid %s' |
|
514 |
% (action, etype, erschema, groupmapping[group]), None) |
|
515 |
except KeyError: |
|
516 |
continue |
|
517 |
for rqlexpr in sorted(erschema.get_rqlexprs(action)): |
|
518 |
yield ('INSERT RQLExpression E: E expression %%(e)s, E exprtype %%(t)s, ' |
|
519 |
'E mainvars %%(v)s, X %s_permission E ' |
|
520 |
'WHERE X is %s, X name "%s"' % (action, etype, erschema), |
|
521 |
{'e': unicode(rqlexpr.expression), 'v': unicode(rqlexpr.mainvars), |
|
522 |
't': unicode(rqlexpr.__class__.__name__)}) |
|
523 |
||
524 |
||
525 |
def updateeschema2rql(eschema): |
|
526 |
relations, values = eschema_relations_values(eschema) |
|
527 |
values['et'] = eschema.type |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
528 |
yield 'SET %s WHERE X is CWEType, X name %%(et)s' % ','.join(relations), values |
0 | 529 |
|
530 |
def updaterschema2rql(rschema): |
|
531 |
relations, values = rschema_relations_values(rschema) |
|
532 |
values['rt'] = rschema.type |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
533 |
yield 'SET %s WHERE X is CWRType, X name %%(rt)s' % ','.join(relations), values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
534 |
|
0 | 535 |
def updaterdef2rql(rschema, subjtype=None, objtype=None, props=None): |
536 |
genmap = {True: updatefrdef2rql, False: updatenfrdef2rql} |
|
537 |
return __rdef2rql(genmap, rschema, subjtype, objtype, props) |
|
538 |
||
539 |
def updatefrdef2rql(rschema, subjtype, objtype, props): |
|
540 |
relations, values = frdef_relations_values(rschema, objtype, props) |
|
541 |
values.update({'se': subjtype, 'rt': str(rschema), 'oe': objtype}) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
542 |
yield 'SET %s WHERE %s, %s, X is CWAttribute' % (','.join(relations), |
0 | 543 |
_LOCATE_RDEF_RQL0, |
544 |
_LOCATE_RDEF_RQL1), values |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
545 |
|
0 | 546 |
def updatenfrdef2rql(rschema, subjtype, objtype, props): |
547 |
relations, values = nfrdef_relations_values(rschema, objtype, props) |
|
548 |
values.update({'se': subjtype, 'rt': str(rschema), 'oe': objtype}) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
549 |
yield 'SET %s WHERE %s, %s, X is CWRelation' % (','.join(relations), |
0 | 550 |
_LOCATE_RDEF_RQL0, |
551 |
_LOCATE_RDEF_RQL1), values |