author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 28 Apr 2010 12:14:22 +0200 | |
changeset 5423 | e15abfdcce38 |
parent 5379 | c082dea0731b |
parent 5421 | 8167de96c523 |
child 5426 | 0d4853a6e5ee |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
10 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
11 |
# logilab-common is distributed in the hope that it will be useful, but WITHOUT |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""functions for schema / permissions (de)serialization using RQL |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
3854
8633cd05b6b5
no progress bar in apycot environment
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
23 |
import os |
0 | 24 |
from itertools import chain |
25 |
||
26 |
from logilab.common.shellutils import ProgressBar |
|
27 |
||
28 |
from yams import schema as schemamod, buildobjs as ybo |
|
29 |
||
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
30 |
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
|
31 |
from cubicweb.server import sqlutils |
0 | 32 |
|
33 |
def group_mapping(cursor, interactive=True): |
|
34 |
"""create a group mapping from an rql cursor |
|
35 |
||
36 |
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
|
37 |
and the actual CWGroup entity's eid as associated value. |
0 | 38 |
In interactive mode (the default), missing groups'eid will be prompted |
39 |
from the user. |
|
40 |
""" |
|
41 |
res = {} |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
42 |
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
|
43 |
build_descr=False): |
0 | 44 |
res[name] = eid |
45 |
if not interactive: |
|
46 |
return res |
|
47 |
missing = [g for g in ('owners', 'managers', 'users', 'guests') if not g in res] |
|
48 |
if missing: |
|
49 |
print 'some native groups are missing but the following groups have been found:' |
|
50 |
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
|
51 |
print |
0 | 52 |
print 'enter the eid of a to group to map to each missing native group' |
53 |
print 'or just type enter to skip permissions granted to a group' |
|
54 |
for group in missing: |
|
55 |
while True: |
|
56 |
value = raw_input('eid for group %s: ' % group).strip() |
|
57 |
if not value: |
|
58 |
continue |
|
59 |
try: |
|
60 |
res[group] = int(value) |
|
61 |
except ValueError: |
|
62 |
print 'eid should be an integer' |
|
63 |
continue |
|
64 |
return res |
|
65 |
||
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
66 |
def cstrtype_mapping(cursor): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
67 |
"""cached constraint types mapping""" |
5379
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
68 |
map = dict(cursor.execute('Any T, X WHERE X is CWConstraintType, X name T')) |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
69 |
if not 'BoundConstraint' in map: |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
70 |
map['BoundConstraint'] = map['BoundaryConstraint'] |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
71 |
return map |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
72 |
|
0 | 73 |
# 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
|
74 |
|
0 | 75 |
def deserialize_schema(schema, session): |
76 |
"""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
|
77 |
as CWRType and CWEType entities |
0 | 78 |
""" |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
79 |
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
|
80 |
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
|
81 |
# 3.6 migration |
4475
37c413a07216
kill most pre 3.2 bw compat code
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4467
diff
changeset
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
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
|
89 |
sidx = {} |
0 | 90 |
permsdict = deserialize_ertype_permissions(session) |
91 |
schema.reading_from_database = True |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
92 |
for eid, etype, desc in session.execute( |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
93 |
'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
|
94 |
build_descr=False): |
0 | 95 |
# base types are already in the schema, skip them |
96 |
if etype in schemamod.BASE_TYPES: |
|
97 |
# just set the eid |
|
98 |
eschema = schema.eschema(etype) |
|
99 |
eschema.eid = eid |
|
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
100 |
sidx[eid] = eschema |
0 | 101 |
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
|
102 |
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
|
103 |
netype = ETYPE_NAME_MAP[etype] |
0 | 104 |
# 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
|
105 |
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
|
106 |
% {'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
|
107 |
{'x': eid, 'n': netype}) |
0 | 108 |
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
|
109 |
{'x': etype, 'n': netype}) |
0 | 110 |
session.commit(False) |
111 |
try: |
|
112 |
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
|
113 |
{'x': etype, 'n': netype}) |
0 | 114 |
except: |
115 |
pass |
|
116 |
tocleanup = [eid] |
|
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
117 |
tocleanup += (eid for eid, (eidetype, uri, extid) in repo._type_source_cache.items() |
0 | 118 |
if etype == eidetype) |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
119 |
repo.clear_caches(tocleanup) |
0 | 120 |
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
|
121 |
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
|
122 |
etype = ybo.EntityType(name=etype, description=desc, eid=eid) |
0 | 123 |
eschema = schema.add_entity_type(etype) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
124 |
sidx[eid] = eschema |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
125 |
set_perms(eschema, permsdict) |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
126 |
for etype, stype in session.execute( |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
127 |
'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
|
128 |
build_descr=False): |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
129 |
schema.eschema(etype)._specialized_type = stype |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
130 |
schema.eschema(stype)._specialized_by.append(etype) |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
131 |
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
|
132 |
'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
|
133 |
'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
|
134 |
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
|
135 |
symmetric=bool(sym), inlined=bool(il), |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
136 |
fulltext_container=ftc, eid=eid) |
0 | 137 |
rschema = schema.add_relation_type(rtype) |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
138 |
sidx[eid] = rschema |
0 | 139 |
cstrsdict = deserialize_rdef_constraints(session) |
140 |
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
|
141 |
'Any X,SE,RT,OE,CARD,ORD,DESC,IDX,FTIDX,I18N,DFLT WHERE X is CWAttribute,' |
0 | 142 |
'X relation_type RT, X cardinality CARD, X ordernum ORD, X indexed IDX,' |
143 |
'X description DESC, X internationalizable I18N, X defaultval DFLT,' |
|
144 |
'X fulltextindexed FTIDX, X from_entity SE, X to_entity OE', |
|
145 |
build_descr=False): |
|
146 |
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
|
147 |
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
|
148 |
cardinality=card, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
149 |
constraints=cstrsdict.get(rdefeid, ()), |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
150 |
order=ord, description=desc, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
151 |
indexed=idx, fulltextindexed=ftidx, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
152 |
internationalizable=i18n, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
153 |
default=default, eid=rdefeid) |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
154 |
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
|
155 |
# 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
|
156 |
if rdefs is not None: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
157 |
set_perms(rdefs, permsdict) |
0 | 158 |
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
|
159 |
'Any X,SE,RT,OE,CARD,ORD,DESC,C WHERE X is CWRelation, X relation_type RT,' |
0 | 160 |
'X cardinality CARD, X ordernum ORD, X description DESC, ' |
161 |
'X from_entity SE, X to_entity OE, X composite C', build_descr=False): |
|
162 |
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
|
163 |
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
|
164 |
constraints=cstrsdict.get(rdefeid, ()), |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
165 |
cardinality=card, order=ord, description=desc, |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
166 |
composite=c, eid=rdefeid) |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
167 |
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
|
168 |
# 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
|
169 |
if rdefs is not None: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
170 |
set_perms(rdefs, permsdict) |
0 | 171 |
schema.infer_specialization_rules() |
172 |
session.commit() |
|
173 |
schema.reading_from_database = False |
|
174 |
||
175 |
||
176 |
def deserialize_ertype_permissions(session): |
|
177 |
"""return sect action:groups associations for the given |
|
178 |
entity or relation schema with its eid, according to schema's |
|
179 |
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
|
180 |
relations between CWEType/CWRType and CWGroup entities |
0 | 181 |
""" |
182 |
res = {} |
|
183 |
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
|
184 |
rql = 'Any E,N WHERE G is CWGroup, G name N, E %s_permission G' % action |
0 | 185 |
for eid, gname in session.execute(rql, build_descr=False): |
186 |
res.setdefault(eid, {}).setdefault(action, []).append(gname) |
|
187 |
rql = ('Any E,X,EXPR,V WHERE X is RQLExpression, X expression EXPR, ' |
|
188 |
'E %s_permission X, X mainvars V' % action) |
|
189 |
for eid, expreid, expr, mainvars in session.execute(rql, build_descr=False): |
|
190 |
# we don't know yet if it's a rql expr for an entity or a relation, |
|
191 |
# so append a tuple to differentiate from groups and so we'll be |
|
192 |
# able to instantiate it later |
|
193 |
res.setdefault(eid, {}).setdefault(action, []).append( (expr, mainvars, expreid) ) |
|
194 |
return res |
|
195 |
||
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
196 |
def deserialize_rdef_constraints(session): |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
197 |
"""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
|
198 |
res = {} |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
199 |
for rdefeid, ceid, ct, val in session.execute( |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
200 |
'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
|
201 |
'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
|
202 |
cstr = CONSTRAINTS[ct].deserialize(val) |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
203 |
cstr.eid = ceid |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
204 |
res.setdefault(rdefeid, []).append(cstr) |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
205 |
return res |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
206 |
|
0 | 207 |
def set_perms(erschema, permsdict): |
208 |
"""set permissions on the given erschema according to the permission |
|
209 |
definition dictionary as built by deserialize_ertype_permissions for a |
|
210 |
given erschema's eid |
|
211 |
""" |
|
4570
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
212 |
# 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
|
213 |
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
|
214 |
try: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
215 |
thispermsdict = permsdict[erschema.eid] |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
216 |
except KeyError: |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
217 |
return |
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
218 |
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
|
219 |
# 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
|
220 |
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
|
221 |
action = 'update' |
ede247bbbf62
follow yams api change: attributes permissions are now defined for
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4475
diff
changeset
|
222 |
erschema.permissions[action] = tuple( |
4312
740397c24ec3
minor schema deserialization optimizations
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
223 |
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
|
224 |
for p in somethings) |
0 | 225 |
|
226 |
||
227 |
# schema / perms serialization ################################################ |
|
228 |
||
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
229 |
def serialize_schema(cursor, schema): |
0 | 230 |
"""synchronize schema and permissions in the database according to |
231 |
current schema |
|
232 |
""" |
|
3737 | 233 |
quiet = os.environ.get('APYCOT_ROOT') |
3723
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
234 |
if not quiet: |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
235 |
_title = '-> storing the schema in the database ' |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
236 |
print _title, |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4808
diff
changeset
|
237 |
execute = cursor.execute |
0 | 238 |
eschemas = schema.entities() |
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
239 |
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
|
240 |
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
|
241 |
+ 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
|
242 |
+ 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
|
243 |
pb = ProgressBar(pb_size, title=_title) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
244 |
else: |
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
245 |
pb = None |
4800
686419cd2dff
nicer explanation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4799
diff
changeset
|
246 |
groupmap = group_mapping(cursor, interactive=False) |
686419cd2dff
nicer explanation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4799
diff
changeset
|
247 |
# 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
|
248 |
# is / is_instance_of insertion |
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
249 |
eschemas.remove(schema.eschema('CWEType')) |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
250 |
eschemas.insert(0, schema.eschema('CWEType')) |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
251 |
for eschema in eschemas: |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
252 |
execschemarql(execute, eschema, eschema2rql(eschema, groupmap)) |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
253 |
if pb is not None: |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
254 |
pb.update() |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
255 |
# serialize constraint types |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
256 |
cstrtypemap = {} |
2597
d9c5a7e0563c
[C schema seria] cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
257 |
rql = 'INSERT CWConstraintType X: X name %(ct)s' |
0 | 258 |
for cstrtype in CONSTRAINTS: |
5379
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
259 |
if cstrtype == 'BoundConstraint': |
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
260 |
continue # XXX deprecated in yams 0.29 / cw 3.8.1 |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
261 |
cstrtypemap[cstrtype] = execute(rql, {'ct': unicode(cstrtype)}, |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
262 |
build_descr=False)[0][0] |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
263 |
if pb is not None: |
0 | 264 |
pb.update() |
5379
c082dea0731b
[schema] update for yams 0.29: BoundConstraint renamed to BoundaryConstraint; new constraint messages; ObjectRelation deprecated
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
265 |
cstrtypemap['BoundConstraint'] = cstrtypemap['BoundaryConstraint'] |
4760
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
266 |
# serialize relations |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
267 |
for rschema in schema.relations(): |
fe0e307b9b70
minor schema serialization optimization:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4722
diff
changeset
|
268 |
# 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
|
269 |
if rschema in VIRTUAL_RTYPES: |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
270 |
if pb is not None: |
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
271 |
pb.update() |
0 | 272 |
continue |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
273 |
execschemarql(execute, rschema, rschema2rql(rschema, addrdef=False)) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
274 |
if rschema.symmetric: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
275 |
rdefs = [rdef for k, rdef in rschema.rdefs.iteritems() |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
276 |
if (rdef.subject, rdef.object) == k] |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
277 |
else: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
278 |
rdefs = rschema.rdefs.itervalues() |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
279 |
for rdef in rdefs: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
280 |
execschemarql(execute, rdef, |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
281 |
rdef2rql(rdef, cstrtypemap, groupmap)) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
282 |
if pb is not None: |
0 | 283 |
pb.update() |
284 |
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
|
285 |
execute(rql, kwargs, build_descr=False) |
3857
6676a839dc97
ensure pb is not None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3854
diff
changeset
|
286 |
if pb is not None: |
0 | 287 |
pb.update() |
3723
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
288 |
if not quiet: |
a961c7cb2644
be quiet w/ apycot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
289 |
print |
0 | 290 |
|
291 |
||
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
292 |
# high level serialization functions |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
293 |
|
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
294 |
def execschemarql(execute, schema, rqls): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
295 |
for rql, kwargs in rqls: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
296 |
kwargs['x'] = schema.eid |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
297 |
rset = execute(rql, kwargs, build_descr=False) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
298 |
if schema.eid is None: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
299 |
schema.eid = rset[0][0] |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
300 |
else: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
301 |
assert rset |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
302 |
|
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
303 |
def erschema2rql(erschema, groupmap): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
304 |
if isinstance(erschema, schemamod.EntitySchema): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
305 |
return eschema2rql(erschema, groupmap=groupmap) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
306 |
return rschema2rql(erschema, groupmap=groupmap) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
307 |
|
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
308 |
def specialize2rql(schema): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
309 |
for eschema in schema.entities(): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
310 |
if eschema.final: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
311 |
continue |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
312 |
for rql, kwargs in eschemaspecialize2rql(eschema): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
313 |
yield rql, kwargs |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
314 |
|
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
315 |
# etype serialization |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
316 |
|
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
317 |
def eschema2rql(eschema, groupmap=None): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
318 |
"""return a list of rql insert statements to enter an entity schema |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
319 |
in the database as an CWEType entity |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
320 |
""" |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
321 |
relations, values = eschema_relations_values(eschema) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
322 |
# NOTE: 'specializes' relation can't be inserted here since there's no |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
323 |
# way to make sure the parent type is inserted before the child type |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
324 |
yield 'INSERT CWEType X: %s' % ','.join(relations) , values |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
325 |
# entity permissions |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
326 |
if groupmap is not None: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
327 |
for rql, args in _erperms2rql(eschema, groupmap): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
328 |
yield rql, args |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
329 |
|
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
330 |
def eschema_relations_values(eschema): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
331 |
values = _ervalues(eschema) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
332 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
333 |
return relations, values |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
334 |
|
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
335 |
def eschemaspecialize2rql(eschema): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
336 |
specialized_type = eschema.specializes() |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
337 |
if specialized_type: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
338 |
values = {'x': eschema.eid, 'et': specialized_type.eid} |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
339 |
yield 'SET X specializes ET WHERE X eid %(x)s, ET eid %(et)s', values |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
340 |
|
0 | 341 |
def _ervalues(erschema): |
342 |
try: |
|
343 |
type_ = unicode(erschema.type) |
|
344 |
except UnicodeDecodeError, e: |
|
345 |
raise Exception("can't decode %s [was %s]" % (erschema.type, e)) |
|
346 |
try: |
|
347 |
desc = unicode(erschema.description) or u'' |
|
348 |
except UnicodeDecodeError, e: |
|
349 |
raise Exception("can't decode %s [was %s]" % (erschema.description, e)) |
|
350 |
return { |
|
351 |
'name': type_, |
|
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2955
diff
changeset
|
352 |
'final': erschema.final, |
0 | 353 |
'description': desc, |
354 |
} |
|
355 |
||
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
356 |
# rtype serialization |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
357 |
|
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
358 |
def rschema2rql(rschema, cstrtypemap=None, addrdef=True, groupmap=None): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
359 |
"""return a list of rql insert statements to enter a relation schema |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
360 |
in the database as an CWRType entity |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
361 |
""" |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
362 |
if rschema.type == 'has_text': |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
363 |
return |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
364 |
relations, values = rschema_relations_values(rschema) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
365 |
yield 'INSERT CWRType X: %s' % ','.join(relations), values |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
366 |
if addrdef: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
367 |
assert cstrtypemap |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
368 |
# sort for testing purpose |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
369 |
for rdef in sorted(rschema.rdefs.itervalues(), |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
370 |
key=lambda x: (x.subject, x.object)): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
371 |
for rql, values in rdef2rql(rdef, cstrtypemap, groupmap): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
372 |
yield rql, values |
0 | 373 |
|
374 |
def rschema_relations_values(rschema): |
|
375 |
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
|
376 |
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
|
377 |
values['symmetric'] = rschema.symmetric |
0 | 378 |
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
|
379 |
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
|
380 |
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
|
381 |
else: |
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4631
diff
changeset
|
382 |
values['fulltext_container'] = rschema.fulltext_container |
0 | 383 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
384 |
return relations, values |
|
385 |
||
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
386 |
# rdef serialization |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
387 |
|
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
388 |
def rdef2rql(rdef, cstrtypemap, groupmap=None): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
389 |
# don't serialize infered relations |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
390 |
if rdef.infered: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
391 |
return |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
392 |
relations, values = _rdef_values(rdef) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
393 |
relations.append('X relation_type ER,X from_entity SE,X to_entity OE') |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
394 |
values.update({'se': rdef.subject.eid, 'rt': rdef.rtype.eid, 'oe': rdef.object.eid}) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
395 |
if rdef.final: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
396 |
etype = 'CWAttribute' |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
397 |
else: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
398 |
etype = 'CWRelation' |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
399 |
yield 'INSERT %s X: %s WHERE SE eid %%(se)s,ER eid %%(rt)s,OE eid %%(oe)s' % ( |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
400 |
etype, ','.join(relations), ), values |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
401 |
for rql, values in constraints2rql(cstrtypemap, rdef.constraints): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
402 |
yield rql, values |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
403 |
# no groupmap means "no security insertion" |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
404 |
if groupmap: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
405 |
for rql, args in _erperms2rql(rdef, groupmap): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
406 |
yield rql, args |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
407 |
|
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
408 |
def _rdef_values(rdef): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
409 |
amap = {'order': 'ordernum', 'default': 'defaultval'} |
0 | 410 |
values = {} |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
411 |
for prop, default in rdef.rproperty_defs(rdef.object).iteritems(): |
4003
b9436fe77c9e
fix bad merge
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
412 |
if prop in ('eid', 'constraints', 'uid', 'infered', 'permissions'): |
0 | 413 |
continue |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
414 |
value = getattr(rdef, prop) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
415 |
# XXX type cast really necessary? |
0 | 416 |
if prop in ('indexed', 'fulltextindexed', 'internationalizable'): |
417 |
value = bool(value) |
|
418 |
elif prop == 'ordernum': |
|
419 |
value = int(value) |
|
420 |
elif isinstance(value, str): |
|
421 |
value = unicode(value) |
|
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
422 |
if value is not None and prop == 'default': |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
423 |
if value is False: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
424 |
value = u'' |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
425 |
if not isinstance(value, unicode): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
426 |
value = unicode(value) |
0 | 427 |
values[amap.get(prop, prop)] = value |
428 |
relations = ['X %s %%(%s)s' % (attr, attr) for attr in sorted(values)] |
|
429 |
return relations, values |
|
430 |
||
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
431 |
def constraints2rql(cstrtypemap, constraints, rdefeid=None): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
432 |
for constraint in constraints: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
433 |
values = {'ct': cstrtypemap[constraint.type()], |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
434 |
'value': unicode(constraint.serialize()), |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
435 |
'x': rdefeid} # when not specified, will have to be set by the caller |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
436 |
yield 'INSERT CWConstraint X: X value %(value)s, X cstrtype CT, EDEF constrained_by X WHERE \ |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
437 |
CT eid %(ct)s, EDEF eid %(x)s', values |
0 | 438 |
|
439 |
||
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4010
diff
changeset
|
440 |
def _erperms2rql(erschema, groupmap): |
0 | 441 |
"""return rql insert statements to enter the entity or relation |
442 |
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
|
443 |
[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
|
444 |
and CWGroup entities |
0 | 445 |
""" |
446 |
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
|
447 |
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
|
448 |
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
|
449 |
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
|
450 |
# 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
|
451 |
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
|
452 |
for group_or_rqlexpr in grantedto: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
453 |
if isinstance(group_or_rqlexpr, basestring): |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
454 |
# group |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
455 |
try: |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
456 |
yield ('SET X %s_permission Y WHERE Y eid %%(g)s, X eid %%(x)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
|
457 |
{'g': groupmap[group_or_rqlexpr]}) |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
458 |
except KeyError: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
459 |
continue |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
460 |
else: |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
461 |
# rqlexpr |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
462 |
rqlexpr = group_or_rqlexpr |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
463 |
yield ('INSERT RQLExpression E: E expression %%(e)s, E exprtype %%(t)s, ' |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
464 |
'E mainvars %%(v)s, X %s_permission E WHERE X eid %%(x)s' % action, |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
465 |
{'e': unicode(rqlexpr.expression), |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
466 |
'v': unicode(rqlexpr.mainvars), |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3876
diff
changeset
|
467 |
't': unicode(rqlexpr.__class__.__name__)}) |
0 | 468 |
|
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
469 |
# update functions |
0 | 470 |
|
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
471 |
def updateeschema2rql(eschema, eid): |
0 | 472 |
relations, values = eschema_relations_values(eschema) |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
473 |
values['x'] = eid |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
474 |
yield 'SET %s WHERE X eid %%(x)s' % ','.join(relations), values |
1630
41aadba8b29f
delete-trailing-whitespaces, check table existance
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
475 |
|
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
476 |
def updaterschema2rql(rschema, eid): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
477 |
relations, values = rschema_relations_values(rschema) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
478 |
values['x'] = eid |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
479 |
yield 'SET %s WHERE X eid %%(x)s' % ','.join(relations), values |
0 | 480 |
|
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
481 |
def updaterdef2rql(rdef, eid): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
482 |
relations, values = _rdef_values(rdef) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
483 |
values['x'] = eid |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4760
diff
changeset
|
484 |
yield 'SET %s WHERE X eid %%(x)s' % ','.join(relations), values |