author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Sat, 30 May 2009 00:42:18 +0200 | |
changeset 2016 | a6638de6d4da |
parent 2011 | 758ccc0a9d16 |
child 2019 | 399a930e10ef |
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 |
|
17 |
from yams.schema2sql import eschema2sql, rschema2sql, _type_from_constraints |
|
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') |
|
396 |
attrtype = _type_from_constraints(sysource.dbhelper, rdef.object, |
|
397 |
constraints) |
|
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 |
# postgres doesn't implement, so do it in two times |
427 |
# ALTER TABLE %s ADD COLUMN %s %s SET DEFAULT %s |
|
428 |
if default is not None: |
|
429 |
if isinstance(default, unicode): |
|
430 |
default = default.encode(sysource.encoding) |
|
431 |
try: |
|
432 |
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
|
433 |
'%%(default)s' % (table, column), |
0 | 434 |
{'default': default}) |
435 |
except Exception, ex: |
|
436 |
# 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
|
437 |
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
|
438 |
session.system_sql('UPDATE %s SET %s=%%(default)s' % (table, column), |
0 | 439 |
{'default': default}) |
440 |
AddErdefOp(session, rdef) |
|
441 |
||
442 |
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
|
443 |
AddCWAttributePreCommitOp(session, entity=entity) |
0 | 444 |
|
445 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
446 |
class AddCWRelationPreCommitOp(PreCommitOperation): |
0 | 447 |
"""an actual relation has been added: |
448 |
* if this is an inlined relation, add the necessary column |
|
449 |
else if it's the first instance of this relation type, add the |
|
450 |
necessary table and set default permissions |
|
451 |
* register an operation to add the relation definition to the |
|
452 |
application's schema on commit |
|
453 |
||
454 |
constraints are handled by specific hooks |
|
455 |
""" |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
456 |
entity = None # make pylint happy |
0 | 457 |
def precommit_event(self): |
458 |
session = self.session |
|
459 |
entity = self.entity |
|
460 |
fromentity = entity.from_entity[0] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
461 |
relationtype = entity.relation_type[0] |
0 | 462 |
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', |
463 |
{'x': entity.eid, 'se': fromentity.eid, 'order': entity.ordernum or 0}) |
|
464 |
subj, rtype = str(fromentity.name), str(relationtype.name) |
|
465 |
obj = str(entity.to_entity[0].name) |
|
466 |
card = entity.get('cardinality') |
|
467 |
rdef = RelationDefinition(subj, rtype, obj, |
|
468 |
cardinality=card, |
|
469 |
order=entity.ordernum, |
|
470 |
composite=entity.composite, |
|
471 |
description=entity.description, |
|
472 |
constraints=get_constraints(session, entity), |
|
473 |
eid=entity.eid) |
|
474 |
schema = session.repo.schema |
|
475 |
rschema = schema.rschema(rtype) |
|
476 |
# this have to be done before permissions setting |
|
477 |
AddErdefOp(session, rdef) |
|
478 |
if rschema.inlined: |
|
479 |
# need to add a column if the relation is inlined and if this is the |
|
480 |
# first occurence of "Subject relation Something" whatever Something |
|
481 |
# and if it has not been added during other event of the same |
|
482 |
# transaction |
|
483 |
key = '%s.%s' % (subj, rtype) |
|
484 |
try: |
|
485 |
alreadythere = bool(rschema.objects(subj)) |
|
486 |
except KeyError: |
|
487 |
alreadythere = False |
|
488 |
if not (alreadythere or |
|
489 |
key in session.query_data('createdattrs', ())): |
|
490 |
add_inline_relation_column(session, subj, rtype) |
|
491 |
else: |
|
492 |
# need to create the relation if no relation definition in the |
|
493 |
# schema and if it has not been added during other event of the same |
|
494 |
# transaction |
|
495 |
if not (rschema.subjects() or |
|
496 |
rtype in session.query_data('createdtables', ())): |
|
497 |
try: |
|
498 |
rschema = schema[rtype] |
|
499 |
tablesql = rschema2sql(rschema) |
|
500 |
except KeyError: |
|
501 |
# fake we add it to the schema now to get a correctly |
|
502 |
# initialized schema but remove it before doing anything |
|
503 |
# more dangerous... |
|
504 |
rschema = schema.add_relation_type(rdef) |
|
505 |
tablesql = rschema2sql(rschema) |
|
506 |
schema.del_relation_type(rtype) |
|
507 |
# create the necessary table |
|
508 |
for sql in tablesql.split(';'): |
|
509 |
if sql.strip(): |
|
510 |
self.session.system_sql(sql) |
|
511 |
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
|
512 |
|
0 | 513 |
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
|
514 |
AddCWRelationPreCommitOp(session, entity=entity) |
0 | 515 |
|
516 |
||
517 |
# update ###################################################################### |
|
518 |
||
519 |
def check_valid_changes(session, entity, ro_attrs=('name', 'final')): |
|
520 |
errors = {} |
|
521 |
# don't use getattr(entity, attr), we would get the modified value if any |
|
522 |
for attr in ro_attrs: |
|
523 |
origval = entity_attr(session, entity.eid, attr) |
|
524 |
if entity.get(attr, origval) != origval: |
|
525 |
errors[attr] = session._("can't change the %s attribute") % \ |
|
526 |
display_name(session, attr) |
|
527 |
if errors: |
|
528 |
raise ValidationError(entity.eid, errors) |
|
529 |
||
530 |
def before_update_eetype(session, entity): |
|
531 |
"""check name change, handle final""" |
|
532 |
check_valid_changes(session, entity, ro_attrs=('final',)) |
|
533 |
# don't use getattr(entity, attr), we would get the modified value if any |
|
534 |
oldname = entity_attr(session, entity.eid, 'name') |
|
535 |
newname = entity.get('name', oldname) |
|
536 |
if newname.lower() != oldname.lower(): |
|
537 |
eschema = session.repo.schema[oldname] |
|
538 |
UpdateEntityTypeName(session, eschema=eschema, |
|
539 |
oldname=oldname, newname=newname) |
|
540 |
||
541 |
def before_update_ertype(session, entity): |
|
542 |
"""check name change, handle final""" |
|
543 |
check_valid_changes(session, entity) |
|
544 |
||
545 |
||
546 |
class UpdateEntityTypeName(SchemaOperation): |
|
547 |
"""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
|
548 |
oldname = newname = None # make pylint happy |
0 | 549 |
|
550 |
def precommit_event(self): |
|
551 |
# we need sql to operate physical changes on the system database |
|
552 |
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
|
553 |
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
|
554 |
SQL_PREFIX, self.newname)) |
0 | 555 |
self.info('renamed table %s to %s', self.oldname, self.newname) |
556 |
sqlexec('UPDATE entities SET type=%s WHERE type=%s', |
|
557 |
(self.newname, self.oldname)) |
|
558 |
sqlexec('UPDATE deleted_entities SET type=%s WHERE type=%s', |
|
559 |
(self.newname, self.oldname)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
560 |
|
0 | 561 |
def commit_event(self): |
562 |
self.session.repo.schema.rename_entity_type(self.oldname, self.newname) |
|
563 |
||
564 |
||
1981
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
565 |
class UpdateRelationDefOp(SchemaOperation): |
0 | 566 |
"""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
|
567 |
rschema = values = None # make pylint happy |
0 | 568 |
|
569 |
def precommit_event(self): |
|
570 |
if 'indexed' in self.values: |
|
571 |
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
|
572 |
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
|
573 |
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
|
574 |
column = SQL_PREFIX + rtype |
0 | 575 |
if self.values['indexed']: |
576 |
sysource.create_index(self.session, table, column) |
|
577 |
else: |
|
578 |
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
|
579 |
if 'cardinality' in self.values and self.rschema.is_final(): |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
580 |
if self.session.pool.source('system').dbdriver == 'sqlite': |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
581 |
# 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
|
582 |
# no worry) |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
583 |
return |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
584 |
sqlexec = self.session.system_sql |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
585 |
etype, rtype = self.kobj[0], self.rschema.type |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
586 |
if self.values['cardinality'][0] == '1': |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
587 |
cmd = 'SET' |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
588 |
else: |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
589 |
cmd = 'DROP' |
e6eed4324357
fix #327301: synchronize_schema doesn't update not-null constraint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1802
diff
changeset
|
590 |
sqlexec('ALTER TABLE %s ALTER COLUMN %s %s NOT NULL' % ( |
2007 | 591 |
SQL_PREFIX + etype, SQL_PREFIX + rtype, cmd)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
592 |
|
0 | 593 |
def commit_event(self): |
594 |
# structure should be clean, not need to remove entity's relations |
|
595 |
# at this point |
|
596 |
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
|
597 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
598 |
|
0 | 599 |
def after_update_erdef(session, entity): |
600 |
desttype = entity.to_entity[0].name |
|
601 |
rschema = session.repo.schema[entity.relation_type[0].name] |
|
602 |
newvalues = {} |
|
603 |
for prop in rschema.rproperty_defs(desttype): |
|
604 |
if prop == 'constraints': |
|
605 |
continue |
|
606 |
if prop == 'order': |
|
607 |
prop = 'ordernum' |
|
608 |
if prop in entity: |
|
609 |
newvalues[prop] = entity[prop] |
|
610 |
if newvalues: |
|
611 |
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
|
612 |
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
|
613 |
rschema=rschema, values=newvalues) |
0 | 614 |
|
615 |
||
616 |
class UpdateRtypeOp(SchemaOperation): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
617 |
"""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
|
618 |
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
|
619 |
|
0 | 620 |
def precommit_event(self): |
621 |
session = self.session |
|
622 |
rschema = self.rschema |
|
623 |
if rschema.is_final() or not 'inlined' in self.values: |
|
624 |
return # nothing to do |
|
625 |
inlined = self.values['inlined'] |
|
626 |
entity = self.entity |
|
627 |
if not entity.inlined_changed(inlined): # check in-lining is necessary/possible |
|
628 |
return # nothing to do |
|
629 |
# inlined changed, make necessary physical changes! |
|
630 |
sqlexec = self.session.system_sql |
|
631 |
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
|
632 |
eidcolumn = SQL_PREFIX + 'eid' |
0 | 633 |
if not inlined: |
634 |
# need to create the relation if it has not been already done by another |
|
635 |
# event of the same transaction |
|
636 |
if not rschema.type in session.query_data('createdtables', ()): |
|
637 |
tablesql = rschema2sql(rschema) |
|
638 |
# create the necessary table |
|
639 |
for sql in tablesql.split(';'): |
|
640 |
if sql.strip(): |
|
641 |
sqlexec(sql) |
|
642 |
session.add_query_data('createdtables', rschema.type) |
|
643 |
# 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
|
644 |
column = SQL_PREFIX + rtype |
0 | 645 |
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
|
646 |
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
|
647 |
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
|
648 |
% (rtype, eidcolumn, column, table, column)) |
0 | 649 |
# drop existant columns |
650 |
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
|
651 |
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
|
652 |
column=SQL_PREFIX + rtype) |
0 | 653 |
else: |
654 |
for etype in rschema.subjects(): |
|
655 |
try: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
656 |
add_inline_relation_column(session, str(etype), rtype) |
0 | 657 |
except Exception, ex: |
658 |
# the column probably already exists. this occurs when |
|
659 |
# the entity's type has just been added or if the column |
|
660 |
# has not been previously dropped |
|
661 |
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
|
662 |
# copy existant data. |
0 | 663 |
# XXX don't use, it's not supported by sqlite (at least at when i tried it) |
664 |
#sqlexec('UPDATE %(etype)s SET %(rtype)s=eid_to ' |
|
665 |
# 'FROM %(rtype)s_relation ' |
|
666 |
# 'WHERE %(etype)s.eid=%(rtype)s_relation.eid_from' |
|
667 |
# % 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
|
668 |
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
|
669 |
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
|
670 |
'%(rtype)s_relation WHERE %(table)s.%(eidcolumn)s=' |
0 | 671 |
'%(rtype)s_relation.eid_from' % locals()) |
672 |
args = [{'val': eid_to, 'x': eid} for eid, eid_to in cursor.fetchall()] |
|
673 |
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
|
674 |
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
|
675 |
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
|
676 |
% (table, column, eidcolumn), args) |
0 | 677 |
# drop existant table |
678 |
DropTableOp(session, table='%s_relation' % rtype) |
|
679 |
||
680 |
def commit_event(self): |
|
681 |
# structure should be clean, not need to remove entity's relations |
|
682 |
# at this point |
|
683 |
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
|
684 |
|
0 | 685 |
def after_update_ertype(session, entity): |
686 |
rschema = session.repo.schema.rschema(entity.name) |
|
687 |
newvalues = {} |
|
688 |
for prop in ('meta', 'symetric', 'inlined'): |
|
689 |
if prop in entity: |
|
690 |
newvalues[prop] = entity[prop] |
|
691 |
if newvalues: |
|
692 |
UpdateRtypeOp(session, entity=entity, rschema=rschema, values=newvalues) |
|
693 |
||
694 |
# constraints synchronization ################################################# |
|
695 |
||
696 |
from cubicweb.schema import CONSTRAINTS |
|
697 |
||
698 |
class ConstraintOp(SchemaOperation): |
|
699 |
"""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
|
700 |
entity = None # make pylint happy |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
701 |
|
0 | 702 |
def prepare_constraints(self, rtype, subjtype, objtype): |
703 |
constraints = rtype.rproperty(subjtype, objtype, 'constraints') |
|
704 |
self.constraints = list(constraints) |
|
705 |
rtype.set_rproperty(subjtype, objtype, 'constraints', self.constraints) |
|
706 |
return self.constraints |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
707 |
|
0 | 708 |
def precommit_event(self): |
709 |
rdef = self.entity.reverse_constrained_by[0] |
|
710 |
session = self.session |
|
711 |
# when the relation is added in the same transaction, the constraint object |
|
712 |
# is created by AddEN?FRDefPreCommitOp, there is nothing to do here |
|
713 |
if rdef.eid in session.query_data('neweids', ()): |
|
714 |
self.cancelled = True |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
715 |
return |
0 | 716 |
self.cancelled = False |
717 |
schema = session.repo.schema |
|
718 |
subjtype, rtype, objtype = schema.schema_by_eid(rdef.eid) |
|
719 |
self.prepare_constraints(rtype, subjtype, objtype) |
|
720 |
cstrtype = self.entity.type |
|
721 |
self.cstr = rtype.constraint_by_type(subjtype, objtype, cstrtype) |
|
722 |
self._cstr = CONSTRAINTS[cstrtype].deserialize(self.entity.value) |
|
723 |
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
|
724 |
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
|
725 |
column = SQL_PREFIX + str(rtype) |
0 | 726 |
# alter the physical schema on size constraint changes |
727 |
if self._cstr.type() == 'SizeConstraint' and ( |
|
728 |
self.cstr is None or self.cstr.max != self._cstr.max): |
|
729 |
try: |
|
730 |
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
|
731 |
% (table, column, self._cstr.max)) |
0 | 732 |
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
|
733 |
column, table, self._cstr.max) |
0 | 734 |
except Exception, ex: |
735 |
# 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
|
736 |
self.error('error while altering table %s: %s', table, ex) |
0 | 737 |
elif cstrtype == 'UniqueConstraint': |
738 |
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
|
739 |
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
|
740 |
|
0 | 741 |
def commit_event(self): |
742 |
if self.cancelled: |
|
743 |
return |
|
744 |
# in-place removing |
|
745 |
if not self.cstr is None: |
|
746 |
self.constraints.remove(self.cstr) |
|
747 |
self.constraints.append(self._cstr) |
|
748 |
||
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
|
749 |
|
0 | 750 |
def after_add_econstraint(session, entity): |
751 |
ConstraintOp(session, entity=entity) |
|
752 |
||
753 |
def after_update_econstraint(session, entity): |
|
754 |
ConstraintOp(session, entity=entity) |
|
755 |
||
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 |
|
0 | 757 |
class DelConstraintOp(ConstraintOp): |
758 |
"""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
|
759 |
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
|
760 |
|
0 | 761 |
def precommit_event(self): |
762 |
self.prepare_constraints(self.rtype, self.subjtype, self.objtype) |
|
763 |
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
|
764 |
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
|
765 |
column = SQL_PREFIX + str(self.rtype) |
0 | 766 |
# alter the physical schema on size/unique constraint changes |
767 |
if cstrtype == 'SizeConstraint': |
|
768 |
try: |
|
769 |
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
|
770 |
% (table, column)) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
771 |
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
|
772 |
column, table) |
0 | 773 |
except Exception, ex: |
774 |
# 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
|
775 |
self.error('error while altering table %s: %s', table, ex) |
0 | 776 |
elif cstrtype == 'UniqueConstraint': |
777 |
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
|
778 |
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
|
779 |
|
0 | 780 |
def commit_event(self): |
781 |
self.constraints.remove(self.cstr) |
|
782 |
||
783 |
||
784 |
def before_delete_constrained_by(session, fromeid, rtype, toeid): |
|
785 |
if not fromeid in session.query_data('pendingeids', ()): |
|
786 |
schema = session.repo.schema |
|
787 |
entity = session.eid_rset(toeid).get_entity(0, 0) |
|
788 |
subjtype, rtype, objtype = schema.schema_by_eid(fromeid) |
|
789 |
try: |
|
790 |
cstr = rtype.constraint_by_type(subjtype, objtype, entity.cstrtype[0].name) |
|
791 |
DelConstraintOp(session, subjtype=subjtype, rtype=rtype, objtype=objtype, |
|
792 |
cstr=cstr) |
|
793 |
except IndexError: |
|
794 |
session.critical('constraint type no more accessible') |
|
795 |
||
796 |
||
797 |
def after_add_constrained_by(session, fromeid, rtype, toeid): |
|
798 |
if fromeid in session.query_data('neweids', ()): |
|
799 |
session.add_query_data(fromeid, toeid) |
|
800 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
801 |
|
0 | 802 |
# schema permissions synchronization ########################################## |
803 |
||
804 |
class PermissionOp(Operation): |
|
805 |
"""base class to synchronize schema permission definitions""" |
|
806 |
def __init__(self, session, perm, etype_eid): |
|
807 |
self.perm = perm |
|
808 |
try: |
|
809 |
self.name = entity_name(session, etype_eid) |
|
810 |
except IndexError: |
|
811 |
self.error('changing permission of a no more existant type #%s', |
|
812 |
etype_eid) |
|
813 |
else: |
|
814 |
Operation.__init__(self, session) |
|
815 |
||
816 |
class AddGroupPermissionOp(PermissionOp): |
|
817 |
"""synchronize schema when a *_permission relation has been added on a group |
|
818 |
""" |
|
819 |
def __init__(self, session, perm, etype_eid, group_eid): |
|
820 |
self.group = entity_name(session, group_eid) |
|
821 |
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
|
822 |
|
0 | 823 |
def commit_event(self): |
824 |
"""the observed connections pool has been commited""" |
|
825 |
try: |
|
826 |
erschema = self.schema[self.name] |
|
827 |
except KeyError: |
|
828 |
# duh, schema not found, log error and skip operation |
|
829 |
self.error('no schema for %s', self.name) |
|
830 |
return |
|
831 |
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
|
832 |
try: |
0 | 833 |
groups.index(self.group) |
834 |
self.warning('group %s already have permission %s on %s', |
|
835 |
self.group, self.perm, erschema.type) |
|
836 |
except ValueError: |
|
837 |
groups.append(self.group) |
|
838 |
erschema.set_groups(self.perm, groups) |
|
839 |
||
840 |
class AddRQLExpressionPermissionOp(PermissionOp): |
|
841 |
"""synchronize schema when a *_permission relation has been added on a rql |
|
842 |
expression |
|
843 |
""" |
|
844 |
def __init__(self, session, perm, etype_eid, expression): |
|
845 |
self.expr = expression |
|
846 |
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
|
847 |
|
0 | 848 |
def commit_event(self): |
849 |
"""the observed connections pool has been commited""" |
|
850 |
try: |
|
851 |
erschema = self.schema[self.name] |
|
852 |
except KeyError: |
|
853 |
# duh, schema not found, log error and skip operation |
|
854 |
self.error('no schema for %s', self.name) |
|
855 |
return |
|
856 |
exprs = list(erschema.get_rqlexprs(self.perm)) |
|
857 |
exprs.append(erschema.rql_expression(self.expr)) |
|
858 |
erschema.set_rqlexprs(self.perm, exprs) |
|
859 |
||
860 |
def after_add_permission(session, subject, rtype, object): |
|
861 |
"""added entity/relation *_permission, need to update schema""" |
|
862 |
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
|
863 |
if session.describe(object)[0] == 'CWGroup': |
0 | 864 |
AddGroupPermissionOp(session, perm, subject, object) |
865 |
else: # RQLExpression |
|
866 |
expr = session.execute('Any EXPR WHERE X eid %(x)s, X expression EXPR', |
|
867 |
{'x': object}, 'x')[0][0] |
|
868 |
AddRQLExpressionPermissionOp(session, perm, subject, expr) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
869 |
|
0 | 870 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
871 |
|
0 | 872 |
class DelGroupPermissionOp(AddGroupPermissionOp): |
873 |
"""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
|
874 |
|
0 | 875 |
def commit_event(self): |
876 |
"""the observed connections pool has been commited""" |
|
877 |
try: |
|
878 |
erschema = self.schema[self.name] |
|
879 |
except KeyError: |
|
880 |
# duh, schema not found, log error and skip operation |
|
881 |
self.error('no schema for %s', self.name) |
|
882 |
return |
|
883 |
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
|
884 |
try: |
0 | 885 |
groups.remove(self.group) |
886 |
erschema.set_groups(self.perm, groups) |
|
887 |
except ValueError: |
|
888 |
self.error('can\'t remove permission %s on %s to group %s', |
|
889 |
self.perm, erschema.type, self.group) |
|
890 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
891 |
|
0 | 892 |
class DelRQLExpressionPermissionOp(AddRQLExpressionPermissionOp): |
893 |
"""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
|
894 |
|
0 | 895 |
def commit_event(self): |
896 |
"""the observed connections pool has been commited""" |
|
897 |
try: |
|
898 |
erschema = self.schema[self.name] |
|
899 |
except KeyError: |
|
900 |
# duh, schema not found, log error and skip operation |
|
901 |
self.error('no schema for %s', self.name) |
|
902 |
return |
|
903 |
rqlexprs = list(erschema.get_rqlexprs(self.perm)) |
|
904 |
for i, rqlexpr in enumerate(rqlexprs): |
|
905 |
if rqlexpr.expression == self.expr: |
|
906 |
rqlexprs.pop(i) |
|
907 |
break |
|
908 |
else: |
|
909 |
self.error('can\'t remove permission %s on %s for expression %s', |
|
910 |
self.perm, erschema.type, self.expr) |
|
911 |
return |
|
912 |
erschema.set_rqlexprs(self.perm, rqlexprs) |
|
913 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1398
diff
changeset
|
914 |
|
0 | 915 |
def before_del_permission(session, subject, rtype, object): |
916 |
"""delete entity/relation *_permission, need to update schema |
|
917 |
||
918 |
skip the operation if the related type is being deleted |
|
919 |
""" |
|
920 |
if subject in session.query_data('pendingeids', ()): |
|
921 |
return |
|
922 |
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
|
923 |
if session.describe(object)[0] == 'CWGroup': |
0 | 924 |
DelGroupPermissionOp(session, perm, subject, object) |
925 |
else: # RQLExpression |
|
926 |
expr = session.execute('Any EXPR WHERE X eid %(x)s, X expression EXPR', |
|
927 |
{'x': object}, 'x')[0][0] |
|
928 |
DelRQLExpressionPermissionOp(session, perm, subject, expr) |
|
929 |
||
930 |
||
931 |
def rebuild_infered_relations(session, subject, rtype, object): |
|
932 |
# registering a schema operation will trigger a call to |
|
933 |
# repo.set_schema() on commit which will in turn rebuild |
|
934 |
# infered relation definitions |
|
935 |
UpdateSchemaOp(session) |
|
936 |
||
937 |
||
938 |
def _register_schema_hooks(hm): |
|
939 |
"""register schema related hooks on the hooks manager""" |
|
940 |
# schema synchronisation ##################### |
|
941 |
# before/after add |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
942 |
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
|
943 |
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
|
944 |
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
|
945 |
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
|
946 |
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
|
947 |
hm.register_hook(after_add_enfrdef, 'after_add_entity', 'CWRelation') |
0 | 948 |
# before/after update |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
949 |
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
|
950 |
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
|
951 |
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
|
952 |
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
|
953 |
hm.register_hook(after_update_erdef, 'after_update_entity', 'CWRelation') |
0 | 954 |
# before/after delete |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
955 |
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
|
956 |
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
|
957 |
hm.register_hook(before_del_ertype, 'before_delete_entity', 'CWRType') |
0 | 958 |
hm.register_hook(after_del_relation_type, 'after_delete_relation', 'relation_type') |
959 |
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
|
960 |
hm.register_hook(rebuild_infered_relations, 'after_delete_relation', 'specializes') |
0 | 961 |
# constraints synchronization hooks |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
962 |
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
|
963 |
hm.register_hook(after_update_econstraint, 'after_update_entity', 'CWConstraint') |
0 | 964 |
hm.register_hook(before_delete_constrained_by, 'before_delete_relation', 'constrained_by') |
965 |
hm.register_hook(after_add_constrained_by, 'after_add_relation', 'constrained_by') |
|
966 |
# permissions synchronisation ################ |
|
967 |
for perm in ('read_permission', 'add_permission', |
|
968 |
'delete_permission', 'update_permission'): |
|
969 |
hm.register_hook(after_add_permission, 'after_add_relation', perm) |
|
970 |
hm.register_hook(before_del_permission, 'before_delete_relation', perm) |