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