author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 26 Feb 2010 13:24:47 +0100 | |
branch | stable |
changeset 4721 | 8f63691ccb7f |
parent 4719 | aaed3f813ef8 |
child 4722 | 9c13d5db03d9 |
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 ############################################## |
|
2044
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
54 |
OLD_SCHEMA_TYPES = frozenset(('EFRDef', 'ENFRDef', 'ERType', 'EEType', |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
55 |
'EConstraintType', 'EConstraint', 'EGroup', |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
56 |
'EUser', 'ECache', 'EPermission', 'EProperty')) |
0 | 57 |
|
58 |
def deserialize_schema(schema, session): |
|
59 |
"""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
|
60 |
as CWRType and CWEType entities |
0 | 61 |
""" |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
62 |
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
|
63 |
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
|
64 |
# 3.6 migration |
4475
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
sidx = {} |
0 | 73 |
permsdict = deserialize_ertype_permissions(session) |
74 |
schema.reading_from_database = True |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
75 |
for eid, etype, desc in session.execute( |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
76 |
'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
|
77 |
build_descr=False): |
0 | 78 |
# base types are already in the schema, skip them |
79 |
if etype in schemamod.BASE_TYPES: |
|
80 |
# just set the eid |
|
81 |
eschema = schema.eschema(etype) |
|
82 |
eschema.eid = eid |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
83 |
sidx[eid] = eschema |
0 | 84 |
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
|
85 |
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
|
86 |
netype = ETYPE_NAME_MAP[etype] |
0 | 87 |
# 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
|
88 |
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
|
89 |
% {'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
|
90 |
{'x': eid, 'n': netype}) |
0 | 91 |
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
|
92 |
{'x': etype, 'n': netype}) |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
93 |
# XXX should be donne as well on sqlite based sources |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
94 |
if not etype in OLD_SCHEMA_TYPES and \ |
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2044
diff
changeset
|
95 |
(getattr(dbhelper, 'case_sensitive', 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
|
96 |
or etype.lower() != netype.lower()): |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
97 |
session.system_sql('ALTER TABLE %s%s RENAME TO %s%s' % ( |
d5589c5bc4e6
alter table in schema serial when a renamed type is detected
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
98 |
sqlutils.SQL_PREFIX, etype, sqlutils.SQL_PREFIX, netype)) |
0 | 99 |
session.commit(False) |
100 |
try: |
|
101 |
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
|
102 |
{'x': etype, 'n': netype}) |
0 | 103 |
except: |
104 |
pass |
|
105 |
tocleanup = [eid] |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
106 |
tocleanup += (eid for eid, (eidetype, uri, extid) in repo._type_source_cache.items() |
0 | 107 |
if etype == eidetype) |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
108 |
repo.clear_caches(tocleanup) |
0 | 109 |
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
|
110 |
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
|
111 |
etype = ybo.EntityType(name=etype, description=desc, eid=eid) |
0 | 112 |
eschema = schema.add_entity_type(etype) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
113 |
sidx[eid] = eschema |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
114 |
set_perms(eschema, permsdict) |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
115 |
for etype, stype in session.execute( |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
116 |
'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
|
117 |
build_descr=False): |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
118 |
schema.eschema(etype)._specialized_type = stype |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
119 |
schema.eschema(stype)._specialized_by.append(etype) |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
120 |
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
|
121 |
'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
|
122 |
'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
|
123 |
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
|
124 |
symmetric=bool(sym), inlined=bool(il), |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
125 |
fulltext_container=ftc, eid=eid) |
0 | 126 |
rschema = schema.add_relation_type(rtype) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
127 |
sidx[eid] = rschema |
0 | 128 |
cstrsdict = deserialize_rdef_constraints(session) |
129 |
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
|
130 |
'Any X,SE,RT,OE,CARD,ORD,DESC,IDX,FTIDX,I18N,DFLT WHERE X is CWAttribute,' |
0 | 131 |
'X relation_type RT, X cardinality CARD, X ordernum ORD, X indexed IDX,' |
132 |
'X description DESC, X internationalizable I18N, X defaultval DFLT,' |
|
133 |
'X fulltextindexed FTIDX, X from_entity SE, X to_entity OE', |
|
134 |
build_descr=False): |
|
135 |
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
|
136 |
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
|
137 |
cardinality=card, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
138 |
constraints=cstrsdict.get(rdefeid, ()), |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
139 |
order=ord, description=desc, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
140 |
indexed=idx, fulltextindexed=ftidx, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
141 |
internationalizable=i18n, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
142 |
default=default, eid=rdefeid) |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
143 |
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
|
144 |
# 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
|
145 |
if rdefs is not None: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
146 |
set_perms(rdefs, permsdict) |
0 | 147 |
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
|
148 |
'Any X,SE,RT,OE,CARD,ORD,DESC,C WHERE X is CWRelation, X relation_type RT,' |
0 | 149 |
'X cardinality CARD, X ordernum ORD, X description DESC, ' |
150 |
'X from_entity SE, X to_entity OE, X composite C', build_descr=False): |
|
151 |
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
|
152 |
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
|
153 |
constraints=cstrsdict.get(rdefeid, ()), |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
154 |
cardinality=card, order=ord, description=desc, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
155 |
composite=c, eid=rdefeid) |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
156 |
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
|
157 |
# 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
|
158 |
if rdefs is not None: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
159 |
set_perms(rdefs, permsdict) |
0 | 160 |
schema.infer_specialization_rules() |
161 |
session.commit() |
|
162 |
schema.reading_from_database = False |
|
163 |
||
164 |
||
165 |
def deserialize_ertype_permissions(session): |
|
166 |
"""return sect action:groups associations for the given |
|
167 |
entity or relation schema with its eid, according to schema's |
|
168 |
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
|
169 |
relations between CWEType/CWRType and CWGroup entities |
0 | 170 |
""" |
171 |
res = {} |
|
172 |
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
|
173 |
rql = 'Any E,N WHERE G is CWGroup, G name N, E %s_permission G' % action |
0 | 174 |
for eid, gname in session.execute(rql, build_descr=False): |
175 |
res.setdefault(eid, {}).setdefault(action, []).append(gname) |
|
176 |
rql = ('Any E,X,EXPR,V WHERE X is RQLExpression, X expression EXPR, ' |
|
177 |
'E %s_permission X, X mainvars V' % action) |
|
178 |
for eid, expreid, expr, mainvars in session.execute(rql, build_descr=False): |
|
179 |
# we don't know yet if it's a rql expr for an entity or a relation, |
|
180 |
# so append a tuple to differentiate from groups and so we'll be |
|
181 |
# able to instantiate it later |
|
182 |
res.setdefault(eid, {}).setdefault(action, []).append( (expr, mainvars, expreid) ) |
|
183 |
return res |
|
184 |
||
185 |
def set_perms(erschema, permsdict): |
|
186 |
"""set permissions on the given erschema according to the permission |
|
187 |
definition dictionary as built by deserialize_ertype_permissions for a |
|
188 |
given erschema's eid |
|
189 |
""" |
|
4570
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
190 |
# 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
|
191 |
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
|
192 |
try: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
193 |
thispermsdict = permsdict[erschema.eid] |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
194 |
except KeyError: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
195 |
return |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
196 |
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
|
197 |
# 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
|
198 |
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
|
199 |
action = 'update' |
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
200 |
erschema.permissions[action] = tuple( |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
201 |
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
|
202 |
for p in somethings) |
0 | 203 |
|
204 |
||
205 |
def deserialize_rdef_constraints(session): |
|
206 |
"""return the list of relation definition's constraints as instances""" |
|
207 |
res = {} |
|
208 |
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
|
209 |
'Any E, X,TN,V WHERE E constrained_by X, X is CWConstraint, ' |
0 | 210 |
'X cstrtype T, T name TN, X value V', build_descr=False): |
211 |
cstr = CONSTRAINTS[ct].deserialize(val) |
|
212 |
cstr.eid = ceid |
|
213 |
res.setdefault(rdefeid, []).append(cstr) |
|
214 |
return res |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
215 |
|
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
216 |
|
0 | 217 |
# schema / perms serialization ################################################ |
218 |
||
219 |
def serialize_schema(cursor, schema, verbose=False): |
|
220 |
"""synchronize schema and permissions in the database according to |
|
221 |
current schema |
|
222 |
""" |
|
3737 | 223 |
quiet = os.environ.get('APYCOT_ROOT') |
3723
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
224 |
if not quiet: |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
225 |
_title = '-> storing the schema in the database ' |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
226 |
print _title, |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
227 |
execute = cursor.execute |
0 | 228 |
eschemas = schema.entities() |
229 |
aller = eschemas + schema.relations() |
|
3723
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
230 |
if not verbose and not quiet: |
0 | 231 |
pb_size = len(aller) + len(CONSTRAINTS) + 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
|
232 |
pb = ProgressBar(pb_size, title=_title) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
233 |
else: |
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
234 |
pb = None |
2597
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
235 |
rql = 'INSERT CWConstraintType X: X name %(ct)s' |
0 | 236 |
for cstrtype in CONSTRAINTS: |
237 |
if verbose: |
|
238 |
print rql |
|
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() |
242 |
groupmap = group_mapping(cursor, interactive=False) |
|
243 |
for ertype in aller: |
|
244 |
# skip eid and has_text relations |
|
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
245 |
if ertype 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 |
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
249 |
for rql, kwargs in erschema2rql(schema[ertype], groupmap): |
0 | 250 |
if verbose: |
251 |
print rql % kwargs |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
252 |
execute(rql, kwargs, build_descr=False) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
253 |
if pb is not None: |
0 | 254 |
pb.update() |
255 |
for rql, kwargs in specialize2rql(schema): |
|
256 |
if verbose: |
|
257 |
print rql % kwargs |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
258 |
execute(rql, kwargs, build_descr=False) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
259 |
if pb is not None: |
0 | 260 |
pb.update() |
3723
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
261 |
if not quiet: |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
262 |
print |
0 | 263 |
|
264 |
||
265 |
def _ervalues(erschema): |
|
266 |
try: |
|
267 |
type_ = unicode(erschema.type) |
|
268 |
except UnicodeDecodeError, e: |
|
269 |
raise Exception("can't decode %s [was %s]" % (erschema.type, e)) |
|
270 |
try: |
|
271 |
desc = unicode(erschema.description) or u'' |
|
272 |
except UnicodeDecodeError, e: |
|
273 |
raise Exception("can't decode %s [was %s]" % (erschema.description, e)) |
|
274 |
return { |
|
275 |
'name': type_, |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2955
diff
changeset
|
276 |
'final': erschema.final, |
0 | 277 |
'description': desc, |
278 |
} |
|
279 |
||
280 |
def eschema_relations_values(eschema): |
|
281 |
values = _ervalues(eschema) |
|
282 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
283 |
return relations, values |
|
284 |
||
285 |
def rschema_relations_values(rschema): |
|
286 |
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
|
287 |
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
|
288 |
values['symmetric'] = rschema.symmetric |
0 | 289 |
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
|
290 |
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
|
291 |
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
|
292 |
else: |
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4631
diff
changeset
|
293 |
values['fulltext_container'] = rschema.fulltext_container |
0 | 294 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
295 |
return relations, values |
|
296 |
||
297 |
def _rdef_values(rschema, objtype, props): |
|
298 |
amap = {'order': 'ordernum'} |
|
299 |
values = {} |
|
4024
6a14cff373c3
more api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
300 |
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
|
301 |
if prop in ('eid', 'constraints', 'uid', 'infered', 'permissions'): |
0 | 302 |
continue |
303 |
value = props.get(prop, default) |
|
304 |
if prop in ('indexed', 'fulltextindexed', 'internationalizable'): |
|
305 |
value = bool(value) |
|
306 |
elif prop == 'ordernum': |
|
307 |
value = int(value) |
|
308 |
elif isinstance(value, str): |
|
309 |
value = unicode(value) |
|
310 |
values[amap.get(prop, prop)] = value |
|
311 |
return values |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
312 |
|
0 | 313 |
def nfrdef_relations_values(rschema, objtype, props): |
314 |
values = _rdef_values(rschema, objtype, props) |
|
315 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
316 |
return relations, values |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
317 |
|
0 | 318 |
def frdef_relations_values(rschema, objtype, props): |
319 |
values = _rdef_values(rschema, objtype, props) |
|
320 |
default = values['default'] |
|
321 |
del values['default'] |
|
322 |
if default is not None: |
|
323 |
if default is False: |
|
324 |
default = u'' |
|
325 |
elif not isinstance(default, unicode): |
|
326 |
default = unicode(default) |
|
327 |
values['defaultval'] = default |
|
328 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
329 |
return relations, values |
|
330 |
||
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
331 |
|
4033
732eccbed54a
missing groupmap propagation to rschema2rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4024
diff
changeset
|
332 |
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
|
333 |
groupmap=None): |
0 | 334 |
if subjtype is None: |
335 |
assert objtype is None |
|
336 |
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
|
337 |
targets = sorted(rschema.rdefs) |
0 | 338 |
else: |
339 |
assert not objtype is None |
|
340 |
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
|
341 |
# relation schema |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
342 |
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
|
343 |
etype = 'CWAttribute' |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
344 |
else: |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
345 |
etype = 'CWRelation' |
0 | 346 |
for subjtype, objtype in targets: |
347 |
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
|
348 |
_props = rschema.rdef(subjtype, objtype) |
0 | 349 |
else: |
350 |
_props = props |
|
351 |
# don't serialize infered relations |
|
352 |
if _props.get('infered'): |
|
353 |
continue |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2955
diff
changeset
|
354 |
gen = genmap[rschema.final] |
0 | 355 |
for rql, values in gen(rschema, subjtype, objtype, _props): |
356 |
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
|
357 |
# 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
|
358 |
if groupmap: |
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
359 |
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
|
360 |
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
|
361 |
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
|
362 |
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
|
363 |
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
|
364 |
'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
|
365 |
'OT name %%(ot)s' % etype, args |
0 | 366 |
|
367 |
||
368 |
def schema2rql(schema, skip=None, allow=None): |
|
369 |
"""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
|
370 |
database as CWRType and CWEType entities |
0 | 371 |
""" |
372 |
assert not (skip is not None and allow is not None), \ |
|
373 |
'can\'t use both skip and allow' |
|
374 |
all = schema.entities() + schema.relations() |
|
375 |
if skip is not None: |
|
376 |
return chain(*[erschema2rql(schema[t]) for t in all if not t in skip]) |
|
377 |
elif allow is not None: |
|
378 |
return chain(*[erschema2rql(schema[t]) for t in all if t in allow]) |
|
379 |
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
|
380 |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
381 |
def erschema2rql(erschema, groupmap): |
0 | 382 |
if isinstance(erschema, schemamod.EntitySchema): |
4033
732eccbed54a
missing groupmap propagation to rschema2rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4024
diff
changeset
|
383 |
return eschema2rql(erschema, groupmap=groupmap) |
732eccbed54a
missing groupmap propagation to rschema2rql
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4024
diff
changeset
|
384 |
return rschema2rql(erschema, groupmap=groupmap) |
0 | 385 |
|
4189
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
386 |
def eschema2rql(eschema, groupmap=None): |
0 | 387 |
"""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
|
388 |
in the database as an CWEType entity |
0 | 389 |
""" |
390 |
relations, values = eschema_relations_values(eschema) |
|
391 |
# NOTE: 'specializes' relation can't be inserted here since there's no |
|
392 |
# 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
|
393 |
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
|
394 |
# entity permissions |
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
395 |
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
|
396 |
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
|
397 |
args['name'] = str(eschema) |
bd552b341334
make groupmap argument optional / sort for testing purpose
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4033
diff
changeset
|
398 |
yield rql + 'X is CWEType, X name %(name)s', args |
0 | 399 |
|
400 |
def specialize2rql(schema): |
|
401 |
for eschema in schema.entities(): |
|
402 |
for rql, kwargs in eschemaspecialize2rql(eschema): |
|
403 |
yield rql, kwargs |
|
404 |
||
405 |
def eschemaspecialize2rql(eschema): |
|
406 |
specialized_type = eschema.specializes() |
|
407 |
if specialized_type: |
|
408 |
values = {'x': eschema.type, 'et': specialized_type.type} |
|
409 |
yield 'SET X specializes ET WHERE X name %(x)s, ET name %(et)s', values |
|
410 |
||
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
411 |
def rschema2rql(rschema, addrdef=True, groupmap=None): |
0 | 412 |
"""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
|
413 |
in the database as an CWRType entity |
0 | 414 |
""" |
415 |
if rschema.type == 'has_text': |
|
416 |
return |
|
417 |
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
|
418 |
yield 'INSERT CWRType X: %s' % ','.join(relations), values |
0 | 419 |
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
|
420 |
for rql, values in rdef2rql(rschema, groupmap=groupmap): |
0 | 421 |
yield rql, values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
422 |
|
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
423 |
def rdef2rql(rschema, subjtype=None, objtype=None, props=None, groupmap=None): |
0 | 424 |
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
|
425 |
return __rdef2rql(genmap, rschema, subjtype, objtype, props, groupmap) |
0 | 426 |
|
427 |
||
428 |
_LOCATE_RDEF_RQL0 = 'X relation_type ER,X from_entity SE,X to_entity OE' |
|
429 |
_LOCATE_RDEF_RQL1 = 'SE name %(se)s,ER name %(rt)s,OE name %(oe)s' |
|
430 |
||
431 |
def frdef2rql(rschema, subjtype, objtype, props): |
|
432 |
relations, values = frdef_relations_values(rschema, objtype, props) |
|
433 |
relations.append(_LOCATE_RDEF_RQL0) |
|
434 |
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
|
435 |
yield 'INSERT CWAttribute X: %s WHERE %s' % (','.join(relations), _LOCATE_RDEF_RQL1), values |
0 | 436 |
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
|
437 |
yield rql + ', EDEF is CWAttribute', values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
438 |
|
0 | 439 |
def nfrdef2rql(rschema, subjtype, objtype, props): |
440 |
relations, values = nfrdef_relations_values(rschema, objtype, props) |
|
441 |
relations.append(_LOCATE_RDEF_RQL0) |
|
442 |
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
|
443 |
yield 'INSERT CWRelation X: %s WHERE %s' % (','.join(relations), _LOCATE_RDEF_RQL1), values |
0 | 444 |
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
|
445 |
yield rql + ', EDEF is CWRelation', values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
446 |
|
0 | 447 |
def rdefrelations2rql(rschema, subjtype, objtype, props): |
448 |
iterators = [] |
|
4021
280c910c8710
move i18n / migration modules from cw.common to cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4014
diff
changeset
|
449 |
for constraint in props.constraints: |
0 | 450 |
iterators.append(constraint2rql(rschema, subjtype, objtype, constraint)) |
451 |
return chain(*iterators) |
|
452 |
||
453 |
def constraint2rql(rschema, subjtype, objtype, constraint): |
|
454 |
values = {'ctname': unicode(constraint.type()), |
|
455 |
'value': unicode(constraint.serialize()), |
|
456 |
'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
|
457 |
yield 'INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X WHERE \ |
0 | 458 |
CT name %(ctname)s, EDEF relation_type ER, EDEF from_entity SE, EDEF to_entity OE, \ |
459 |
ER name %(rt)s, SE name %(se)s, OE name %(oe)s', values |
|
460 |
||
461 |
||
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
462 |
def _erperms2rql(erschema, groupmap): |
0 | 463 |
"""return rql insert statements to enter the entity or relation |
464 |
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
|
465 |
[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
|
466 |
and CWGroup entities |
0 | 467 |
""" |
468 |
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
|
469 |
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
|
470 |
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
|
471 |
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
|
472 |
# 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
|
473 |
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
|
474 |
for group_or_rqlexpr in grantedto: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
475 |
if isinstance(group_or_rqlexpr, basestring): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
476 |
# group |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
477 |
try: |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
478 |
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
|
479 |
{'g': groupmap[group_or_rqlexpr]}) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
480 |
except KeyError: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
481 |
continue |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
482 |
else: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
483 |
# rqlexpr |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
484 |
rqlexpr = group_or_rqlexpr |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
485 |
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
|
486 |
'E mainvars %%(v)s, X %s_permission E WHERE ' % action, |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
487 |
{'e': unicode(rqlexpr.expression), |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
488 |
'v': unicode(rqlexpr.mainvars), |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
489 |
't': unicode(rqlexpr.__class__.__name__)}) |
0 | 490 |
|
491 |
||
492 |
def updateeschema2rql(eschema): |
|
493 |
relations, values = eschema_relations_values(eschema) |
|
494 |
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
|
495 |
yield 'SET %s WHERE X is CWEType, X name %%(et)s' % ','.join(relations), values |
0 | 496 |
|
497 |
def updaterschema2rql(rschema): |
|
498 |
relations, values = rschema_relations_values(rschema) |
|
499 |
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
|
500 |
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
|
501 |
|
0 | 502 |
def updaterdef2rql(rschema, subjtype=None, objtype=None, props=None): |
503 |
genmap = {True: updatefrdef2rql, False: updatenfrdef2rql} |
|
504 |
return __rdef2rql(genmap, rschema, subjtype, objtype, props) |
|
505 |
||
506 |
def updatefrdef2rql(rschema, subjtype, objtype, props): |
|
507 |
relations, values = frdef_relations_values(rschema, objtype, props) |
|
508 |
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
|
509 |
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
|
510 |
_LOCATE_RDEF_RQL0, |
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
511 |
_LOCATE_RDEF_RQL1), values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
512 |
|
0 | 513 |
def updatenfrdef2rql(rschema, subjtype, objtype, props): |
514 |
relations, values = nfrdef_relations_values(rschema, objtype, props) |
|
515 |
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
|
516 |
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
|
517 |
_LOCATE_RDEF_RQL0, |
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
518 |
_LOCATE_RDEF_RQL1), values |