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