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