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