author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 10 Jun 2009 12:09:06 +0200 | |
branch | stable |
changeset 2087 | 88fa02f50e71 |
parent 2056 | 57d287a2132a |
child 2101 | 08003e0354a7 |
permissions | -rw-r--r-- |
0 | 1 |
"""schema hooks: |
2 |
||
3 |
- synchronize the living schema object with the persistent schema |
|
4 |
- perform physical update on the source when necessary |
|
5 |
||
6 |
checking for schema consistency is done in hooks.py |
|
7 |
||
8 |
:organization: Logilab |
|
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
9 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 10 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
11 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 12 |
""" |
13 |
__docformat__ = "restructuredtext en" |
|
14 |
||
15 |
from yams.schema import BASE_TYPES |
|
16 |
from yams.buildobjs import EntityType, RelationType, RelationDefinition |
|
2019
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
17 |
from yams.schema2sql import eschema2sql, rschema2sql, type_from_constraints |
0 | 18 |
|
19 |
from cubicweb import ValidationError, RepositoryError |
|
20 |
from cubicweb.server import schemaserial as ss |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
21 |
from cubicweb.server.sqlutils import SQL_PREFIX |
0 | 22 |
from cubicweb.server.pool import Operation, SingleLastOperation, PreCommitOperation |
23 |
from cubicweb.server.hookhelper import (entity_attr, entity_name, |
|
24 |
check_internal_entity) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
25 |
|
0 | 26 |
# core entity and relation types which can't be removed |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
27 |
CORE_ETYPES = list(BASE_TYPES) + ['CWEType', 'CWRType', 'CWUser', 'CWGroup', |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
28 |
'CWConstraint', 'CWAttribute', 'CWRelation'] |
0 | 29 |
CORE_RTYPES = ['eid', 'creation_date', 'modification_date', |
30 |
'login', 'upassword', 'name', |
|
31 |
'is', 'instanceof', 'owned_by', 'created_by', 'in_group', |
|
32 |
'relation_type', 'from_entity', 'to_entity', |
|
33 |
'constrainted_by', |
|
34 |
'read_permission', 'add_permission', |
|
35 |
'delete_permission', 'updated_permission', |
|
36 |
] |
|
37 |
||
38 |
def get_constraints(session, entity): |
|
39 |
constraints = [] |
|
40 |
for cstreid in session.query_data(entity.eid, ()): |
|
41 |
cstrent = session.entity(cstreid) |
|
42 |
cstr = CONSTRAINTS[cstrent.type].deserialize(cstrent.value) |
|
43 |
cstr.eid = cstreid |
|
44 |
constraints.append(cstr) |
|
45 |
return constraints |
|
46 |
||
47 |
def add_inline_relation_column(session, etype, rtype): |
|
48 |
"""add necessary column and index for an inlined relation""" |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
49 |
table = SQL_PREFIX + etype |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
50 |
column = SQL_PREFIX + rtype |
0 | 51 |
try: |
52 |
session.system_sql(str('ALTER TABLE %s ADD COLUMN %s integer' |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
53 |
% (table, column))) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
54 |
session.info('added column %s to table %s', column, table) |
0 | 55 |
except: |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
56 |
# silent exception here, if this error has not been raised because the |
0 | 57 |
# column already exists, index creation will fail anyway |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
58 |
session.exception('error while adding column %s to table %s', |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
59 |
table, column) |
0 | 60 |
# create index before alter table which may expectingly fail during test |
61 |
# (sqlite) while index creation should never fail (test for index existence |
|
62 |
# is done by the dbhelper) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
63 |
session.pool.source('system').create_index(session, table, column) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
64 |
session.info('added index on %s(%s)', table, column) |
0 | 65 |
session.add_query_data('createdattrs', '%s.%s' % (etype, rtype)) |
66 |
||
67 |
||
68 |
class SchemaOperation(Operation): |
|
69 |
"""base class for schema operations""" |
|
70 |
def __init__(self, session, kobj=None, **kwargs): |
|
71 |
self.schema = session.repo.schema |
|
72 |
self.kobj = kobj |
|
73 |
# once Operation.__init__ has been called, event may be triggered, so |
|
74 |
# do this last ! |
|
75 |
Operation.__init__(self, session, **kwargs) |
|
76 |
# every schema operation is triggering a schema update |
|
77 |
UpdateSchemaOp(session) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
78 |
|
0 | 79 |
class EarlySchemaOperation(SchemaOperation): |
80 |
def insert_index(self): |
|
81 |
"""schema operation which are inserted at the begining of the queue |
|
82 |
(typically to add/remove entity or relation types) |
|
83 |
""" |
|
84 |
i = -1 |
|
85 |
for i, op in enumerate(self.session.pending_operations): |
|
86 |
if not isinstance(op, EarlySchemaOperation): |
|
87 |
return i |
|
88 |
return i + 1 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
89 |
|
0 | 90 |
class UpdateSchemaOp(SingleLastOperation): |
91 |
"""the update schema operation: |
|
92 |
||
93 |
special operation which should be called once and after all other schema |
|
94 |
operations. It will trigger internal structures rebuilding to consider |
|
95 |
schema changes |
|
96 |
""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
97 |
|
0 | 98 |
def __init__(self, session): |
99 |
self.repo = session.repo |
|
100 |
SingleLastOperation.__init__(self, session) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
101 |
|
0 | 102 |
def commit_event(self): |
103 |
self.repo.set_schema(self.repo.schema) |
|
104 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
105 |
|
0 | 106 |
class DropTableOp(PreCommitOperation): |
107 |
"""actually remove a database from the application's schema""" |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
108 |
table = None # make pylint happy |
0 | 109 |
def precommit_event(self): |
110 |
dropped = self.session.query_data('droppedtables', |
|
111 |
default=set(), setdefault=True) |
|
112 |
if self.table in dropped: |
|
113 |
return # already processed |
|
114 |
dropped.add(self.table) |
|
115 |
self.session.system_sql('DROP TABLE %s' % self.table) |
|
116 |
self.info('dropped table %s', self.table) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
117 |
|
0 | 118 |
class DropColumnOp(PreCommitOperation): |
119 |
"""actually remove the attribut's column from entity table in the system |
|
120 |
database |
|
121 |
""" |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
122 |
table = column = None # make pylint happy |
0 | 123 |
def precommit_event(self): |
124 |
session, table, column = self.session, self.table, self.column |
|
125 |
# drop index if any |
|
126 |
session.pool.source('system').drop_index(session, table, column) |
|
127 |
try: |
|
128 |
session.system_sql('ALTER TABLE %s DROP COLUMN %s' |
|
129 |
% (table, column)) |
|
130 |
self.info('dropped column %s from table %s', column, table) |
|
131 |
except Exception, ex: |
|
132 |
# not supported by sqlite for instance |
|
133 |
self.error('error while altering table %s: %s', table, ex) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
134 |
|
0 | 135 |
|
136 |
# deletion #################################################################### |
|
137 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
138 |
class DeleteCWETypeOp(SchemaOperation): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
139 |
"""actually remove the entity type from the application's schema""" |
0 | 140 |
def commit_event(self): |
141 |
try: |
|
142 |
# del_entity_type also removes entity's relations |
|
143 |
self.schema.del_entity_type(self.kobj) |
|
144 |
except KeyError: |
|
145 |
# s/o entity type have already been deleted |
|
146 |
pass |
|
147 |
||
148 |
def before_del_eetype(session, eid): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
149 |
"""before deleting a CWEType entity: |
0 | 150 |
* check that we don't remove a core entity type |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
151 |
* cascade to delete related CWAttribute and CWRelation entities |
0 | 152 |
* instantiate an operation to delete the entity type on commit |
153 |
""" |
|
154 |
# final entities can't be deleted, don't care about that |
|
155 |
name = check_internal_entity(session, eid, CORE_ETYPES) |
|
156 |
# delete every entities of this type |
|
157 |
session.unsafe_execute('DELETE %s X' % name) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
158 |
DropTableOp(session, table=SQL_PREFIX + name) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
159 |
DeleteCWETypeOp(session, name) |
0 | 160 |
|
161 |
def after_del_eetype(session, eid): |
|
162 |
# workflow cleanup |
|
163 |
session.execute('DELETE State X WHERE NOT X state_of Y') |
|
164 |
session.execute('DELETE Transition X WHERE NOT X transition_of Y') |
|
165 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
166 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
167 |
class DeleteCWRTypeOp(SchemaOperation): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
168 |
"""actually remove the relation type from the application's schema""" |
0 | 169 |
def commit_event(self): |
170 |
try: |
|
171 |
self.schema.del_relation_type(self.kobj) |
|
172 |
except KeyError: |
|
173 |
# s/o entity type have already been deleted |
|
174 |
pass |
|
175 |
||
176 |
def before_del_ertype(session, eid): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
177 |
"""before deleting a CWRType entity: |
0 | 178 |
* check that we don't remove a core relation type |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
179 |
* cascade to delete related CWAttribute and CWRelation entities |
0 | 180 |
* instantiate an operation to delete the relation type on commit |
181 |
""" |
|
182 |
name = check_internal_entity(session, eid, CORE_RTYPES) |
|
183 |
# delete relation definitions using this relation type |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
184 |
session.execute('DELETE CWAttribute X WHERE X relation_type Y, Y eid %(x)s', |
0 | 185 |
{'x': eid}) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
186 |
session.execute('DELETE CWRelation X WHERE X relation_type Y, Y eid %(x)s', |
0 | 187 |
{'x': eid}) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
188 |
DeleteCWRTypeOp(session, name) |
0 | 189 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
190 |
|
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
191 |
class DelRelationDefOp(SchemaOperation): |
0 | 192 |
"""actually remove the relation definition from the application's schema""" |
193 |
def commit_event(self): |
|
194 |
subjtype, rtype, objtype = self.kobj |
|
195 |
try: |
|
196 |
self.schema.del_relation_def(subjtype, rtype, objtype) |
|
197 |
except KeyError: |
|
198 |
# relation type may have been already deleted |
|
199 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
200 |
|
0 | 201 |
def after_del_relation_type(session, rdefeid, rtype, rteid): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
202 |
"""before deleting a CWAttribute or CWRelation entity: |
0 | 203 |
* if this is a final or inlined relation definition, instantiate an |
204 |
operation to drop necessary column, else if this is the last instance |
|
205 |
of a non final relation, instantiate an operation to drop necessary |
|
206 |
table |
|
207 |
* instantiate an operation to delete the relation definition on commit |
|
208 |
* delete the associated relation type when necessary |
|
209 |
""" |
|
210 |
subjschema, rschema, objschema = session.repo.schema.schema_by_eid(rdefeid) |
|
211 |
pendings = session.query_data('pendingeids', ()) |
|
212 |
# first delete existing relation if necessary |
|
213 |
if rschema.is_final(): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
214 |
rdeftype = 'CWAttribute' |
0 | 215 |
else: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
216 |
rdeftype = 'CWRelation' |
0 | 217 |
if not (subjschema.eid in pendings or objschema.eid in pendings): |
218 |
session.execute('DELETE X %s Y WHERE X is %s, Y is %s' |
|
219 |
% (rschema, subjschema, objschema)) |
|
220 |
execute = session.unsafe_execute |
|
221 |
rset = execute('Any COUNT(X) WHERE X is %s, X relation_type R,' |
|
222 |
'R eid %%(x)s' % rdeftype, {'x': rteid}) |
|
223 |
lastrel = rset[0][0] == 0 |
|
224 |
# we have to update physical schema systematically for final and inlined |
|
225 |
# relations, but only if it's the last instance for this relation type |
|
226 |
# for other relations |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
227 |
|
0 | 228 |
if (rschema.is_final() or rschema.inlined): |
229 |
rset = execute('Any COUNT(X) WHERE X is %s, X relation_type R, ' |
|
230 |
'R eid %%(x)s, X from_entity E, E name %%(name)s' |
|
231 |
% rdeftype, {'x': rteid, 'name': str(subjschema)}) |
|
232 |
if rset[0][0] == 0 and not subjschema.eid in pendings: |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
233 |
DropColumnOp(session, table=SQL_PREFIX + subjschema.type, |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
234 |
column=SQL_PREFIX + rschema.type) |
0 | 235 |
elif lastrel: |
236 |
DropTableOp(session, table='%s_relation' % rschema.type) |
|
237 |
# if this is the last instance, drop associated relation type |
|
238 |
if lastrel and not rteid in pendings: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
239 |
execute('DELETE CWRType X WHERE X eid %(x)s', {'x': rteid}, 'x') |
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
240 |
DelRelationDefOp(session, (subjschema, rschema, objschema)) |
0 | 241 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
242 |
|
0 | 243 |
# addition #################################################################### |
244 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
245 |
class AddCWETypeOp(EarlySchemaOperation): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
246 |
"""actually add the entity type to the application's schema""" |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
247 |
eid = None # make pylint happy |
0 | 248 |
def commit_event(self): |
249 |
eschema = self.schema.add_entity_type(self.kobj) |
|
250 |
eschema.eid = self.eid |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
251 |
|
0 | 252 |
def before_add_eetype(session, entity): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
253 |
"""before adding a CWEType entity: |
0 | 254 |
* check that we are not using an existing entity type, |
255 |
""" |
|
256 |
name = entity['name'] |
|
257 |
schema = session.repo.schema |
|
258 |
if name in schema and schema[name].eid is not None: |
|
259 |
raise RepositoryError('an entity type %s already exists' % name) |
|
260 |
||
261 |
def after_add_eetype(session, entity): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
262 |
"""after adding a CWEType entity: |
0 | 263 |
* create the necessary table |
264 |
* set creation_date and modification_date by creating the necessary |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
265 |
CWAttribute entities |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
266 |
* add owned_by relation by creating the necessary CWRelation entity |
0 | 267 |
* register an operation to add the entity type to the application's |
268 |
schema on commit |
|
269 |
""" |
|
270 |
if entity.get('final'): |
|
271 |
return |
|
272 |
schema = session.repo.schema |
|
273 |
name = entity['name'] |
|
274 |
etype = EntityType(name=name, description=entity.get('description'), |
|
275 |
meta=entity.get('meta')) # don't care about final |
|
276 |
# fake we add it to the schema now to get a correctly initialized schema |
|
277 |
# but remove it before doing anything more dangerous... |
|
278 |
schema = session.repo.schema |
|
279 |
eschema = schema.add_entity_type(etype) |
|
280 |
eschema.set_default_groups() |
|
281 |
# generate table sql and rql to add metadata |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
282 |
tablesql = eschema2sql(session.pool.source('system').dbhelper, eschema, |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
283 |
prefix=SQL_PREFIX) |
0 | 284 |
relrqls = [] |
285 |
for rtype in ('is', 'is_instance_of', 'creation_date', 'modification_date', |
|
286 |
'created_by', 'owned_by'): |
|
287 |
rschema = schema[rtype] |
|
288 |
sampletype = rschema.subjects()[0] |
|
289 |
desttype = rschema.objects()[0] |
|
290 |
props = rschema.rproperties(sampletype, desttype) |
|
291 |
relrqls += list(ss.rdef2rql(rschema, name, desttype, props)) |
|
292 |
# now remove it ! |
|
293 |
schema.del_entity_type(name) |
|
294 |
# create the necessary table |
|
295 |
for sql in tablesql.split(';'): |
|
296 |
if sql.strip(): |
|
297 |
session.system_sql(sql) |
|
298 |
# register operation to modify the schema on commit |
|
299 |
# this have to be done before adding other relations definitions |
|
300 |
# or permission settings |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
301 |
AddCWETypeOp(session, etype, eid=entity.eid) |
0 | 302 |
# add meta creation_date, modification_date and owned_by relations |
303 |
for rql, kwargs in relrqls: |
|
304 |
session.execute(rql, kwargs) |
|
305 |
||
306 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
307 |
class AddCWRTypeOp(EarlySchemaOperation): |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
308 |
"""actually add the relation type to the application's schema""" |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
309 |
eid = None # make pylint happy |
0 | 310 |
def commit_event(self): |
311 |
rschema = self.schema.add_relation_type(self.kobj) |
|
312 |
rschema.set_default_groups() |
|
313 |
rschema.eid = self.eid |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
314 |
|
0 | 315 |
def before_add_ertype(session, entity): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
316 |
"""before adding a CWRType entity: |
0 | 317 |
* check that we are not using an existing relation type, |
318 |
* register an operation to add the relation type to the application's |
|
319 |
schema on commit |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
320 |
|
0 | 321 |
We don't know yeat this point if a table is necessary |
322 |
""" |
|
323 |
name = entity['name'] |
|
324 |
if name in session.repo.schema.relations(): |
|
325 |
raise RepositoryError('a relation type %s already exists' % name) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
326 |
|
0 | 327 |
def after_add_ertype(session, entity): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
328 |
"""after a CWRType entity has been added: |
0 | 329 |
* register an operation to add the relation type to the application's |
330 |
schema on commit |
|
331 |
We don't know yeat this point if a table is necessary |
|
332 |
""" |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
333 |
AddCWRTypeOp(session, RelationType(name=entity['name'], |
0 | 334 |
description=entity.get('description'), |
335 |
meta=entity.get('meta', False), |
|
336 |
inlined=entity.get('inlined', False), |
|
337 |
symetric=entity.get('symetric', False)), |
|
338 |
eid=entity.eid) |
|
339 |
||
340 |
||
341 |
class AddErdefOp(EarlySchemaOperation): |
|
342 |
"""actually add the attribute relation definition to the application's |
|
343 |
schema |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
344 |
""" |
0 | 345 |
def commit_event(self): |
346 |
self.schema.add_relation_def(self.kobj) |
|
347 |
||
348 |
TYPE_CONVERTER = { |
|
349 |
'Boolean': bool, |
|
350 |
'Int': int, |
|
351 |
'Float': float, |
|
352 |
'Password': str, |
|
353 |
'String': unicode, |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
354 |
'Date' : unicode, |
0 | 355 |
'Datetime' : unicode, |
356 |
'Time' : unicode, |
|
357 |
} |
|
358 |
||
359 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
360 |
class AddCWAttributePreCommitOp(PreCommitOperation): |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
361 |
"""an attribute relation (CWAttribute) has been added: |
0 | 362 |
* add the necessary column |
363 |
* set default on this column if any and possible |
|
364 |
* register an operation to add the relation definition to the |
|
365 |
application's schema on commit |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
366 |
|
0 | 367 |
constraints are handled by specific hooks |
368 |
""" |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
369 |
entity = None # make pylint happy |
0 | 370 |
def precommit_event(self): |
371 |
session = self.session |
|
372 |
entity = self.entity |
|
373 |
fromentity = entity.from_entity[0] |
|
374 |
relationtype = entity.relation_type[0] |
|
375 |
session.execute('SET X ordernum Y+1 WHERE X from_entity SE, SE eid %(se)s, X ordernum Y, X ordernum >= %(order)s, NOT X eid %(x)s', |
|
376 |
{'x': entity.eid, 'se': fromentity.eid, 'order': entity.ordernum or 0}) |
|
377 |
subj, rtype = str(fromentity.name), str(relationtype.name) |
|
378 |
obj = str(entity.to_entity[0].name) |
|
379 |
# at this point default is a string or None, but we need a correctly |
|
380 |
# typed value |
|
381 |
default = entity.defaultval |
|
382 |
if default is not None: |
|
383 |
default = TYPE_CONVERTER[obj](default) |
|
384 |
constraints = get_constraints(session, entity) |
|
385 |
rdef = RelationDefinition(subj, rtype, obj, |
|
386 |
cardinality=entity.cardinality, |
|
387 |
order=entity.ordernum, |
|
388 |
description=entity.description, |
|
389 |
default=default, |
|
390 |
indexed=entity.indexed, |
|
391 |
fulltextindexed=entity.fulltextindexed, |
|
392 |
internationalizable=entity.internationalizable, |
|
393 |
constraints=constraints, |
|
394 |
eid=entity.eid) |
|
395 |
sysource = session.pool.source('system') |
|
2019
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
396 |
attrtype = type_from_constraints(sysource.dbhelper, rdef.object, |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
397 |
constraints) |
0 | 398 |
# XXX should be moved somehow into lgc.adbh: sqlite doesn't support to |
399 |
# add a new column with UNIQUE, it should be added after the ALTER TABLE |
|
400 |
# using ADD INDEX |
|
401 |
if sysource.dbdriver == 'sqlite' and 'UNIQUE' in attrtype: |
|
402 |
extra_unique_index = True |
|
403 |
attrtype = attrtype.replace(' UNIQUE', '') |
|
404 |
else: |
|
405 |
extra_unique_index = False |
|
406 |
# added some str() wrapping query since some backend (eg psycopg) don't |
|
407 |
# allow unicode queries |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
408 |
table = SQL_PREFIX + subj |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
409 |
column = SQL_PREFIX + rtype |
0 | 410 |
try: |
411 |
session.system_sql(str('ALTER TABLE %s ADD COLUMN %s %s' |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
412 |
% (table, column, attrtype))) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
413 |
self.info('added column %s to table %s', table, column) |
0 | 414 |
except Exception, ex: |
415 |
# the column probably already exists. this occurs when |
|
416 |
# the entity's type has just been added or if the column |
|
417 |
# has not been previously dropped |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
418 |
self.error('error while altering table %s: %s', table, ex) |
0 | 419 |
if extra_unique_index or entity.indexed: |
420 |
try: |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
421 |
sysource.create_index(session, table, column, |
0 | 422 |
unique=extra_unique_index) |
423 |
except Exception, ex: |
|
424 |
self.error('error while creating index for %s.%s: %s', |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
425 |
table, column, ex) |
0 | 426 |
AddErdefOp(session, rdef) |
427 |
||
428 |
def after_add_efrdef(session, entity): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
429 |
AddCWAttributePreCommitOp(session, entity=entity) |
0 | 430 |
|
431 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
432 |
class AddCWRelationPreCommitOp(PreCommitOperation): |
0 | 433 |
"""an actual relation has been added: |
434 |
* if this is an inlined relation, add the necessary column |
|
435 |
else if it's the first instance of this relation type, add the |
|
436 |
necessary table and set default permissions |
|
437 |
* register an operation to add the relation definition to the |
|
438 |
application's schema on commit |
|
439 |
||
440 |
constraints are handled by specific hooks |
|
441 |
""" |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
442 |
entity = None # make pylint happy |
0 | 443 |
def precommit_event(self): |
444 |
session = self.session |
|
445 |
entity = self.entity |
|
446 |
fromentity = entity.from_entity[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
447 |
relationtype = entity.relation_type[0] |
0 | 448 |
session.execute('SET X ordernum Y+1 WHERE X from_entity SE, SE eid %(se)s, X ordernum Y, X ordernum >= %(order)s, NOT X eid %(x)s', |
449 |
{'x': entity.eid, 'se': fromentity.eid, 'order': entity.ordernum or 0}) |
|
450 |
subj, rtype = str(fromentity.name), str(relationtype.name) |
|
451 |
obj = str(entity.to_entity[0].name) |
|
452 |
card = entity.get('cardinality') |
|
453 |
rdef = RelationDefinition(subj, rtype, obj, |
|
454 |
cardinality=card, |
|
455 |
order=entity.ordernum, |
|
456 |
composite=entity.composite, |
|
457 |
description=entity.description, |
|
458 |
constraints=get_constraints(session, entity), |
|
459 |
eid=entity.eid) |
|
460 |
schema = session.repo.schema |
|
461 |
rschema = schema.rschema(rtype) |
|
462 |
# this have to be done before permissions setting |
|
463 |
AddErdefOp(session, rdef) |
|
464 |
if rschema.inlined: |
|
465 |
# need to add a column if the relation is inlined and if this is the |
|
466 |
# first occurence of "Subject relation Something" whatever Something |
|
467 |
# and if it has not been added during other event of the same |
|
468 |
# transaction |
|
469 |
key = '%s.%s' % (subj, rtype) |
|
470 |
try: |
|
471 |
alreadythere = bool(rschema.objects(subj)) |
|
472 |
except KeyError: |
|
473 |
alreadythere = False |
|
474 |
if not (alreadythere or |
|
475 |
key in session.query_data('createdattrs', ())): |
|
476 |
add_inline_relation_column(session, subj, rtype) |
|
477 |
else: |
|
478 |
# need to create the relation if no relation definition in the |
|
479 |
# schema and if it has not been added during other event of the same |
|
480 |
# transaction |
|
481 |
if not (rschema.subjects() or |
|
482 |
rtype in session.query_data('createdtables', ())): |
|
483 |
try: |
|
484 |
rschema = schema[rtype] |
|
485 |
tablesql = rschema2sql(rschema) |
|
486 |
except KeyError: |
|
487 |
# fake we add it to the schema now to get a correctly |
|
488 |
# initialized schema but remove it before doing anything |
|
489 |
# more dangerous... |
|
490 |
rschema = schema.add_relation_type(rdef) |
|
491 |
tablesql = rschema2sql(rschema) |
|
492 |
schema.del_relation_type(rtype) |
|
493 |
# create the necessary table |
|
494 |
for sql in tablesql.split(';'): |
|
495 |
if sql.strip(): |
|
496 |
self.session.system_sql(sql) |
|
497 |
session.add_query_data('createdtables', rtype) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
498 |
|
0 | 499 |
def after_add_enfrdef(session, entity): |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
500 |
AddCWRelationPreCommitOp(session, entity=entity) |
0 | 501 |
|
502 |
||
503 |
# update ###################################################################### |
|
504 |
||
505 |
def check_valid_changes(session, entity, ro_attrs=('name', 'final')): |
|
506 |
errors = {} |
|
507 |
# don't use getattr(entity, attr), we would get the modified value if any |
|
508 |
for attr in ro_attrs: |
|
509 |
origval = entity_attr(session, entity.eid, attr) |
|
510 |
if entity.get(attr, origval) != origval: |
|
511 |
errors[attr] = session._("can't change the %s attribute") % \ |
|
512 |
display_name(session, attr) |
|
513 |
if errors: |
|
514 |
raise ValidationError(entity.eid, errors) |
|
515 |
||
516 |
def before_update_eetype(session, entity): |
|
517 |
"""check name change, handle final""" |
|
518 |
check_valid_changes(session, entity, ro_attrs=('final',)) |
|
519 |
# don't use getattr(entity, attr), we would get the modified value if any |
|
520 |
oldname = entity_attr(session, entity.eid, 'name') |
|
521 |
newname = entity.get('name', oldname) |
|
522 |
if newname.lower() != oldname.lower(): |
|
523 |
eschema = session.repo.schema[oldname] |
|
524 |
UpdateEntityTypeName(session, eschema=eschema, |
|
525 |
oldname=oldname, newname=newname) |
|
526 |
||
527 |
def before_update_ertype(session, entity): |
|
528 |
"""check name change, handle final""" |
|
529 |
check_valid_changes(session, entity) |
|
530 |
||
531 |
||
532 |
class UpdateEntityTypeName(SchemaOperation): |
|
533 |
"""this operation updates physical storage accordingly""" |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
534 |
oldname = newname = None # make pylint happy |
0 | 535 |
|
536 |
def precommit_event(self): |
|
537 |
# we need sql to operate physical changes on the system database |
|
538 |
sqlexec = self.session.system_sql |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
539 |
sqlexec('ALTER TABLE %s%s RENAME TO %s%s' % (SQL_PREFIX, self.oldname, |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
540 |
SQL_PREFIX, self.newname)) |
0 | 541 |
self.info('renamed table %s to %s', self.oldname, self.newname) |
542 |
sqlexec('UPDATE entities SET type=%s WHERE type=%s', |
|
543 |
(self.newname, self.oldname)) |
|
544 |
sqlexec('UPDATE deleted_entities SET type=%s WHERE type=%s', |
|
545 |
(self.newname, self.oldname)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
546 |
|
0 | 547 |
def commit_event(self): |
548 |
self.session.repo.schema.rename_entity_type(self.oldname, self.newname) |
|
549 |
||
550 |
||
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
551 |
class UpdateRelationDefOp(SchemaOperation): |
0 | 552 |
"""actually update some properties of a relation definition""" |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
553 |
rschema = values = None # make pylint happy |
0 | 554 |
|
555 |
def precommit_event(self): |
|
2019
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
556 |
etype = self.kobj[0] |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
557 |
table = SQL_PREFIX + etype |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
558 |
column = SQL_PREFIX + self.rschema.type |
0 | 559 |
if 'indexed' in self.values: |
560 |
sysource = self.session.pool.source('system') |
|
561 |
if self.values['indexed']: |
|
562 |
sysource.create_index(self.session, table, column) |
|
563 |
else: |
|
564 |
sysource.drop_index(self.session, table, column) |
|
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
565 |
if 'cardinality' in self.values and self.rschema.is_final(): |
2019
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
566 |
adbh = self.session.pool.source('system').dbhelper |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
567 |
if not adbh.alter_column_support: |
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
568 |
# not supported (and NOT NULL not set by yams in that case, so |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
569 |
# no worry) |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
570 |
return |
2019
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
571 |
atype = self.rschema.objects(etype)[0] |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
572 |
constraints = self.rschema.rproperty(etype, atype, 'constraints') |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
573 |
coltype = type_from_constraints(adbh, atype, constraints, |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
574 |
creating=False) |
2056 | 575 |
# XXX check self.values['cardinality'][0] actually changed? |
2019
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
576 |
sql = adbh.sql_set_null_allowed(table, column, coltype, |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
577 |
self.values['cardinality'][0] != '1') |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
578 |
self.session.system_sql(sql) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
579 |
|
0 | 580 |
def commit_event(self): |
581 |
# structure should be clean, not need to remove entity's relations |
|
582 |
# at this point |
|
583 |
self.rschema._rproperties[self.kobj].update(self.values) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
584 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
585 |
|
0 | 586 |
def after_update_erdef(session, entity): |
587 |
desttype = entity.to_entity[0].name |
|
588 |
rschema = session.repo.schema[entity.relation_type[0].name] |
|
589 |
newvalues = {} |
|
590 |
for prop in rschema.rproperty_defs(desttype): |
|
591 |
if prop == 'constraints': |
|
592 |
continue |
|
593 |
if prop == 'order': |
|
594 |
prop = 'ordernum' |
|
595 |
if prop in entity: |
|
596 |
newvalues[prop] = entity[prop] |
|
597 |
if newvalues: |
|
598 |
subjtype = entity.from_entity[0].name |
|
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
599 |
UpdateRelationDefOp(session, (subjtype, desttype), |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
600 |
rschema=rschema, values=newvalues) |
0 | 601 |
|
602 |
||
603 |
class UpdateRtypeOp(SchemaOperation): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
604 |
"""actually update some properties of a relation definition""" |
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
605 |
rschema = values = entity = None # make pylint happy |
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
606 |
|
0 | 607 |
def precommit_event(self): |
608 |
session = self.session |
|
609 |
rschema = self.rschema |
|
610 |
if rschema.is_final() or not 'inlined' in self.values: |
|
611 |
return # nothing to do |
|
612 |
inlined = self.values['inlined'] |
|
613 |
entity = self.entity |
|
614 |
if not entity.inlined_changed(inlined): # check in-lining is necessary/possible |
|
615 |
return # nothing to do |
|
616 |
# inlined changed, make necessary physical changes! |
|
617 |
sqlexec = self.session.system_sql |
|
618 |
rtype = rschema.type |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
619 |
eidcolumn = SQL_PREFIX + 'eid' |
0 | 620 |
if not inlined: |
621 |
# need to create the relation if it has not been already done by another |
|
622 |
# event of the same transaction |
|
623 |
if not rschema.type in session.query_data('createdtables', ()): |
|
624 |
tablesql = rschema2sql(rschema) |
|
625 |
# create the necessary table |
|
626 |
for sql in tablesql.split(';'): |
|
627 |
if sql.strip(): |
|
628 |
sqlexec(sql) |
|
629 |
session.add_query_data('createdtables', rschema.type) |
|
630 |
# copy existant data |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
631 |
column = SQL_PREFIX + rtype |
0 | 632 |
for etype in rschema.subjects(): |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
633 |
table = SQL_PREFIX + str(etype) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
634 |
sqlexec('INSERT INTO %s_relation SELECT %s, %s FROM %s WHERE NOT %s IS NULL' |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
635 |
% (rtype, eidcolumn, column, table, column)) |
0 | 636 |
# drop existant columns |
637 |
for etype in rschema.subjects(): |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
638 |
DropColumnOp(session, table=SQL_PREFIX + str(etype), |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
639 |
column=SQL_PREFIX + rtype) |
0 | 640 |
else: |
641 |
for etype in rschema.subjects(): |
|
642 |
try: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
643 |
add_inline_relation_column(session, str(etype), rtype) |
0 | 644 |
except Exception, ex: |
645 |
# the column probably already exists. this occurs when |
|
646 |
# the entity's type has just been added or if the column |
|
647 |
# has not been previously dropped |
|
648 |
self.error('error while altering table %s: %s', etype, ex) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
649 |
# copy existant data. |
0 | 650 |
# XXX don't use, it's not supported by sqlite (at least at when i tried it) |
651 |
#sqlexec('UPDATE %(etype)s SET %(rtype)s=eid_to ' |
|
652 |
# 'FROM %(rtype)s_relation ' |
|
653 |
# 'WHERE %(etype)s.eid=%(rtype)s_relation.eid_from' |
|
654 |
# % locals()) |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
655 |
table = SQL_PREFIX + str(etype) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
656 |
cursor = sqlexec('SELECT eid_from, eid_to FROM %(table)s, ' |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
657 |
'%(rtype)s_relation WHERE %(table)s.%(eidcolumn)s=' |
0 | 658 |
'%(rtype)s_relation.eid_from' % locals()) |
659 |
args = [{'val': eid_to, 'x': eid} for eid, eid_to in cursor.fetchall()] |
|
660 |
if args: |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
661 |
column = SQL_PREFIX + rtype |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
662 |
cursor.executemany('UPDATE %s SET %s=%%(val)s WHERE %s=%%(x)s' |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
663 |
% (table, column, eidcolumn), args) |
0 | 664 |
# drop existant table |
665 |
DropTableOp(session, table='%s_relation' % rtype) |
|
666 |
||
667 |
def commit_event(self): |
|
668 |
# structure should be clean, not need to remove entity's relations |
|
669 |
# at this point |
|
670 |
self.rschema.__dict__.update(self.values) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
671 |
|
0 | 672 |
def after_update_ertype(session, entity): |
673 |
rschema = session.repo.schema.rschema(entity.name) |
|
674 |
newvalues = {} |
|
675 |
for prop in ('meta', 'symetric', 'inlined'): |
|
676 |
if prop in entity: |
|
677 |
newvalues[prop] = entity[prop] |
|
678 |
if newvalues: |
|
679 |
UpdateRtypeOp(session, entity=entity, rschema=rschema, values=newvalues) |
|
680 |
||
681 |
# constraints synchronization ################################################# |
|
682 |
||
683 |
from cubicweb.schema import CONSTRAINTS |
|
684 |
||
685 |
class ConstraintOp(SchemaOperation): |
|
686 |
"""actually update constraint of a relation definition""" |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
687 |
entity = None # make pylint happy |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
688 |
|
0 | 689 |
def prepare_constraints(self, rtype, subjtype, objtype): |
690 |
constraints = rtype.rproperty(subjtype, objtype, 'constraints') |
|
691 |
self.constraints = list(constraints) |
|
692 |
rtype.set_rproperty(subjtype, objtype, 'constraints', self.constraints) |
|
693 |
return self.constraints |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
694 |
|
0 | 695 |
def precommit_event(self): |
696 |
rdef = self.entity.reverse_constrained_by[0] |
|
697 |
session = self.session |
|
698 |
# when the relation is added in the same transaction, the constraint object |
|
699 |
# is created by AddEN?FRDefPreCommitOp, there is nothing to do here |
|
700 |
if rdef.eid in session.query_data('neweids', ()): |
|
701 |
self.cancelled = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
702 |
return |
0 | 703 |
self.cancelled = False |
704 |
schema = session.repo.schema |
|
705 |
subjtype, rtype, objtype = schema.schema_by_eid(rdef.eid) |
|
706 |
self.prepare_constraints(rtype, subjtype, objtype) |
|
707 |
cstrtype = self.entity.type |
|
708 |
self.cstr = rtype.constraint_by_type(subjtype, objtype, cstrtype) |
|
709 |
self._cstr = CONSTRAINTS[cstrtype].deserialize(self.entity.value) |
|
710 |
self._cstr.eid = self.entity.eid |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
711 |
table = SQL_PREFIX + str(subjtype) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
712 |
column = SQL_PREFIX + str(rtype) |
0 | 713 |
# alter the physical schema on size constraint changes |
714 |
if self._cstr.type() == 'SizeConstraint' and ( |
|
715 |
self.cstr is None or self.cstr.max != self._cstr.max): |
|
2019
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
716 |
adbh = self.session.pool.source('system').dbhelper |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
717 |
card = rtype.rproperty(subjtype, objtype, 'cardinality') |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
718 |
coltype = type_from_constraints(adbh, objtype, [self._cstr], |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
719 |
creating=False) |
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
720 |
sql = adbh.sql_change_col_type(table, column, coltype, card != '1') |
0 | 721 |
try: |
2019
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
722 |
session.system_sql(sql) |
0 | 723 |
self.info('altered column %s of table %s: now VARCHAR(%s)', |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
724 |
column, table, self._cstr.max) |
0 | 725 |
except Exception, ex: |
726 |
# not supported by sqlite for instance |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
727 |
self.error('error while altering table %s: %s', table, ex) |
0 | 728 |
elif cstrtype == 'UniqueConstraint': |
729 |
session.pool.source('system').create_index( |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
730 |
self.session, table, column, unique=True) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
731 |
|
0 | 732 |
def commit_event(self): |
733 |
if self.cancelled: |
|
734 |
return |
|
2019
399a930e10ef
#343488: SQL error in migration script (depends on a new release of yams and lgc)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2011
diff
changeset
|
735 |
# in-place modification |
0 | 736 |
if not self.cstr is None: |
737 |
self.constraints.remove(self.cstr) |
|
738 |
self.constraints.append(self._cstr) |
|
739 |
||
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
740 |
|
0 | 741 |
def after_add_econstraint(session, entity): |
742 |
ConstraintOp(session, entity=entity) |
|
743 |
||
744 |
def after_update_econstraint(session, entity): |
|
745 |
ConstraintOp(session, entity=entity) |
|
746 |
||
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
747 |
|
0 | 748 |
class DelConstraintOp(ConstraintOp): |
749 |
"""actually remove a constraint of a relation definition""" |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
750 |
rtype = subjtype = objtype = None # make pylint happy |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
751 |
|
0 | 752 |
def precommit_event(self): |
753 |
self.prepare_constraints(self.rtype, self.subjtype, self.objtype) |
|
754 |
cstrtype = self.cstr.type() |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
755 |
table = SQL_PREFIX + str(self.subjtype) |
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
756 |
column = SQL_PREFIX + str(self.rtype) |
0 | 757 |
# alter the physical schema on size/unique constraint changes |
758 |
if cstrtype == 'SizeConstraint': |
|
759 |
try: |
|
760 |
self.session.system_sql('ALTER TABLE %s ALTER COLUMN %s TYPE TEXT' |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
761 |
% (table, column)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
762 |
self.info('altered column %s of table %s: now TEXT', |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
763 |
column, table) |
0 | 764 |
except Exception, ex: |
765 |
# not supported by sqlite for instance |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
766 |
self.error('error while altering table %s: %s', table, ex) |
0 | 767 |
elif cstrtype == 'UniqueConstraint': |
768 |
self.session.pool.source('system').drop_index( |
|
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
769 |
self.session, table, column, unique=True) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
770 |
|
0 | 771 |
def commit_event(self): |
772 |
self.constraints.remove(self.cstr) |
|
773 |
||
774 |
||
775 |
def before_delete_constrained_by(session, fromeid, rtype, toeid): |
|
776 |
if not fromeid in session.query_data('pendingeids', ()): |
|
777 |
schema = session.repo.schema |
|
778 |
entity = session.eid_rset(toeid).get_entity(0, 0) |
|
779 |
subjtype, rtype, objtype = schema.schema_by_eid(fromeid) |
|
780 |
try: |
|
781 |
cstr = rtype.constraint_by_type(subjtype, objtype, entity.cstrtype[0].name) |
|
782 |
DelConstraintOp(session, subjtype=subjtype, rtype=rtype, objtype=objtype, |
|
783 |
cstr=cstr) |
|
784 |
except IndexError: |
|
785 |
session.critical('constraint type no more accessible') |
|
786 |
||
787 |
||
788 |
def after_add_constrained_by(session, fromeid, rtype, toeid): |
|
789 |
if fromeid in session.query_data('neweids', ()): |
|
790 |
session.add_query_data(fromeid, toeid) |
|
791 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
792 |
|
0 | 793 |
# schema permissions synchronization ########################################## |
794 |
||
795 |
class PermissionOp(Operation): |
|
796 |
"""base class to synchronize schema permission definitions""" |
|
797 |
def __init__(self, session, perm, etype_eid): |
|
798 |
self.perm = perm |
|
799 |
try: |
|
800 |
self.name = entity_name(session, etype_eid) |
|
801 |
except IndexError: |
|
802 |
self.error('changing permission of a no more existant type #%s', |
|
803 |
etype_eid) |
|
804 |
else: |
|
805 |
Operation.__init__(self, session) |
|
806 |
||
807 |
class AddGroupPermissionOp(PermissionOp): |
|
808 |
"""synchronize schema when a *_permission relation has been added on a group |
|
809 |
""" |
|
810 |
def __init__(self, session, perm, etype_eid, group_eid): |
|
811 |
self.group = entity_name(session, group_eid) |
|
812 |
PermissionOp.__init__(self, session, perm, etype_eid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
813 |
|
0 | 814 |
def commit_event(self): |
815 |
"""the observed connections pool has been commited""" |
|
816 |
try: |
|
817 |
erschema = self.schema[self.name] |
|
818 |
except KeyError: |
|
819 |
# duh, schema not found, log error and skip operation |
|
820 |
self.error('no schema for %s', self.name) |
|
821 |
return |
|
822 |
groups = list(erschema.get_groups(self.perm)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
823 |
try: |
0 | 824 |
groups.index(self.group) |
825 |
self.warning('group %s already have permission %s on %s', |
|
826 |
self.group, self.perm, erschema.type) |
|
827 |
except ValueError: |
|
828 |
groups.append(self.group) |
|
829 |
erschema.set_groups(self.perm, groups) |
|
830 |
||
831 |
class AddRQLExpressionPermissionOp(PermissionOp): |
|
832 |
"""synchronize schema when a *_permission relation has been added on a rql |
|
833 |
expression |
|
834 |
""" |
|
835 |
def __init__(self, session, perm, etype_eid, expression): |
|
836 |
self.expr = expression |
|
837 |
PermissionOp.__init__(self, session, perm, etype_eid) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
838 |
|
0 | 839 |
def commit_event(self): |
840 |
"""the observed connections pool has been commited""" |
|
841 |
try: |
|
842 |
erschema = self.schema[self.name] |
|
843 |
except KeyError: |
|
844 |
# duh, schema not found, log error and skip operation |
|
845 |
self.error('no schema for %s', self.name) |
|
846 |
return |
|
847 |
exprs = list(erschema.get_rqlexprs(self.perm)) |
|
848 |
exprs.append(erschema.rql_expression(self.expr)) |
|
849 |
erschema.set_rqlexprs(self.perm, exprs) |
|
850 |
||
851 |
def after_add_permission(session, subject, rtype, object): |
|
852 |
"""added entity/relation *_permission, need to update schema""" |
|
853 |
perm = rtype.split('_', 1)[0] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
854 |
if session.describe(object)[0] == 'CWGroup': |
0 | 855 |
AddGroupPermissionOp(session, perm, subject, object) |
856 |
else: # RQLExpression |
|
857 |
expr = session.execute('Any EXPR WHERE X eid %(x)s, X expression EXPR', |
|
858 |
{'x': object}, 'x')[0][0] |
|
859 |
AddRQLExpressionPermissionOp(session, perm, subject, expr) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
860 |
|
0 | 861 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
862 |
|
0 | 863 |
class DelGroupPermissionOp(AddGroupPermissionOp): |
864 |
"""synchronize schema when a *_permission relation has been deleted from a group""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
865 |
|
0 | 866 |
def commit_event(self): |
867 |
"""the observed connections pool has been commited""" |
|
868 |
try: |
|
869 |
erschema = self.schema[self.name] |
|
870 |
except KeyError: |
|
871 |
# duh, schema not found, log error and skip operation |
|
872 |
self.error('no schema for %s', self.name) |
|
873 |
return |
|
874 |
groups = list(erschema.get_groups(self.perm)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
875 |
try: |
0 | 876 |
groups.remove(self.group) |
877 |
erschema.set_groups(self.perm, groups) |
|
878 |
except ValueError: |
|
879 |
self.error('can\'t remove permission %s on %s to group %s', |
|
880 |
self.perm, erschema.type, self.group) |
|
881 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
882 |
|
0 | 883 |
class DelRQLExpressionPermissionOp(AddRQLExpressionPermissionOp): |
884 |
"""synchronize schema when a *_permission relation has been deleted from an rql expression""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
885 |
|
0 | 886 |
def commit_event(self): |
887 |
"""the observed connections pool has been commited""" |
|
888 |
try: |
|
889 |
erschema = self.schema[self.name] |
|
890 |
except KeyError: |
|
891 |
# duh, schema not found, log error and skip operation |
|
892 |
self.error('no schema for %s', self.name) |
|
893 |
return |
|
894 |
rqlexprs = list(erschema.get_rqlexprs(self.perm)) |
|
895 |
for i, rqlexpr in enumerate(rqlexprs): |
|
896 |
if rqlexpr.expression == self.expr: |
|
897 |
rqlexprs.pop(i) |
|
898 |
break |
|
899 |
else: |
|
900 |
self.error('can\'t remove permission %s on %s for expression %s', |
|
901 |
self.perm, erschema.type, self.expr) |
|
902 |
return |
|
903 |
erschema.set_rqlexprs(self.perm, rqlexprs) |
|
904 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
905 |
|
0 | 906 |
def before_del_permission(session, subject, rtype, object): |
907 |
"""delete entity/relation *_permission, need to update schema |
|
908 |
||
909 |
skip the operation if the related type is being deleted |
|
910 |
""" |
|
911 |
if subject in session.query_data('pendingeids', ()): |
|
912 |
return |
|
913 |
perm = rtype.split('_', 1)[0] |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
914 |
if session.describe(object)[0] == 'CWGroup': |
0 | 915 |
DelGroupPermissionOp(session, perm, subject, object) |
916 |
else: # RQLExpression |
|
917 |
expr = session.execute('Any EXPR WHERE X eid %(x)s, X expression EXPR', |
|
918 |
{'x': object}, 'x')[0][0] |
|
919 |
DelRQLExpressionPermissionOp(session, perm, subject, expr) |
|
920 |
||
921 |
||
922 |
def rebuild_infered_relations(session, subject, rtype, object): |
|
923 |
# registering a schema operation will trigger a call to |
|
924 |
# repo.set_schema() on commit which will in turn rebuild |
|
925 |
# infered relation definitions |
|
926 |
UpdateSchemaOp(session) |
|
927 |
||
928 |
||
929 |
def _register_schema_hooks(hm): |
|
930 |
"""register schema related hooks on the hooks manager""" |
|
931 |
# schema synchronisation ##################### |
|
932 |
# before/after add |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
933 |
hm.register_hook(before_add_eetype, 'before_add_entity', 'CWEType') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
934 |
hm.register_hook(before_add_ertype, 'before_add_entity', 'CWRType') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
935 |
hm.register_hook(after_add_eetype, 'after_add_entity', 'CWEType') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
936 |
hm.register_hook(after_add_ertype, 'after_add_entity', 'CWRType') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
937 |
hm.register_hook(after_add_efrdef, 'after_add_entity', 'CWAttribute') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
938 |
hm.register_hook(after_add_enfrdef, 'after_add_entity', 'CWRelation') |
0 | 939 |
# before/after update |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
940 |
hm.register_hook(before_update_eetype, 'before_update_entity', 'CWEType') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
941 |
hm.register_hook(before_update_ertype, 'before_update_entity', 'CWRType') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
942 |
hm.register_hook(after_update_ertype, 'after_update_entity', 'CWRType') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
943 |
hm.register_hook(after_update_erdef, 'after_update_entity', 'CWAttribute') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
944 |
hm.register_hook(after_update_erdef, 'after_update_entity', 'CWRelation') |
0 | 945 |
# before/after delete |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
946 |
hm.register_hook(before_del_eetype, 'before_delete_entity', 'CWEType') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
947 |
hm.register_hook(after_del_eetype, 'after_delete_entity', 'CWEType') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
948 |
hm.register_hook(before_del_ertype, 'before_delete_entity', 'CWRType') |
0 | 949 |
hm.register_hook(after_del_relation_type, 'after_delete_relation', 'relation_type') |
950 |
hm.register_hook(rebuild_infered_relations, 'after_add_relation', 'specializes') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
951 |
hm.register_hook(rebuild_infered_relations, 'after_delete_relation', 'specializes') |
0 | 952 |
# constraints synchronization hooks |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
953 |
hm.register_hook(after_add_econstraint, 'after_add_entity', 'CWConstraint') |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
954 |
hm.register_hook(after_update_econstraint, 'after_update_entity', 'CWConstraint') |
0 | 955 |
hm.register_hook(before_delete_constrained_by, 'before_delete_relation', 'constrained_by') |
956 |
hm.register_hook(after_add_constrained_by, 'after_add_relation', 'constrained_by') |
|
957 |
# permissions synchronisation ################ |
|
958 |
for perm in ('read_permission', 'add_permission', |
|
959 |
'delete_permission', 'update_permission'): |
|
960 |
hm.register_hook(after_add_permission, 'after_add_relation', perm) |
|
961 |
hm.register_hook(before_del_permission, 'before_delete_relation', perm) |