author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 17 Mar 2010 09:21:58 +0100 | |
branch | stable |
changeset 4928 | cbca73dc9753 |
parent 4800 | 686419cd2dff |
child 4808 | 23df4a120c96 |
permissions | -rw-r--r-- |
0 | 1 |
"""functions for schema / permissions (de)serialization using RQL |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3897
diff
changeset
|
4 |
:copyright: 2001-2010 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 |
||
3854
8633cd05b6b5
no progress bar in apycot environment
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
10 |
import os |
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 |
||
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
17 |
from cubicweb.schema import CONSTRAINTS, ETYPE_NAME_MAP, VIRTUAL_RTYPES |
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 = {} |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
29 |
for eid, name in cursor.execute('Any G, N WHERE G is CWGroup, G name N', |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
30 |
build_descr=False): |
0 | 31 |
res[name] = eid |
32 |
if not interactive: |
|
33 |
return res |
|
34 |
missing = [g for g in ('owners', 'managers', 'users', 'guests') if not g in res] |
|
35 |
if missing: |
|
36 |
print 'some native groups are missing but the following groups have been found:' |
|
37 |
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
|
38 |
print |
0 | 39 |
print 'enter the eid of a to group to map to each missing native group' |
40 |
print 'or just type enter to skip permissions granted to a group' |
|
41 |
for group in missing: |
|
42 |
while True: |
|
43 |
value = raw_input('eid for group %s: ' % group).strip() |
|
44 |
if not value: |
|
45 |
continue |
|
46 |
try: |
|
47 |
res[group] = int(value) |
|
48 |
except ValueError: |
|
49 |
print 'eid should be an integer' |
|
50 |
continue |
|
51 |
return res |
|
52 |
||
53 |
# schema / perms deserialization ############################################## |
|
4791
b9a1b16a8d26
fix schema serialization, bad repartition of changes between stable and default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4789
diff
changeset
|
54 |
|
0 | 55 |
def deserialize_schema(schema, session): |
56 |
"""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
|
57 |
as CWRType and CWEType entities |
0 | 58 |
""" |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
59 |
repo = session.repo |
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
60 |
dbhelper = repo.system_source.dbhelper |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
61 |
# 3.6 migration |
4475
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
62 |
sqlcu = session.pool['system'] |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
63 |
sqlcu.execute("SELECT * FROM cw_CWRType WHERE cw_name='symetric'") |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
64 |
if sqlcu.fetchall(): |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
65 |
sql = dbhelper.sql_rename_col('cw_CWRType', 'cw_symetric', 'cw_symmetric', |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
66 |
dbhelper.TYPE_MAPPING['Boolean'], True) |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
67 |
sqlcu.execute(sql) |
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
68 |
sqlcu.execute("UPDATE cw_CWRType SET cw_name='symmetric' WHERE cw_name='symetric'") |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
69 |
sidx = {} |
0 | 70 |
permsdict = deserialize_ertype_permissions(session) |
71 |
schema.reading_from_database = True |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
72 |
for eid, etype, desc in session.execute( |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
73 |
'Any X, N, D WHERE X is CWEType, X name N, X description D', |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
74 |
build_descr=False): |
0 | 75 |
# base types are already in the schema, skip them |
76 |
if etype in schemamod.BASE_TYPES: |
|
77 |
# just set the eid |
|
78 |
eschema = schema.eschema(etype) |
|
79 |
eschema.eid = eid |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
80 |
sidx[eid] = eschema |
0 | 81 |
continue |
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
82 |
if etype in ETYPE_NAME_MAP: |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
83 |
netype = ETYPE_NAME_MAP[etype] |
0 | 84 |
# 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
|
85 |
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
|
86 |
% {'p': sqlutils.SQL_PREFIX}, |
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
87 |
{'x': eid, 'n': netype}) |
0 | 88 |
session.system_sql('UPDATE entities SET type=%(n)s WHERE type=%(x)s', |
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
89 |
{'x': etype, 'n': netype}) |
0 | 90 |
session.commit(False) |
91 |
try: |
|
92 |
session.system_sql('UPDATE deleted_entities SET type=%(n)s WHERE type=%(x)s', |
|
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
93 |
{'x': etype, 'n': netype}) |
0 | 94 |
except: |
95 |
pass |
|
96 |
tocleanup = [eid] |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
97 |
tocleanup += (eid for eid, (eidetype, uri, extid) in repo._type_source_cache.items() |
0 | 98 |
if etype == eidetype) |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
99 |
repo.clear_caches(tocleanup) |
0 | 100 |
session.commit(False) |
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
101 |
etype = netype |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2044
diff
changeset
|
102 |
etype = ybo.EntityType(name=etype, description=desc, eid=eid) |
0 | 103 |
eschema = schema.add_entity_type(etype) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
104 |
sidx[eid] = eschema |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
105 |
set_perms(eschema, permsdict) |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
106 |
for etype, stype in session.execute( |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
107 |
'Any XN, ETN WHERE X is CWEType, X name XN, X specializes ET, ET name ETN', |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
108 |
build_descr=False): |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
109 |
schema.eschema(etype)._specialized_type = stype |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
110 |
schema.eschema(stype)._specialized_by.append(etype) |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
111 |
for eid, rtype, desc, sym, il, ftc in session.execute( |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
112 |
'Any X,N,D,S,I,FTC WHERE X is CWRType, X name N, X description D, ' |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
113 |
'X symmetric S, X inlined I, X fulltext_container FTC', build_descr=False): |
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2044
diff
changeset
|
114 |
rtype = ybo.RelationType(name=rtype, description=desc, |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
115 |
symmetric=bool(sym), inlined=bool(il), |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
116 |
fulltext_container=ftc, eid=eid) |
0 | 117 |
rschema = schema.add_relation_type(rtype) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
118 |
sidx[eid] = rschema |
0 | 119 |
cstrsdict = deserialize_rdef_constraints(session) |
120 |
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
|
121 |
'Any X,SE,RT,OE,CARD,ORD,DESC,IDX,FTIDX,I18N,DFLT WHERE X is CWAttribute,' |
0 | 122 |
'X relation_type RT, X cardinality CARD, X ordernum ORD, X indexed IDX,' |
123 |
'X description DESC, X internationalizable I18N, X defaultval DFLT,' |
|
124 |
'X fulltextindexed FTIDX, X from_entity SE, X to_entity OE', |
|
125 |
build_descr=False): |
|
126 |
rdefeid, seid, reid, teid, card, ord, desc, idx, ftidx, i18n, default = values |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
127 |
rdef = ybo.RelationDefinition(sidx[seid].type, sidx[reid].type, sidx[teid].type, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
128 |
cardinality=card, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
129 |
constraints=cstrsdict.get(rdefeid, ()), |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
130 |
order=ord, description=desc, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
131 |
indexed=idx, fulltextindexed=ftidx, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
132 |
internationalizable=i18n, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
133 |
default=default, eid=rdefeid) |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
134 |
rdefs = schema.add_relation_def(rdef) |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
135 |
# rdefs can be None on duplicated relation definitions (e.g. symmetrics) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
136 |
if rdefs is not None: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
137 |
set_perms(rdefs, permsdict) |
0 | 138 |
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
|
139 |
'Any X,SE,RT,OE,CARD,ORD,DESC,C WHERE X is CWRelation, X relation_type RT,' |
0 | 140 |
'X cardinality CARD, X ordernum ORD, X description DESC, ' |
141 |
'X from_entity SE, X to_entity OE, X composite C', build_descr=False): |
|
142 |
rdefeid, seid, reid, teid, card, ord, desc, c = values |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
143 |
rdef = ybo.RelationDefinition(sidx[seid].type, sidx[reid].type, sidx[teid].type, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
144 |
constraints=cstrsdict.get(rdefeid, ()), |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
145 |
cardinality=card, order=ord, description=desc, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
146 |
composite=c, eid=rdefeid) |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
147 |
rdefs = schema.add_relation_def(rdef) |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
148 |
# rdefs can be None on duplicated relation definitions (e.g. symmetrics) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
149 |
if rdefs is not None: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
150 |
set_perms(rdefs, permsdict) |
0 | 151 |
schema.infer_specialization_rules() |
152 |
session.commit() |
|
153 |
schema.reading_from_database = False |
|
154 |
||
155 |
||
156 |
def deserialize_ertype_permissions(session): |
|
157 |
"""return sect action:groups associations for the given |
|
158 |
entity or relation schema with its eid, according to schema's |
|
159 |
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
|
160 |
relations between CWEType/CWRType and CWGroup entities |
0 | 161 |
""" |
162 |
res = {} |
|
163 |
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
|
164 |
rql = 'Any E,N WHERE G is CWGroup, G name N, E %s_permission G' % action |
0 | 165 |
for eid, gname in session.execute(rql, build_descr=False): |
166 |
res.setdefault(eid, {}).setdefault(action, []).append(gname) |
|
167 |
rql = ('Any E,X,EXPR,V WHERE X is RQLExpression, X expression EXPR, ' |
|
168 |
'E %s_permission X, X mainvars V' % action) |
|
169 |
for eid, expreid, expr, mainvars in session.execute(rql, build_descr=False): |
|
170 |
# we don't know yet if it's a rql expr for an entity or a relation, |
|
171 |
# so append a tuple to differentiate from groups and so we'll be |
|
172 |
# able to instantiate it later |
|
173 |
res.setdefault(eid, {}).setdefault(action, []).append( (expr, mainvars, expreid) ) |
|
174 |
return res |
|
175 |
||
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
176 |
def deserialize_rdef_constraints(session): |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
177 |
"""return the list of relation definition's constraints as instances""" |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
178 |
res = {} |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
179 |
for rdefeid, ceid, ct, val in session.execute( |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
180 |
'Any E, X,TN,V WHERE E constrained_by X, X is CWConstraint, ' |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
181 |
'X cstrtype T, T name TN, X value V', build_descr=False): |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
182 |
cstr = CONSTRAINTS[ct].deserialize(val) |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
183 |
cstr.eid = ceid |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
184 |
res.setdefault(rdefeid, []).append(cstr) |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
185 |
return res |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
186 |
|
0 | 187 |
def set_perms(erschema, permsdict): |
188 |
"""set permissions on the given erschema according to the permission |
|
189 |
definition dictionary as built by deserialize_ertype_permissions for a |
|
190 |
given erschema's eid |
|
191 |
""" |
|
4570
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
192 |
# reset erschema permissions here to avoid getting yams default anyway |
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
193 |
erschema.permissions = dict((action, ()) for action in erschema.ACTIONS) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
194 |
try: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
195 |
thispermsdict = permsdict[erschema.eid] |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
196 |
except KeyError: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
197 |
return |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
198 |
for action, somethings in thispermsdict.iteritems(): |
4570
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
199 |
# XXX cw < 3.6.1 bw compat |
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
200 |
if isinstance(erschema, schemamod.RelationDefinitionSchema) and erschema.final and action == 'add': |
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
201 |
action = 'update' |
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
202 |
erschema.permissions[action] = tuple( |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
203 |
isinstance(p, tuple) and erschema.rql_expression(*p) or p |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
204 |
for p in somethings) |
0 | 205 |
|
206 |
||
207 |
# schema / perms serialization ################################################ |
|
208 |
||
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
209 |
def serialize_schema(cursor, schema): |
0 | 210 |
"""synchronize schema and permissions in the database according to |
211 |
current schema |
|
212 |
""" |
|
3737 | 213 |
quiet = os.environ.get('APYCOT_ROOT') |
3723
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
214 |
if not quiet: |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
215 |
_title = '-> storing the schema in the database ' |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
216 |
print _title, |
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
217 |
execute = cursor.unsafe_execute |
0 | 218 |
eschemas = schema.entities() |
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
219 |
if not quiet: |
4791
b9a1b16a8d26
fix schema serialization, bad repartition of changes between stable and default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4789
diff
changeset
|
220 |
pb_size = (len(eschemas + schema.relations()) |
b9a1b16a8d26
fix schema serialization, bad repartition of changes between stable and default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4789
diff
changeset
|
221 |
+ len(CONSTRAINTS) |
b9a1b16a8d26
fix schema serialization, bad repartition of changes between stable and default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4789
diff
changeset
|
222 |
+ len([x for x in eschemas if x.specializes()])) |
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
223 |
pb = ProgressBar(pb_size, title=_title) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
224 |
else: |
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
225 |
pb = None |
4800
686419cd2dff
nicer explanation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4799
diff
changeset
|
226 |
groupmap = group_mapping(cursor, interactive=False) |
686419cd2dff
nicer explanation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4799
diff
changeset
|
227 |
# serialize all entity types, assuring CWEType is serialized first for proper |
686419cd2dff
nicer explanation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4799
diff
changeset
|
228 |
# is / is_instance_of insertion |
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
229 |
eschemas.remove(schema.eschema('CWEType')) |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
230 |
eschemas.insert(0, schema.eschema('CWEType')) |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
231 |
for eschema in eschemas: |
4789
c93770e4aaf4
remove stuff from default introduced accidentally in stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
232 |
for rql, kwargs in eschema2rql(eschema, groupmap): |
c93770e4aaf4
remove stuff from default introduced accidentally in stable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
233 |
execute(rql, kwargs, build_descr=False) |
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
234 |
if pb is not None: |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
235 |
pb.update() |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
236 |
# serialize constraint types |
2597
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
237 |
rql = 'INSERT CWConstraintType X: X name %(ct)s' |
0 | 238 |
for cstrtype in CONSTRAINTS: |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
239 |
execute(rql, {'ct': unicode(cstrtype)}, build_descr=False) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
240 |
if pb is not None: |
0 | 241 |
pb.update() |
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
242 |
# serialize relations |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
243 |
for rschema in schema.relations(): |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
244 |
# skip virtual relations such as eid, has_text and identity |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
245 |
if rschema in VIRTUAL_RTYPES: |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
246 |
if pb is not None: |
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
247 |
pb.update() |
0 | 248 |
continue |
4799
99e31d0ce391
fix schema serialization bug: groupmap considered as addrdef argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4791
diff
changeset
|
249 |
for rql, kwargs in rschema2rql(rschema, groupmap=groupmap): |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
250 |
execute(rql, kwargs, build_descr=False) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
251 |
if pb is not None: |
0 | 252 |
pb.update() |
253 |
for rql, kwargs in specialize2rql(schema): |
|
4791
b9a1b16a8d26
fix schema serialization, bad repartition of changes between stable and default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4789
diff
changeset
|
254 |
execute(rql, kwargs, build_descr=False) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
255 |
if pb is not None: |
0 | 256 |
pb.update() |
3723
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
257 |
if not quiet: |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
258 |
print |
0 | 259 |
|
260 |
||
261 |
def _ervalues(erschema): |
|
262 |
try: |
|
263 |
type_ = unicode(erschema.type) |
|
264 |
except UnicodeDecodeError, e: |
|
265 |
raise Exception("can't decode %s [was %s]" % (erschema.type, e)) |
|
266 |
try: |
|
267 |
desc = unicode(erschema.description) or u'' |
|
268 |
except UnicodeDecodeError, e: |
|
269 |
raise Exception("can't decode %s [was %s]" % (erschema.description, e)) |
|
270 |
return { |
|
271 |
'name': type_, |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2955
diff
changeset
|
272 |
'final': erschema.final, |
0 | 273 |
'description': desc, |
274 |
} |
|
275 |
||
276 |
def eschema_relations_values(eschema): |
|
277 |
values = _ervalues(eschema) |
|
278 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
279 |
return relations, values |
|
280 |
||
281 |
def rschema_relations_values(rschema): |
|
282 |
values = _ervalues(rschema) |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2955
diff
changeset
|
283 |
values['final'] = rschema.final |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4312
diff
changeset
|
284 |
values['symmetric'] = rschema.symmetric |
0 | 285 |
values['inlined'] = rschema.inlined |
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4631
diff
changeset
|
286 |
if isinstance(rschema.fulltext_container, str): |
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4631
diff
changeset
|
287 |
values['fulltext_container'] = unicode(rschema.fulltext_container) |
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4631
diff
changeset
|
288 |
else: |
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4631
diff
changeset
|
289 |
values['fulltext_container'] = rschema.fulltext_container |
0 | 290 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
291 |
return relations, values |
|
292 |
||
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
293 |
def _rdef_values(objtype, props): |
0 | 294 |
amap = {'order': 'ordernum'} |
295 |
values = {} |
|
4024
6a14cff373c3
more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
296 |
for prop, default in schemamod.RelationDefinitionSchema.rproperty_defs(objtype).iteritems(): |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
297 |
if prop in ('eid', 'constraints', 'uid', 'infered', 'permissions'): |
0 | 298 |
continue |
299 |
value = props.get(prop, default) |
|
300 |
if prop in ('indexed', 'fulltextindexed', 'internationalizable'): |
|
301 |
value = bool(value) |
|
302 |
elif prop == 'ordernum': |
|
303 |
value = int(value) |
|
304 |
elif isinstance(value, str): |
|
305 |
value = unicode(value) |
|
306 |
values[amap.get(prop, prop)] = value |
|
307 |
return values |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
308 |
|
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
309 |
def nfrdef_relations_values(objtype, props): |
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
310 |
values = _rdef_values(objtype, props) |
0 | 311 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
312 |
return relations, values |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
313 |
|
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
314 |
def frdef_relations_values(objtype, props): |
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
315 |
values = _rdef_values(objtype, props) |
0 | 316 |
default = values['default'] |
317 |
del values['default'] |
|
318 |
if default is not None: |
|
319 |
if default is False: |
|
320 |
default = u'' |
|
321 |
elif not isinstance(default, unicode): |
|
322 |
default = unicode(default) |
|
323 |
values['defaultval'] = default |
|
324 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
325 |
return relations, values |
|
326 |
||
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
327 |
|
4033
732eccbed54a
missing groupmap propagation to rschema2rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4024
diff
changeset
|
328 |
def __rdef2rql(genmap, rschema, subjtype=None, objtype=None, props=None, |
732eccbed54a
missing groupmap propagation to rschema2rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4024
diff
changeset
|
329 |
groupmap=None): |
0 | 330 |
if subjtype is None: |
331 |
assert objtype is None |
|
332 |
assert props is None |
|
4189
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
333 |
targets = sorted(rschema.rdefs) |
0 | 334 |
else: |
335 |
assert not objtype is None |
|
336 |
targets = [(subjtype, objtype)] |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
337 |
# relation schema |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
338 |
if rschema.final: |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
339 |
etype = 'CWAttribute' |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
340 |
else: |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
341 |
etype = 'CWRelation' |
0 | 342 |
for subjtype, objtype in targets: |
343 |
if props is None: |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
344 |
_props = rschema.rdef(subjtype, objtype) |
0 | 345 |
else: |
346 |
_props = props |
|
347 |
# don't serialize infered relations |
|
348 |
if _props.get('infered'): |
|
349 |
continue |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2955
diff
changeset
|
350 |
gen = genmap[rschema.final] |
0 | 351 |
for rql, values in gen(rschema, subjtype, objtype, _props): |
352 |
yield rql, values |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
353 |
# no groupmap means "no security insertion" |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
354 |
if groupmap: |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
355 |
for rql, args in _erperms2rql(_props, groupmap): |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
356 |
args['st'] = str(subjtype) |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
357 |
args['rt'] = str(rschema) |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
358 |
args['ot'] = str(objtype) |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
359 |
yield rql + 'X is %s, X from_entity ST, X to_entity OT, '\ |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
360 |
'X relation_type RT, RT name %%(rt)s, ST name %%(st)s, '\ |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
361 |
'OT name %%(ot)s' % etype, args |
0 | 362 |
|
363 |
||
364 |
def schema2rql(schema, skip=None, allow=None): |
|
365 |
"""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
|
366 |
database as CWRType and CWEType entities |
0 | 367 |
""" |
368 |
assert not (skip is not None and allow is not None), \ |
|
369 |
'can\'t use both skip and allow' |
|
370 |
all = schema.entities() + schema.relations() |
|
371 |
if skip is not None: |
|
372 |
return chain(*[erschema2rql(schema[t]) for t in all if not t in skip]) |
|
373 |
elif allow is not None: |
|
374 |
return chain(*[erschema2rql(schema[t]) for t in all if t in allow]) |
|
375 |
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
|
376 |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
377 |
def erschema2rql(erschema, groupmap): |
0 | 378 |
if isinstance(erschema, schemamod.EntitySchema): |
4033
732eccbed54a
missing groupmap propagation to rschema2rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4024
diff
changeset
|
379 |
return eschema2rql(erschema, groupmap=groupmap) |
732eccbed54a
missing groupmap propagation to rschema2rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4024
diff
changeset
|
380 |
return rschema2rql(erschema, groupmap=groupmap) |
0 | 381 |
|
4189
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
382 |
def eschema2rql(eschema, groupmap=None): |
0 | 383 |
"""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
|
384 |
in the database as an CWEType entity |
0 | 385 |
""" |
386 |
relations, values = eschema_relations_values(eschema) |
|
387 |
# NOTE: 'specializes' relation can't be inserted here since there's no |
|
388 |
# 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
|
389 |
yield 'INSERT CWEType X: %s' % ','.join(relations) , values |
4189
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
390 |
# entity permissions |
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
391 |
if groupmap is not None: |
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
392 |
for rql, args in _erperms2rql(eschema, groupmap): |
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
393 |
args['name'] = str(eschema) |
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
394 |
yield rql + 'X is CWEType, X name %(name)s', args |
0 | 395 |
|
396 |
def specialize2rql(schema): |
|
397 |
for eschema in schema.entities(): |
|
398 |
for rql, kwargs in eschemaspecialize2rql(eschema): |
|
399 |
yield rql, kwargs |
|
400 |
||
401 |
def eschemaspecialize2rql(eschema): |
|
402 |
specialized_type = eschema.specializes() |
|
403 |
if specialized_type: |
|
404 |
values = {'x': eschema.type, 'et': specialized_type.type} |
|
405 |
yield 'SET X specializes ET WHERE X name %(x)s, ET name %(et)s', values |
|
406 |
||
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
407 |
def rschema2rql(rschema, addrdef=True, groupmap=None): |
0 | 408 |
"""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
|
409 |
in the database as an CWRType entity |
0 | 410 |
""" |
411 |
if rschema.type == 'has_text': |
|
412 |
return |
|
413 |
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
|
414 |
yield 'INSERT CWRType X: %s' % ','.join(relations), values |
0 | 415 |
if addrdef: |
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
416 |
for rql, values in rdef2rql(rschema, groupmap=groupmap): |
0 | 417 |
yield rql, values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
418 |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
419 |
def rdef2rql(rschema, subjtype=None, objtype=None, props=None, groupmap=None): |
0 | 420 |
genmap = {True: frdef2rql, False: nfrdef2rql} |
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
421 |
return __rdef2rql(genmap, rschema, subjtype, objtype, props, groupmap) |
0 | 422 |
|
423 |
||
424 |
_LOCATE_RDEF_RQL0 = 'X relation_type ER,X from_entity SE,X to_entity OE' |
|
425 |
_LOCATE_RDEF_RQL1 = 'SE name %(se)s,ER name %(rt)s,OE name %(oe)s' |
|
426 |
||
427 |
def frdef2rql(rschema, subjtype, objtype, props): |
|
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
428 |
relations, values = frdef_relations_values(objtype, props) |
0 | 429 |
relations.append(_LOCATE_RDEF_RQL0) |
430 |
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
|
431 |
yield 'INSERT CWAttribute X: %s WHERE %s' % (','.join(relations), _LOCATE_RDEF_RQL1), values |
0 | 432 |
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
|
433 |
yield rql + ', EDEF is CWAttribute', values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
434 |
|
0 | 435 |
def nfrdef2rql(rschema, subjtype, objtype, props): |
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
436 |
relations, values = nfrdef_relations_values(objtype, props) |
0 | 437 |
relations.append(_LOCATE_RDEF_RQL0) |
438 |
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
|
439 |
yield 'INSERT CWRelation X: %s WHERE %s' % (','.join(relations), _LOCATE_RDEF_RQL1), values |
0 | 440 |
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
|
441 |
yield rql + ', EDEF is CWRelation', values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
442 |
|
0 | 443 |
def rdefrelations2rql(rschema, subjtype, objtype, props): |
444 |
iterators = [] |
|
4021
280c910c8710
move i18n / migration modules from cw.common to cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4014
diff
changeset
|
445 |
for constraint in props.constraints: |
0 | 446 |
iterators.append(constraint2rql(rschema, subjtype, objtype, constraint)) |
447 |
return chain(*iterators) |
|
448 |
||
449 |
def constraint2rql(rschema, subjtype, objtype, constraint): |
|
450 |
values = {'ctname': unicode(constraint.type()), |
|
451 |
'value': unicode(constraint.serialize()), |
|
452 |
'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
|
453 |
yield 'INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X WHERE \ |
0 | 454 |
CT name %(ctname)s, EDEF relation_type ER, EDEF from_entity SE, EDEF to_entity OE, \ |
455 |
ER name %(rt)s, SE name %(se)s, OE name %(oe)s', values |
|
456 |
||
457 |
||
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
458 |
def _erperms2rql(erschema, groupmap): |
0 | 459 |
"""return rql insert statements to enter the entity or relation |
460 |
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
|
461 |
[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
|
462 |
and CWGroup entities |
0 | 463 |
""" |
464 |
for action in erschema.ACTIONS: |
|
4631
457160bc8cfe
when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
465 |
try: |
457160bc8cfe
when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
466 |
grantedto = erschema.action_permissions(action) |
457160bc8cfe
when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
467 |
except KeyError: |
457160bc8cfe
when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
468 |
# may occurs when modifying persistent schema |
457160bc8cfe
when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
469 |
continue |
457160bc8cfe
when modifying a persistent schema, erschema may miss some permissions which wil trigger a key error, but we don't want to crash on such cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4570
diff
changeset
|
470 |
for group_or_rqlexpr in grantedto: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
471 |
if isinstance(group_or_rqlexpr, basestring): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
472 |
# group |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
473 |
try: |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
474 |
yield ('SET X %s_permission Y WHERE Y eid %%(g)s, ' % action, |
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
475 |
{'g': groupmap[group_or_rqlexpr]}) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
476 |
except KeyError: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
477 |
continue |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
478 |
else: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
479 |
# rqlexpr |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
480 |
rqlexpr = group_or_rqlexpr |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
481 |
yield ('INSERT RQLExpression E: E expression %%(e)s, E exprtype %%(t)s, ' |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
482 |
'E mainvars %%(v)s, X %s_permission E WHERE ' % action, |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
483 |
{'e': unicode(rqlexpr.expression), |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
484 |
'v': unicode(rqlexpr.mainvars), |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
485 |
't': unicode(rqlexpr.__class__.__name__)}) |
0 | 486 |
|
487 |
||
488 |
def updateeschema2rql(eschema): |
|
489 |
relations, values = eschema_relations_values(eschema) |
|
490 |
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
|
491 |
yield 'SET %s WHERE X is CWEType, X name %%(et)s' % ','.join(relations), values |
0 | 492 |
|
493 |
def updaterschema2rql(rschema): |
|
494 |
relations, values = rschema_relations_values(rschema) |
|
495 |
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
|
496 |
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
|
497 |
|
0 | 498 |
def updaterdef2rql(rschema, subjtype=None, objtype=None, props=None): |
499 |
genmap = {True: updatefrdef2rql, False: updatenfrdef2rql} |
|
500 |
return __rdef2rql(genmap, rschema, subjtype, objtype, props) |
|
501 |
||
502 |
def updatefrdef2rql(rschema, subjtype, objtype, props): |
|
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
503 |
relations, values = frdef_relations_values(objtype, props) |
0 | 504 |
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
|
505 |
yield 'SET %s WHERE %s, %s, X is CWAttribute' % (','.join(relations), |
2597
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
506 |
_LOCATE_RDEF_RQL0, |
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
507 |
_LOCATE_RDEF_RQL1), values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
508 |
|
0 | 509 |
def updatenfrdef2rql(rschema, subjtype, objtype, props): |
4722
9c13d5db03d9
pylint suggested refactorings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
510 |
relations, values = nfrdef_relations_values(objtype, props) |
0 | 511 |
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
|
512 |
yield 'SET %s WHERE %s, %s, X is CWRelation' % (','.join(relations), |
2597
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
513 |
_LOCATE_RDEF_RQL0, |
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
514 |
_LOCATE_RDEF_RQL1), values |