author | sylvain.thenault@logilab.fr |
Wed, 29 Apr 2009 09:03:31 +0200 | |
branch | tls-sprint |
changeset 1530 | 849fd3d64f11 |
parent 1477 | b056a49c16dc |
child 1790 | a3e2b079de00 |
permissions | -rw-r--r-- |
0 | 1 |
"""a class implementing basic actions used in migration scripts. |
2 |
||
3 |
The following schema actions are supported for now: |
|
4 |
* add/drop/rename attribute |
|
5 |
* add/drop entity/relation type |
|
6 |
* rename entity type |
|
7 |
||
8 |
The following data actions are supported for now: |
|
9 |
* add an entity |
|
10 |
* execute raw RQL queries |
|
11 |
||
12 |
||
13 |
:organization: Logilab |
|
447 | 14 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 15 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
16 |
""" |
|
17 |
__docformat__ = "restructuredtext en" |
|
18 |
||
19 |
import sys |
|
20 |
import os |
|
21 |
from os.path import join, exists |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
972
diff
changeset
|
22 |
from datetime import datetime |
0 | 23 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
24 |
from logilab.common.deprecation import deprecated_function, obsolete |
0 | 25 |
from logilab.common.decorators import cached |
26 |
from logilab.common.adbh import get_adv_func_helper |
|
27 |
||
28 |
from yams.constraints import SizeConstraint |
|
29 |
from yams.schema2sql import eschema2sql, rschema2sql |
|
30 |
||
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
31 |
from cubicweb import AuthenticationError, ETYPE_NAME_MAP |
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
32 |
from cubicweb.schema import CubicWebRelationSchema |
0 | 33 |
from cubicweb.dbapi import get_repository, repo_connect |
34 |
from cubicweb.common.migration import MigrationHelper, yes |
|
35 |
||
36 |
try: |
|
37 |
from cubicweb.server import schemaserial as ss |
|
38 |
from cubicweb.server.utils import manager_userpasswd |
|
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:
1240
diff
changeset
|
39 |
from cubicweb.server.sqlutils import sqlexec, SQL_PREFIX |
0 | 40 |
except ImportError: # LAX |
41 |
pass |
|
42 |
||
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:
1240
diff
changeset
|
43 |
|
0 | 44 |
class ServerMigrationHelper(MigrationHelper): |
45 |
"""specific migration helper for server side migration scripts, |
|
46 |
providind actions related to schema/data migration |
|
47 |
""" |
|
48 |
||
49 |
def __init__(self, config, schema, interactive=True, |
|
50 |
repo=None, cnx=None, verbosity=1, connect=True): |
|
51 |
MigrationHelper.__init__(self, config, interactive, verbosity) |
|
52 |
if not interactive: |
|
53 |
assert cnx |
|
54 |
assert repo |
|
55 |
if cnx is not None: |
|
56 |
assert repo |
|
57 |
self._cnx = cnx |
|
58 |
self.repo = repo |
|
59 |
elif connect: |
|
60 |
self.repo_connect() |
|
61 |
if not schema: |
|
62 |
schema = config.load_schema(expand_cubes=True) |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
63 |
self.fs_schema = schema |
0 | 64 |
self._synchronized = set() |
65 |
||
66 |
@cached |
|
67 |
def repo_connect(self): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
68 |
self.repo = get_repository(method='inmemory', config=self.config) |
0 | 69 |
return self.repo |
1477 | 70 |
|
0 | 71 |
def shutdown(self): |
72 |
if self.repo is not None: |
|
73 |
self.repo.shutdown() |
|
1477 | 74 |
|
0 | 75 |
def rewrite_vcconfiguration(self): |
76 |
"""write current installed versions (of cubicweb software |
|
77 |
and of each used cube) into the database |
|
78 |
""" |
|
79 |
self.cmd_set_property('system.version.cubicweb', self.config.cubicweb_version()) |
|
80 |
for pkg in self.config.cubes(): |
|
81 |
pkgversion = self.config.cube_version(pkg) |
|
82 |
self.cmd_set_property('system.version.%s' % pkg.lower(), pkgversion) |
|
83 |
self.commit() |
|
1477 | 84 |
|
0 | 85 |
def backup_database(self, backupfile=None, askconfirm=True): |
86 |
config = self.config |
|
87 |
source = config.sources()['system'] |
|
88 |
helper = get_adv_func_helper(source['db-driver']) |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
972
diff
changeset
|
89 |
date = datetime.now().strftime('%Y-%m-%d_%H:%M:%S') |
0 | 90 |
app = config.appid |
91 |
backupfile = backupfile or join(config.backup_dir(), |
|
92 |
'%s-%s.dump' % (app, date)) |
|
93 |
if exists(backupfile): |
|
94 |
if not self.confirm('a backup already exists for %s, overwrite it?' % app): |
|
95 |
return |
|
96 |
elif askconfirm and not self.confirm('backup %s database?' % app): |
|
97 |
return |
|
98 |
cmd = helper.backup_command(source['db-name'], source.get('db-host'), |
|
99 |
source.get('db-user'), backupfile, |
|
100 |
keepownership=False) |
|
101 |
while True: |
|
102 |
print cmd |
|
103 |
if os.system(cmd): |
|
104 |
print 'error while backuping the base' |
|
105 |
answer = self.confirm('continue anyway?', |
|
106 |
shell=False, abort=False, retry=True) |
|
107 |
if not answer: |
|
108 |
raise SystemExit(1) |
|
109 |
if answer == 1: # 1: continue, 2: retry |
|
110 |
break |
|
111 |
else: |
|
1240
6a9f621e07cc
restrict read perms to user on backup files
sylvain.thenault@logilab.fr
parents:
1080
diff
changeset
|
112 |
from cubicweb.toolsutils import restrict_perms_to_user |
0 | 113 |
print 'database backup:', backupfile |
1240
6a9f621e07cc
restrict read perms to user on backup files
sylvain.thenault@logilab.fr
parents:
1080
diff
changeset
|
114 |
restrict_perms_to_user(backupfile, self.info) |
0 | 115 |
break |
1477 | 116 |
|
0 | 117 |
def restore_database(self, backupfile, drop=True): |
118 |
config = self.config |
|
119 |
source = config.sources()['system'] |
|
120 |
helper = get_adv_func_helper(source['db-driver']) |
|
121 |
app = config.appid |
|
122 |
if not exists(backupfile): |
|
123 |
raise Exception("backup file %s doesn't exist" % backupfile) |
|
124 |
if self.confirm('restore %s database from %s ?' % (app, backupfile)): |
|
125 |
for cmd in helper.restore_commands(source['db-name'], source.get('db-host'), |
|
126 |
source.get('db-user'), backupfile, |
|
127 |
source['db-encoding'], |
|
128 |
keepownership=False, drop=drop): |
|
129 |
while True: |
|
130 |
print cmd |
|
131 |
if os.system(cmd): |
|
132 |
print 'error while restoring the base' |
|
133 |
answer = self.confirm('continue anyway?', |
|
134 |
shell=False, abort=False, retry=True) |
|
135 |
if not answer: |
|
136 |
raise SystemExit(1) |
|
137 |
if answer == 1: # 1: continue, 2: retry |
|
138 |
break |
|
139 |
else: |
|
140 |
break |
|
141 |
print 'database restored' |
|
1477 | 142 |
|
0 | 143 |
def migrate(self, vcconf, toupgrade, options): |
144 |
if not options.fs_only: |
|
145 |
if options.backup_db is None: |
|
146 |
self.backup_database() |
|
147 |
elif options.backup_db: |
|
148 |
self.backup_database(askconfirm=False) |
|
149 |
super(ServerMigrationHelper, self).migrate(vcconf, toupgrade, options) |
|
1477 | 150 |
|
0 | 151 |
def process_script(self, migrscript, funcname=None, *args, **kwargs): |
152 |
"""execute a migration script |
|
153 |
in interactive mode, display the migration script path, ask for |
|
154 |
confirmation and execute it if confirmed |
|
155 |
""" |
|
156 |
if migrscript.endswith('.sql'): |
|
157 |
if self.execscript_confirm(migrscript): |
|
158 |
sqlexec(open(migrscript).read(), self.session.system_sql) |
|
159 |
else: |
|
160 |
return super(ServerMigrationHelper, self).process_script( |
|
161 |
migrscript, funcname, *args, **kwargs) |
|
1477 | 162 |
|
0 | 163 |
@property |
164 |
def cnx(self): |
|
165 |
"""lazy connection""" |
|
166 |
try: |
|
167 |
return self._cnx |
|
168 |
except AttributeError: |
|
169 |
sourcescfg = self.repo.config.sources() |
|
170 |
try: |
|
171 |
login = sourcescfg['admin']['login'] |
|
172 |
pwd = sourcescfg['admin']['password'] |
|
173 |
except KeyError: |
|
174 |
login, pwd = manager_userpasswd() |
|
175 |
while True: |
|
176 |
try: |
|
177 |
self._cnx = repo_connect(self.repo, login, pwd) |
|
178 |
if not 'managers' in self._cnx.user(self.session).groups: |
|
179 |
print 'migration need an account in the managers group' |
|
180 |
else: |
|
181 |
break |
|
182 |
except AuthenticationError: |
|
183 |
print 'wrong user/password' |
|
184 |
except (KeyboardInterrupt, EOFError): |
|
185 |
print 'aborting...' |
|
186 |
sys.exit(0) |
|
187 |
try: |
|
188 |
login, pwd = manager_userpasswd() |
|
189 |
except (KeyboardInterrupt, EOFError): |
|
190 |
print 'aborting...' |
|
191 |
sys.exit(0) |
|
192 |
return self._cnx |
|
193 |
||
194 |
@property |
|
195 |
def session(self): |
|
196 |
return self.repo._get_session(self.cnx.sessionid) |
|
1477 | 197 |
|
0 | 198 |
@property |
199 |
@cached |
|
200 |
def rqlcursor(self): |
|
201 |
"""lazy rql cursor""" |
|
1080
7437abc17e02
should not give session as cnx.cursor(), else we may try to execute some query while no pool is set on the session
sylvain.thenault@logilab.fr
parents:
972
diff
changeset
|
202 |
# should not give session as cnx.cursor(), else we may try to execute |
7437abc17e02
should not give session as cnx.cursor(), else we may try to execute some query while no pool is set on the session
sylvain.thenault@logilab.fr
parents:
972
diff
changeset
|
203 |
# some query while no pool is set on the session (eg on entity attribute |
7437abc17e02
should not give session as cnx.cursor(), else we may try to execute some query while no pool is set on the session
sylvain.thenault@logilab.fr
parents:
972
diff
changeset
|
204 |
# access for instance) |
7437abc17e02
should not give session as cnx.cursor(), else we may try to execute some query while no pool is set on the session
sylvain.thenault@logilab.fr
parents:
972
diff
changeset
|
205 |
return self.cnx.cursor() |
1477 | 206 |
|
0 | 207 |
def commit(self): |
208 |
if hasattr(self, '_cnx'): |
|
209 |
self._cnx.commit() |
|
1477 | 210 |
|
0 | 211 |
def rollback(self): |
212 |
if hasattr(self, '_cnx'): |
|
213 |
self._cnx.rollback() |
|
1477 | 214 |
|
0 | 215 |
def rqlexecall(self, rqliter, cachekey=None, ask_confirm=True): |
216 |
for rql, kwargs in rqliter: |
|
217 |
self.rqlexec(rql, kwargs, cachekey, ask_confirm) |
|
218 |
||
219 |
@cached |
|
220 |
def _create_context(self): |
|
221 |
"""return a dictionary to use as migration script execution context""" |
|
222 |
context = super(ServerMigrationHelper, self)._create_context() |
|
223 |
context.update({'checkpoint': self.checkpoint, |
|
224 |
'sql': self.sqlexec, |
|
225 |
'rql': self.rqlexec, |
|
226 |
'rqliter': self.rqliter, |
|
227 |
'schema': self.repo.schema, |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
228 |
'fsschema': self.fs_schema, |
0 | 229 |
'session' : self.session, |
230 |
'repo' : self.repo, |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
231 |
'synchronize_schema': deprecated_function(self.sync_schema_props_perms), |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
232 |
'synchronize_eschema': deprecated_function(self.sync_schema_props_perms), |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
233 |
'synchronize_rschema': deprecated_function(self.sync_schema_props_perms), |
0 | 234 |
}) |
235 |
return context |
|
236 |
||
237 |
@cached |
|
238 |
def group_mapping(self): |
|
239 |
"""cached group mapping""" |
|
240 |
return ss.group_mapping(self.rqlcursor) |
|
1477 | 241 |
|
0 | 242 |
def exec_event_script(self, event, cubepath=None, funcname=None, |
1477 | 243 |
*args, **kwargs): |
0 | 244 |
if cubepath: |
245 |
apc = join(cubepath, 'migration', '%s.py' % event) |
|
246 |
else: |
|
247 |
apc = join(self.config.migration_scripts_dir(), '%s.py' % event) |
|
248 |
if exists(apc): |
|
249 |
if self.config.free_wheel: |
|
250 |
from cubicweb.server.hooks import setowner_after_add_entity |
|
251 |
self.repo.hm.unregister_hook(setowner_after_add_entity, |
|
252 |
'after_add_entity', '') |
|
253 |
self.deactivate_verification_hooks() |
|
254 |
self.info('executing %s', apc) |
|
255 |
confirm = self.confirm |
|
256 |
execscript_confirm = self.execscript_confirm |
|
257 |
self.confirm = yes |
|
258 |
self.execscript_confirm = yes |
|
259 |
try: |
|
260 |
return self.process_script(apc, funcname, *args, **kwargs) |
|
261 |
finally: |
|
262 |
self.confirm = confirm |
|
263 |
self.execscript_confirm = execscript_confirm |
|
264 |
if self.config.free_wheel: |
|
265 |
self.repo.hm.register_hook(setowner_after_add_entity, |
|
266 |
'after_add_entity', '') |
|
267 |
self.reactivate_verification_hooks() |
|
268 |
||
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
269 |
# schema synchronization internals ######################################## |
0 | 270 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
271 |
def _synchronize_permissions(self, ertype): |
0 | 272 |
"""permission synchronization for an entity or relation type""" |
273 |
if ertype in ('eid', 'has_text', 'identity'): |
|
274 |
return |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
275 |
newrschema = self.fs_schema[ertype] |
0 | 276 |
teid = self.repo.schema[ertype].eid |
277 |
if 'update' in newrschema.ACTIONS or newrschema.is_final(): |
|
278 |
# entity type |
|
279 |
exprtype = u'ERQLExpression' |
|
280 |
else: |
|
281 |
# relation type |
|
282 |
exprtype = u'RRQLExpression' |
|
283 |
assert teid, ertype |
|
284 |
gm = self.group_mapping() |
|
285 |
confirm = self.verbosity >= 2 |
|
286 |
# * remove possibly deprecated permission (eg in the persistent schema |
|
287 |
# but not in the new schema) |
|
288 |
# * synchronize existing expressions |
|
289 |
# * add new groups/expressions |
|
290 |
for action in newrschema.ACTIONS: |
|
291 |
perm = '%s_permission' % action |
|
292 |
# handle groups |
|
293 |
newgroups = list(newrschema.get_groups(action)) |
|
294 |
for geid, gname in self.rqlexec('Any G, GN WHERE T %s G, G name GN, ' |
|
295 |
'T eid %%(x)s' % perm, {'x': teid}, 'x', |
|
296 |
ask_confirm=False): |
|
297 |
if not gname in newgroups: |
|
298 |
if not confirm or self.confirm('remove %s permission of %s to %s?' |
|
299 |
% (action, ertype, gname)): |
|
300 |
self.rqlexec('DELETE T %s G WHERE G eid %%(x)s, T eid %s' |
|
301 |
% (perm, teid), |
|
302 |
{'x': geid}, 'x', ask_confirm=False) |
|
303 |
else: |
|
304 |
newgroups.remove(gname) |
|
305 |
for gname in newgroups: |
|
306 |
if not confirm or self.confirm('grant %s permission of %s to %s?' |
|
307 |
% (action, ertype, gname)): |
|
308 |
self.rqlexec('SET T %s G WHERE G eid %%(x)s, T eid %s' |
|
309 |
% (perm, teid), |
|
310 |
{'x': gm[gname]}, 'x', ask_confirm=False) |
|
311 |
# handle rql expressions |
|
312 |
newexprs = dict((expr.expression, expr) for expr in newrschema.get_rqlexprs(action)) |
|
313 |
for expreid, expression in self.rqlexec('Any E, EX WHERE T %s E, E expression EX, ' |
|
314 |
'T eid %s' % (perm, teid), |
|
315 |
ask_confirm=False): |
|
316 |
if not expression in newexprs: |
|
317 |
if not confirm or self.confirm('remove %s expression for %s permission of %s?' |
|
318 |
% (expression, action, ertype)): |
|
319 |
# deleting the relation will delete the expression entity |
|
320 |
self.rqlexec('DELETE T %s E WHERE E eid %%(x)s, T eid %s' |
|
321 |
% (perm, teid), |
|
322 |
{'x': expreid}, 'x', ask_confirm=False) |
|
323 |
else: |
|
324 |
newexprs.pop(expression) |
|
325 |
for expression in newexprs.values(): |
|
326 |
expr = expression.expression |
|
327 |
if not confirm or self.confirm('add %s expression for %s permission of %s?' |
|
328 |
% (expr, action, ertype)): |
|
329 |
self.rqlexec('INSERT RQLExpression X: X exprtype %%(exprtype)s, ' |
|
330 |
'X expression %%(expr)s, X mainvars %%(vars)s, T %s X ' |
|
331 |
'WHERE T eid %%(x)s' % perm, |
|
332 |
{'expr': expr, 'exprtype': exprtype, |
|
333 |
'vars': expression.mainvars, 'x': teid}, 'x', |
|
334 |
ask_confirm=False) |
|
1477 | 335 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
336 |
def _synchronize_rschema(self, rtype, syncrdefs=True, syncperms=True): |
0 | 337 |
"""synchronize properties of the persistent relation schema against its |
338 |
current definition: |
|
1477 | 339 |
|
0 | 340 |
* description |
341 |
* symetric, meta |
|
342 |
* inlined |
|
343 |
* relation definitions if `syncrdefs` |
|
344 |
* permissions if `syncperms` |
|
1477 | 345 |
|
0 | 346 |
physical schema changes should be handled by repository's schema hooks |
347 |
""" |
|
348 |
rtype = str(rtype) |
|
349 |
if rtype in self._synchronized: |
|
350 |
return |
|
351 |
self._synchronized.add(rtype) |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
352 |
rschema = self.fs_schema.rschema(rtype) |
0 | 353 |
self.rqlexecall(ss.updaterschema2rql(rschema), |
354 |
ask_confirm=self.verbosity>=2) |
|
355 |
reporschema = self.repo.schema.rschema(rtype) |
|
356 |
if syncrdefs: |
|
357 |
for subj, obj in rschema.iter_rdefs(): |
|
358 |
if not reporschema.has_rdef(subj, obj): |
|
359 |
continue |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
360 |
self._synchronize_rdef_schema(subj, rschema, obj) |
0 | 361 |
if syncperms: |
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
362 |
self._synchronize_permissions(rtype) |
1477 | 363 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
364 |
def _synchronize_eschema(self, etype, syncperms=True): |
0 | 365 |
"""synchronize properties of the persistent entity schema against |
366 |
its current definition: |
|
1477 | 367 |
|
0 | 368 |
* description |
369 |
* internationalizable, fulltextindexed, indexed, meta |
|
370 |
* relations from/to this entity |
|
371 |
* permissions if `syncperms` |
|
372 |
""" |
|
373 |
etype = str(etype) |
|
374 |
if etype in self._synchronized: |
|
375 |
return |
|
376 |
self._synchronized.add(etype) |
|
377 |
repoeschema = self.repo.schema.eschema(etype) |
|
378 |
try: |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
379 |
eschema = self.fs_schema.eschema(etype) |
0 | 380 |
except KeyError: |
381 |
return |
|
382 |
repospschema = repoeschema.specializes() |
|
383 |
espschema = eschema.specializes() |
|
384 |
if repospschema and not espschema: |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
385 |
self.rqlexec('DELETE X specializes Y WHERE X is CWEType, X name %(x)s', |
447 | 386 |
{'x': str(repoeschema)}) |
0 | 387 |
elif not repospschema and espschema: |
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
388 |
self.rqlexec('SET X specializes Y WHERE X is CWEType, X name %(x)s, ' |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
389 |
'Y is CWEType, Y name %(y)s', |
447 | 390 |
{'x': str(repoeschema), 'y': str(espschema)}) |
0 | 391 |
self.rqlexecall(ss.updateeschema2rql(eschema), |
392 |
ask_confirm=self.verbosity >= 2) |
|
393 |
for rschema, targettypes, x in eschema.relation_definitions(True): |
|
394 |
if x == 'subject': |
|
395 |
if not rschema in repoeschema.subject_relations(): |
|
396 |
continue |
|
397 |
subjtypes, objtypes = [etype], targettypes |
|
398 |
else: # x == 'object' |
|
399 |
if not rschema in repoeschema.object_relations(): |
|
400 |
continue |
|
401 |
subjtypes, objtypes = targettypes, [etype] |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
402 |
self._synchronize_rschema(rschema, syncperms=syncperms, |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
403 |
syncrdefs=False) |
0 | 404 |
reporschema = self.repo.schema.rschema(rschema) |
405 |
for subj in subjtypes: |
|
406 |
for obj in objtypes: |
|
407 |
if not reporschema.has_rdef(subj, obj): |
|
408 |
continue |
|
1414
448c9802d7df
does not apply any more
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1409
diff
changeset
|
409 |
self._synchronize_rdef_schema(subj, rschema, obj) |
0 | 410 |
if syncperms: |
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
411 |
self._synchronize_permissions(etype) |
0 | 412 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
413 |
def _synchronize_rdef_schema(self, subjtype, rtype, objtype): |
0 | 414 |
"""synchronize properties of the persistent relation definition schema |
415 |
against its current definition: |
|
416 |
* order and other properties |
|
417 |
* constraints |
|
418 |
""" |
|
419 |
subjtype, objtype = str(subjtype), str(objtype) |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
420 |
rschema = self.fs_schema.rschema(rtype) |
0 | 421 |
reporschema = self.repo.schema.rschema(rschema) |
422 |
if (subjtype, rschema, objtype) in self._synchronized: |
|
423 |
return |
|
424 |
self._synchronized.add((subjtype, rschema, objtype)) |
|
425 |
if rschema.symetric: |
|
426 |
self._synchronized.add((objtype, rschema, subjtype)) |
|
427 |
confirm = self.verbosity >= 2 |
|
428 |
# properties |
|
429 |
self.rqlexecall(ss.updaterdef2rql(rschema, subjtype, objtype), |
|
430 |
ask_confirm=confirm) |
|
431 |
# constraints |
|
432 |
newconstraints = list(rschema.rproperty(subjtype, objtype, 'constraints')) |
|
433 |
# 1. remove old constraints and update constraints of the same type |
|
434 |
# NOTE: don't use rschema.constraint_by_type because it may be |
|
435 |
# out of sync with newconstraints when multiple |
|
436 |
# constraints of the same type are used |
|
437 |
for cstr in reporschema.rproperty(subjtype, objtype, 'constraints'): |
|
438 |
for newcstr in newconstraints: |
|
439 |
if newcstr.type() == cstr.type(): |
|
440 |
break |
|
441 |
else: |
|
442 |
newcstr = None |
|
443 |
if newcstr is None: |
|
444 |
self.rqlexec('DELETE X constrained_by C WHERE C eid %(x)s', |
|
445 |
{'x': cstr.eid}, 'x', |
|
446 |
ask_confirm=confirm) |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
447 |
self.rqlexec('DELETE CWConstraint C WHERE C eid %(x)s', |
0 | 448 |
{'x': cstr.eid}, 'x', |
449 |
ask_confirm=confirm) |
|
450 |
else: |
|
451 |
newconstraints.remove(newcstr) |
|
452 |
values = {'x': cstr.eid, |
|
453 |
'v': unicode(newcstr.serialize())} |
|
454 |
self.rqlexec('SET X value %(v)s WHERE X eid %(x)s', |
|
455 |
values, 'x', ask_confirm=confirm) |
|
456 |
# 2. add new constraints |
|
457 |
for newcstr in newconstraints: |
|
458 |
self.rqlexecall(ss.constraint2rql(rschema, subjtype, objtype, |
|
459 |
newcstr), |
|
460 |
ask_confirm=confirm) |
|
1477 | 461 |
|
0 | 462 |
# base actions ############################################################ |
463 |
||
464 |
def checkpoint(self): |
|
465 |
"""checkpoint action""" |
|
466 |
if self.confirm('commit now ?', shell=False): |
|
467 |
self.commit() |
|
468 |
||
469 |
def cmd_add_cube(self, cube, update_database=True): |
|
676
270eb87a768a
provide a new add_cubes() migration function for cases where the new cubes are linked together by new relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
470 |
self.cmd_add_cubes( (cube,), update_database) |
1477 | 471 |
|
676
270eb87a768a
provide a new add_cubes() migration function for cases where the new cubes are linked together by new relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
472 |
def cmd_add_cubes(self, cubes, update_database=True): |
0 | 473 |
"""update_database is telling if the database schema should be updated |
474 |
or if only the relevant eproperty should be inserted (for the case where |
|
475 |
a cube has been extracted from an existing application, so the |
|
476 |
cube schema is already in there) |
|
477 |
""" |
|
676
270eb87a768a
provide a new add_cubes() migration function for cases where the new cubes are linked together by new relations
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
447
diff
changeset
|
478 |
newcubes = super(ServerMigrationHelper, self).cmd_add_cubes(cubes) |
0 | 479 |
if not newcubes: |
480 |
return |
|
481 |
for pack in newcubes: |
|
482 |
self.cmd_set_property('system.version.'+pack, |
|
483 |
self.config.cube_version(pack)) |
|
484 |
if not update_database: |
|
485 |
self.commit() |
|
486 |
return |
|
1036
593df4919845
when reading the schema while adding/removing cubes, read schema in non-strict mode
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
487 |
newcubes_schema = self.config.load_schema(construction_mode='non-strict') |
0 | 488 |
new = set() |
489 |
# execute pre-create files |
|
490 |
for pack in reversed(newcubes): |
|
491 |
self.exec_event_script('precreate', self.config.cube_dir(pack)) |
|
492 |
# add new entity and relation types |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
493 |
for rschema in newcubes_schema.relations(): |
0 | 494 |
if not rschema in self.repo.schema: |
495 |
self.cmd_add_relation_type(rschema.type) |
|
496 |
new.add(rschema.type) |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
497 |
for eschema in newcubes_schema.entities(): |
0 | 498 |
if not eschema in self.repo.schema: |
499 |
self.cmd_add_entity_type(eschema.type) |
|
500 |
new.add(eschema.type) |
|
501 |
# check if attributes has been added to existing entities |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
502 |
for rschema in newcubes_schema.relations(): |
0 | 503 |
existingschema = self.repo.schema.rschema(rschema.type) |
504 |
for (fromtype, totype) in rschema.iter_rdefs(): |
|
505 |
if existingschema.has_rdef(fromtype, totype): |
|
506 |
continue |
|
507 |
# check we should actually add the relation definition |
|
508 |
if not (fromtype in new or totype in new or rschema in new): |
|
509 |
continue |
|
1477 | 510 |
self.cmd_add_relation_definition(str(fromtype), rschema.type, |
0 | 511 |
str(totype)) |
512 |
# execute post-create files |
|
513 |
for pack in reversed(newcubes): |
|
514 |
self.exec_event_script('postcreate', self.config.cube_dir(pack)) |
|
1477 | 515 |
self.commit() |
516 |
||
0 | 517 |
def cmd_remove_cube(self, cube): |
518 |
removedcubes = super(ServerMigrationHelper, self).cmd_remove_cube(cube) |
|
519 |
if not removedcubes: |
|
520 |
return |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
521 |
fsschema = self.fs_schema |
1036
593df4919845
when reading the schema while adding/removing cubes, read schema in non-strict mode
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
522 |
removedcubes_schema = self.config.load_schema(construction_mode='non-strict') |
0 | 523 |
reposchema = self.repo.schema |
524 |
# execute pre-remove files |
|
525 |
for pack in reversed(removedcubes): |
|
526 |
self.exec_event_script('preremove', self.config.cube_dir(pack)) |
|
527 |
# remove cubes'entity and relation types |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
528 |
for rschema in fsschema.relations(): |
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
529 |
if not rschema in removedcubes_schema and rschema in reposchema: |
0 | 530 |
self.cmd_drop_relation_type(rschema.type) |
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
531 |
for eschema in fsschema.entities(): |
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
532 |
if not eschema in removedcubes_schema and eschema in reposchema: |
0 | 533 |
self.cmd_drop_entity_type(eschema.type) |
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
534 |
for rschema in fsschema.relations(): |
1477 | 535 |
if rschema in removedcubes_schema and rschema in reposchema: |
536 |
# check if attributes/relations has been added to entities from |
|
0 | 537 |
# other cubes |
538 |
for fromtype, totype in rschema.iter_rdefs(): |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
539 |
if not removedcubes_schema[rschema.type].has_rdef(fromtype, totype) and \ |
0 | 540 |
reposchema[rschema.type].has_rdef(fromtype, totype): |
541 |
self.cmd_drop_relation_definition( |
|
542 |
str(fromtype), rschema.type, str(totype)) |
|
543 |
# execute post-remove files |
|
544 |
for pack in reversed(removedcubes): |
|
545 |
self.exec_event_script('postremove', self.config.cube_dir(pack)) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
546 |
self.rqlexec('DELETE CWProperty X WHERE X pkey %(pk)s', |
0 | 547 |
{'pk': u'system.version.'+pack}, ask_confirm=False) |
548 |
self.commit() |
|
1477 | 549 |
|
0 | 550 |
# schema migration actions ################################################ |
1477 | 551 |
|
0 | 552 |
def cmd_add_attribute(self, etype, attrname, attrtype=None, commit=True): |
553 |
"""add a new attribute on the given entity type""" |
|
554 |
if attrtype is None: |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
555 |
rschema = self.fs_schema.rschema(attrname) |
0 | 556 |
attrtype = rschema.objects(etype)[0] |
557 |
self.cmd_add_relation_definition(etype, attrname, attrtype, commit=commit) |
|
1477 | 558 |
|
0 | 559 |
def cmd_drop_attribute(self, etype, attrname, commit=True): |
560 |
"""drop an existing attribute from the given entity type |
|
1477 | 561 |
|
0 | 562 |
`attrname` is a string giving the name of the attribute to drop |
563 |
""" |
|
564 |
rschema = self.repo.schema.rschema(attrname) |
|
565 |
attrtype = rschema.objects(etype)[0] |
|
566 |
self.cmd_drop_relation_definition(etype, attrname, attrtype, commit=commit) |
|
567 |
||
568 |
def cmd_rename_attribute(self, etype, oldname, newname, commit=True): |
|
569 |
"""rename an existing attribute of the given entity type |
|
1477 | 570 |
|
0 | 571 |
`oldname` is a string giving the name of the existing attribute |
572 |
`newname` is a string giving the name of the renamed attribute |
|
573 |
""" |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
574 |
eschema = self.fs_schema.eschema(etype) |
0 | 575 |
attrtype = eschema.destination(newname) |
576 |
# have to commit this first step anyway to get the definition |
|
577 |
# actually in the schema |
|
578 |
self.cmd_add_attribute(etype, newname, attrtype, commit=True) |
|
579 |
# skipp NULL values if the attribute is required |
|
580 |
rql = 'SET X %s VAL WHERE X is %s, X %s VAL' % (newname, etype, oldname) |
|
581 |
card = eschema.rproperty(newname, 'cardinality')[0] |
|
582 |
if card == '1': |
|
583 |
rql += ', NOT X %s NULL' % oldname |
|
584 |
self.rqlexec(rql, ask_confirm=self.verbosity>=2) |
|
585 |
self.cmd_drop_attribute(etype, oldname, commit=commit) |
|
1477 | 586 |
|
0 | 587 |
def cmd_add_entity_type(self, etype, auto=True, commit=True): |
588 |
"""register a new entity type |
|
1477 | 589 |
|
0 | 590 |
in auto mode, automatically register entity's relation where the |
591 |
targeted type is known |
|
592 |
""" |
|
593 |
applschema = self.repo.schema |
|
594 |
if etype in applschema: |
|
595 |
eschema = applschema[etype] |
|
596 |
if eschema.is_final(): |
|
597 |
applschema.del_entity_type(etype) |
|
598 |
else: |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
599 |
eschema = self.fs_schema.eschema(etype) |
0 | 600 |
confirm = self.verbosity >= 2 |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
601 |
# register the entity into CWEType |
0 | 602 |
self.rqlexecall(ss.eschema2rql(eschema), ask_confirm=confirm) |
603 |
# add specializes relation if needed |
|
604 |
self.rqlexecall(ss.eschemaspecialize2rql(eschema), ask_confirm=confirm) |
|
605 |
# register groups / permissions for the entity |
|
606 |
self.rqlexecall(ss.erperms2rql(eschema, self.group_mapping()), |
|
607 |
ask_confirm=confirm) |
|
608 |
# register entity's attributes |
|
609 |
for rschema, attrschema in eschema.attribute_definitions(): |
|
610 |
# ignore those meta relations, they will be automatically added |
|
611 |
if rschema.type in ('eid', 'creation_date', 'modification_date'): |
|
612 |
continue |
|
613 |
if not rschema.type in applschema: |
|
614 |
# need to add the relation type and to commit to get it |
|
615 |
# actually in the schema |
|
616 |
self.cmd_add_relation_type(rschema.type, False, commit=True) |
|
617 |
# register relation definition |
|
618 |
self.rqlexecall(ss.rdef2rql(rschema, etype, attrschema.type), |
|
619 |
ask_confirm=confirm) |
|
620 |
if auto: |
|
621 |
# we have commit here to get relation types actually in the schema |
|
622 |
self.commit() |
|
623 |
added = [] |
|
624 |
for rschema in eschema.subject_relations(): |
|
625 |
# attribute relation have already been processed and |
|
626 |
# 'owned_by'/'created_by' will be automatically added |
|
1477 | 627 |
if rschema.final or rschema.type in ('owned_by', 'created_by', 'is', 'is_instance_of'): |
0 | 628 |
continue |
629 |
rtypeadded = rschema.type in applschema |
|
630 |
for targetschema in rschema.objects(etype): |
|
631 |
# ignore relations where the targeted type is not in the |
|
632 |
# current application schema |
|
633 |
targettype = targetschema.type |
|
634 |
if not targettype in applschema and targettype != etype: |
|
635 |
continue |
|
636 |
if not rtypeadded: |
|
637 |
# need to add the relation type and to commit to get it |
|
638 |
# actually in the schema |
|
639 |
added.append(rschema.type) |
|
640 |
self.cmd_add_relation_type(rschema.type, False, commit=True) |
|
641 |
rtypeadded = True |
|
642 |
# register relation definition |
|
643 |
# remember this two avoid adding twice non symetric relation |
|
644 |
# such as "Emailthread forked_from Emailthread" |
|
645 |
added.append((etype, rschema.type, targettype)) |
|
646 |
self.rqlexecall(ss.rdef2rql(rschema, etype, targettype), |
|
647 |
ask_confirm=confirm) |
|
648 |
for rschema in eschema.object_relations(): |
|
649 |
rtypeadded = rschema.type in applschema or rschema.type in added |
|
650 |
for targetschema in rschema.subjects(etype): |
|
651 |
# ignore relations where the targeted type is not in the |
|
652 |
# current application schema |
|
653 |
targettype = targetschema.type |
|
654 |
# don't check targettype != etype since in this case the |
|
655 |
# relation has already been added as a subject relation |
|
656 |
if not targettype in applschema: |
|
657 |
continue |
|
658 |
if not rtypeadded: |
|
659 |
# need to add the relation type and to commit to get it |
|
660 |
# actually in the schema |
|
661 |
self.cmd_add_relation_type(rschema.type, False, commit=True) |
|
662 |
rtypeadded = True |
|
663 |
elif (targettype, rschema.type, etype) in added: |
|
664 |
continue |
|
665 |
# register relation definition |
|
666 |
self.rqlexecall(ss.rdef2rql(rschema, targettype, etype), |
|
667 |
ask_confirm=confirm) |
|
668 |
if commit: |
|
669 |
self.commit() |
|
1477 | 670 |
|
0 | 671 |
def cmd_drop_entity_type(self, etype, commit=True): |
672 |
"""unregister an existing entity type |
|
1477 | 673 |
|
0 | 674 |
This will trigger deletion of necessary relation types and definitions |
675 |
""" |
|
676 |
# XXX what if we delete an entity type which is specialized by other types |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
677 |
# unregister the entity from CWEType |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
678 |
self.rqlexec('DELETE CWEType X WHERE X name %(etype)s', {'etype': etype}, |
0 | 679 |
ask_confirm=self.verbosity>=2) |
680 |
if commit: |
|
681 |
self.commit() |
|
682 |
||
683 |
def cmd_rename_entity_type(self, oldname, newname, commit=True): |
|
684 |
"""rename an existing entity type in the persistent schema |
|
1477 | 685 |
|
0 | 686 |
`oldname` is a string giving the name of the existing entity type |
687 |
`newname` is a string giving the name of the renamed entity type |
|
688 |
""" |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
689 |
self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(oldname)s', |
0 | 690 |
{'newname' : unicode(newname), 'oldname' : oldname}) |
691 |
if commit: |
|
692 |
self.commit() |
|
1477 | 693 |
|
0 | 694 |
def cmd_add_relation_type(self, rtype, addrdef=True, commit=True): |
695 |
"""register a new relation type named `rtype`, as described in the |
|
696 |
schema description file. |
|
697 |
||
698 |
`addrdef` is a boolean value; when True, it will also add all relations |
|
699 |
of the type just added found in the schema definition file. Note that it |
|
700 |
implies an intermediate "commit" which commits the relation type |
|
701 |
creation (but not the relation definitions themselves, for which |
|
702 |
committing depends on the `commit` argument value). |
|
1477 | 703 |
|
0 | 704 |
""" |
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
705 |
rschema = self.fs_schema.rschema(rtype) |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
706 |
# register the relation into CWRType and insert necessary relation |
0 | 707 |
# definitions |
708 |
self.rqlexecall(ss.rschema2rql(rschema, addrdef=False), |
|
709 |
ask_confirm=self.verbosity>=2) |
|
710 |
# register groups / permissions for the relation |
|
711 |
self.rqlexecall(ss.erperms2rql(rschema, self.group_mapping()), |
|
712 |
ask_confirm=self.verbosity>=2) |
|
713 |
if addrdef: |
|
714 |
self.commit() |
|
715 |
self.rqlexecall(ss.rdef2rql(rschema), |
|
716 |
ask_confirm=self.verbosity>=2) |
|
717 |
if commit: |
|
718 |
self.commit() |
|
1477 | 719 |
|
0 | 720 |
def cmd_drop_relation_type(self, rtype, commit=True): |
721 |
"""unregister an existing relation type""" |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
722 |
# unregister the relation from CWRType |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
723 |
self.rqlexec('DELETE CWRType X WHERE X name %r' % rtype, |
0 | 724 |
ask_confirm=self.verbosity>=2) |
725 |
if commit: |
|
726 |
self.commit() |
|
1477 | 727 |
|
0 | 728 |
def cmd_rename_relation(self, oldname, newname, commit=True): |
729 |
"""rename an existing relation |
|
1477 | 730 |
|
0 | 731 |
`oldname` is a string giving the name of the existing relation |
732 |
`newname` is a string giving the name of the renamed relation |
|
733 |
""" |
|
734 |
self.cmd_add_relation_type(newname, commit=True) |
|
735 |
self.rqlexec('SET X %s Y WHERE X %s Y' % (newname, oldname), |
|
736 |
ask_confirm=self.verbosity>=2) |
|
737 |
self.cmd_drop_relation_type(oldname, commit=commit) |
|
738 |
||
739 |
def cmd_add_relation_definition(self, subjtype, rtype, objtype, commit=True): |
|
740 |
"""register a new relation definition, from its definition found in the |
|
741 |
schema definition file |
|
742 |
""" |
|
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
743 |
rschema = self.fs_schema.rschema(rtype) |
0 | 744 |
if not rtype in self.repo.schema: |
745 |
self.cmd_add_relation_type(rtype, addrdef=False, commit=True) |
|
746 |
self.rqlexecall(ss.rdef2rql(rschema, subjtype, objtype), |
|
747 |
ask_confirm=self.verbosity>=2) |
|
748 |
if commit: |
|
749 |
self.commit() |
|
1477 | 750 |
|
0 | 751 |
def cmd_drop_relation_definition(self, subjtype, rtype, objtype, commit=True): |
752 |
"""unregister an existing relation definition""" |
|
753 |
rschema = self.repo.schema.rschema(rtype) |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
754 |
# unregister the definition from CWAttribute or CWRelation |
0 | 755 |
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
|
756 |
etype = 'CWAttribute' |
0 | 757 |
else: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
758 |
etype = 'CWRelation' |
0 | 759 |
rql = ('DELETE %s X WHERE X from_entity FE, FE name "%s",' |
760 |
'X relation_type RT, RT name "%s", X to_entity TE, TE name "%s"') |
|
761 |
self.rqlexec(rql % (etype, subjtype, rtype, objtype), |
|
762 |
ask_confirm=self.verbosity>=2) |
|
763 |
if commit: |
|
764 |
self.commit() |
|
1477 | 765 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
766 |
def cmd_sync_schema_props_perms(self, ertype=None, syncperms=True, |
1409
f4dee84a618f
sql attributes bugfix
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1399
diff
changeset
|
767 |
syncprops=True, syncrdefs=True, commit=True): |
0 | 768 |
"""synchronize the persistent schema against the current definition |
769 |
schema. |
|
1477 | 770 |
|
0 | 771 |
It will synch common stuff between the definition schema and the |
772 |
actual persistent schema, it won't add/remove any entity or relation. |
|
773 |
""" |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
774 |
assert syncperms or syncprops, 'nothing to do' |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
775 |
if ertype is not None: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
776 |
if isinstance(ertype, (tuple, list)): |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
777 |
assert len(ertype) == 3, 'not a relation definition' |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
778 |
assert syncprops, 'can\'t update permission for a relation definition' |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
779 |
self._synchronize_rdef_schema(*ertype) |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
780 |
elif syncprops: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
781 |
erschema = self.repo.schema[ertype] |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
782 |
if isinstance(erschema, CubicWebRelationSchema): |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
783 |
self._synchronize_rschema(erschema, syncperms=syncperms, |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
784 |
syncrdefs=syncrdefs) |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
785 |
else: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
786 |
self._synchronize_eschema(erschema, syncperms=syncperms) |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
787 |
else: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
788 |
self._synchronize_permissions(ertype) |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
789 |
else: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
790 |
for etype in self.repo.schema.entities(): |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
791 |
if syncprops: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
792 |
self._synchronize_eschema(etype, syncperms=syncperms) |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
793 |
else: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
794 |
self._synchronize_permissions(etype) |
0 | 795 |
if commit: |
796 |
self.commit() |
|
1477 | 797 |
|
0 | 798 |
def cmd_change_relation_props(self, subjtype, rtype, objtype, |
799 |
commit=True, **kwargs): |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
800 |
"""change some properties of a relation definition |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
801 |
|
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
802 |
you usually want to use sync_schema_props_perms instead. |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
803 |
""" |
0 | 804 |
assert kwargs |
805 |
restriction = [] |
|
806 |
if subjtype and subjtype != 'Any': |
|
807 |
restriction.append('X from_entity FE, FE name "%s"' % subjtype) |
|
808 |
if objtype and objtype != 'Any': |
|
809 |
restriction.append('X to_entity TE, TE name "%s"' % objtype) |
|
810 |
if rtype and rtype != 'Any': |
|
811 |
restriction.append('X relation_type RT, RT name "%s"' % rtype) |
|
812 |
assert restriction |
|
813 |
values = [] |
|
814 |
for k, v in kwargs.items(): |
|
815 |
values.append('X %s %%(%s)s' % (k, k)) |
|
816 |
if isinstance(v, str): |
|
817 |
kwargs[k] = unicode(v) |
|
818 |
rql = 'SET %s WHERE %s' % (','.join(values), ','.join(restriction)) |
|
819 |
self.rqlexec(rql, kwargs, ask_confirm=self.verbosity>=2) |
|
820 |
if commit: |
|
821 |
self.commit() |
|
822 |
||
823 |
def cmd_set_size_constraint(self, etype, rtype, size, commit=True): |
|
824 |
"""set change size constraint of a string attribute |
|
825 |
||
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
826 |
if size is None any size constraint will be removed. |
1477 | 827 |
|
828 |
you usually want to use sync_schema_props_perms instead. |
|
0 | 829 |
""" |
830 |
oldvalue = None |
|
831 |
for constr in self.repo.schema.eschema(etype).constraints(rtype): |
|
832 |
if isinstance(constr, SizeConstraint): |
|
833 |
oldvalue = constr.max |
|
834 |
if oldvalue == size: |
|
835 |
return |
|
836 |
if oldvalue is None and not size is None: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
837 |
ceid = self.rqlexec('INSERT CWConstraint C: C value %(v)s, C cstrtype CT ' |
0 | 838 |
'WHERE CT name "SizeConstraint"', |
839 |
{'v': SizeConstraint(size).serialize()}, |
|
840 |
ask_confirm=self.verbosity>=2)[0][0] |
|
841 |
self.rqlexec('SET X constrained_by C WHERE X from_entity S, X relation_type R, ' |
|
842 |
'S name "%s", R name "%s", C eid %s' % (etype, rtype, ceid), |
|
843 |
ask_confirm=self.verbosity>=2) |
|
844 |
elif not oldvalue is None: |
|
845 |
if not size is None: |
|
846 |
self.rqlexec('SET C value %%(v)s WHERE X from_entity S, X relation_type R,' |
|
847 |
'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
|
848 |
'S name "%s", R name "%s"' % (etype, rtype), |
|
849 |
{'v': unicode(SizeConstraint(size).serialize())}, |
|
850 |
ask_confirm=self.verbosity>=2) |
|
851 |
else: |
|
852 |
self.rqlexec('DELETE X constrained_by C WHERE X from_entity S, X relation_type R,' |
|
853 |
'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
|
854 |
'S name "%s", R name "%s"' % (etype, rtype), |
|
855 |
ask_confirm=self.verbosity>=2) |
|
856 |
# cleanup unused constraints |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
857 |
self.rqlexec('DELETE CWConstraint C WHERE NOT X constrained_by C') |
0 | 858 |
if commit: |
859 |
self.commit() |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
860 |
|
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
861 |
@obsolete('use sync_schema_props_perms(ertype, syncprops=False)') |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
862 |
def cmd_synchronize_permissions(self, ertype, commit=True): |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
863 |
self.cmd_sync_schema_props_perms(ertype, syncprops=False, commit=commit) |
1477 | 864 |
|
0 | 865 |
# Workflows handling ###################################################### |
1477 | 866 |
|
0 | 867 |
def cmd_add_state(self, name, stateof, initial=False, commit=False, **kwargs): |
868 |
"""method to ease workflow definition: add a state for one or more |
|
869 |
entity type(s) |
|
870 |
""" |
|
871 |
stateeid = self.cmd_add_entity('State', name=name, **kwargs) |
|
872 |
if not isinstance(stateof, (list, tuple)): |
|
873 |
stateof = (stateof,) |
|
874 |
for etype in stateof: |
|
875 |
# XXX ensure etype validity |
|
876 |
self.rqlexec('SET X state_of Y WHERE X eid %(x)s, Y name %(et)s', |
|
877 |
{'x': stateeid, 'et': etype}, 'x', ask_confirm=False) |
|
878 |
if initial: |
|
879 |
self.rqlexec('SET ET initial_state S WHERE ET name %(et)s, S eid %(x)s', |
|
880 |
{'x': stateeid, 'et': etype}, 'x', ask_confirm=False) |
|
881 |
if commit: |
|
882 |
self.commit() |
|
883 |
return stateeid |
|
1477 | 884 |
|
0 | 885 |
def cmd_add_transition(self, name, transitionof, fromstates, tostate, |
886 |
requiredgroups=(), conditions=(), commit=False, **kwargs): |
|
887 |
"""method to ease workflow definition: add a transition for one or more |
|
888 |
entity type(s), from one or more state and to a single state |
|
889 |
""" |
|
890 |
treid = self.cmd_add_entity('Transition', name=name, **kwargs) |
|
891 |
if not isinstance(transitionof, (list, tuple)): |
|
892 |
transitionof = (transitionof,) |
|
893 |
for etype in transitionof: |
|
894 |
# XXX ensure etype validity |
|
895 |
self.rqlexec('SET X transition_of Y WHERE X eid %(x)s, Y name %(et)s', |
|
896 |
{'x': treid, 'et': etype}, 'x', ask_confirm=False) |
|
897 |
for stateeid in fromstates: |
|
898 |
self.rqlexec('SET X allowed_transition Y WHERE X eid %(x)s, Y eid %(y)s', |
|
899 |
{'x': stateeid, 'y': treid}, 'x', ask_confirm=False) |
|
900 |
self.rqlexec('SET X destination_state Y WHERE X eid %(x)s, Y eid %(y)s', |
|
901 |
{'x': treid, 'y': tostate}, 'x', ask_confirm=False) |
|
902 |
self.cmd_set_transition_permissions(treid, requiredgroups, conditions, |
|
903 |
reset=False) |
|
904 |
if commit: |
|
905 |
self.commit() |
|
906 |
return treid |
|
907 |
||
908 |
def cmd_set_transition_permissions(self, treid, |
|
909 |
requiredgroups=(), conditions=(), |
|
910 |
reset=True, commit=False): |
|
911 |
"""set or add (if `reset` is False) groups and conditions for a |
|
912 |
transition |
|
913 |
""" |
|
914 |
if reset: |
|
915 |
self.rqlexec('DELETE T require_group G WHERE T eid %(x)s', |
|
916 |
{'x': treid}, 'x', ask_confirm=False) |
|
917 |
self.rqlexec('DELETE T condition R WHERE T eid %(x)s', |
|
918 |
{'x': treid}, 'x', ask_confirm=False) |
|
919 |
for gname in requiredgroups: |
|
920 |
### XXX ensure gname validity |
|
921 |
self.rqlexec('SET T require_group G WHERE T eid %(x)s, G name %(gn)s', |
|
922 |
{'x': treid, 'gn': gname}, 'x', ask_confirm=False) |
|
923 |
if isinstance(conditions, basestring): |
|
924 |
conditions = (conditions,) |
|
925 |
for expr in conditions: |
|
926 |
if isinstance(expr, str): |
|
927 |
expr = unicode(expr) |
|
928 |
self.rqlexec('INSERT RQLExpression X: X exprtype "ERQLExpression", ' |
|
929 |
'X expression %(expr)s, T condition X ' |
|
930 |
'WHERE T eid %(x)s', |
|
931 |
{'x': treid, 'expr': expr}, 'x', ask_confirm=False) |
|
932 |
if commit: |
|
933 |
self.commit() |
|
934 |
||
972 | 935 |
def cmd_set_state(self, eid, statename, commit=False): |
936 |
self.session.set_pool() # ensure pool is set |
|
937 |
entity = self.session.eid_rset(eid).get_entity(0, 0) |
|
938 |
entity.change_state(entity.wf_state(statename).eid) |
|
939 |
if commit: |
|
940 |
self.commit() |
|
1477 | 941 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
942 |
# CWProperty handling ###################################################### |
0 | 943 |
|
944 |
def cmd_property_value(self, pkey): |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
945 |
rql = 'Any V WHERE X is CWProperty, X pkey %(k)s, X value V' |
0 | 946 |
rset = self.rqlexec(rql, {'k': pkey}, ask_confirm=False) |
947 |
return rset[0][0] |
|
948 |
||
949 |
def cmd_set_property(self, pkey, value): |
|
950 |
value = unicode(value) |
|
951 |
try: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
952 |
prop = self.rqlexec('CWProperty X WHERE X pkey %(k)s', {'k': pkey}, |
0 | 953 |
ask_confirm=False).get_entity(0, 0) |
954 |
except: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
955 |
self.cmd_add_entity('CWProperty', pkey=unicode(pkey), value=value) |
0 | 956 |
else: |
957 |
self.rqlexec('SET X value %(v)s WHERE X pkey %(k)s', |
|
958 |
{'k': pkey, 'v': value}, ask_confirm=False) |
|
959 |
||
960 |
# other data migration commands ########################################### |
|
1477 | 961 |
|
0 | 962 |
def cmd_add_entity(self, etype, *args, **kwargs): |
963 |
"""add a new entity of the given type""" |
|
964 |
rql = 'INSERT %s X' % etype |
|
965 |
relations = [] |
|
966 |
restrictions = [] |
|
967 |
for rtype, rvar in args: |
|
968 |
relations.append('X %s %s' % (rtype, rvar)) |
|
969 |
restrictions.append('%s eid %s' % (rvar, kwargs.pop(rvar))) |
|
970 |
commit = kwargs.pop('commit', False) |
|
971 |
for attr in kwargs: |
|
972 |
relations.append('X %s %%(%s)s' % (attr, attr)) |
|
973 |
if relations: |
|
974 |
rql = '%s: %s' % (rql, ', '.join(relations)) |
|
975 |
if restrictions: |
|
976 |
rql = '%s WHERE %s' % (rql, ', '.join(restrictions)) |
|
977 |
eid = self.rqlexec(rql, kwargs, ask_confirm=self.verbosity>=2).rows[0][0] |
|
978 |
if commit: |
|
979 |
self.commit() |
|
980 |
return eid |
|
1477 | 981 |
|
0 | 982 |
def sqlexec(self, sql, args=None, ask_confirm=True): |
983 |
"""execute the given sql if confirmed |
|
1477 | 984 |
|
0 | 985 |
should only be used for low level stuff undoable with existing higher |
986 |
level actions |
|
987 |
""" |
|
988 |
if not ask_confirm or self.confirm('execute sql: %s ?' % sql): |
|
989 |
self.session.set_pool() # ensure pool is set |
|
990 |
try: |
|
991 |
cu = self.session.system_sql(sql, args) |
|
992 |
except: |
|
993 |
ex = sys.exc_info()[1] |
|
994 |
if self.confirm('error: %s\nabort?' % ex): |
|
995 |
raise |
|
996 |
return |
|
997 |
try: |
|
998 |
return cu.fetchall() |
|
999 |
except: |
|
1000 |
# no result to fetch |
|
1001 |
return |
|
1477 | 1002 |
|
0 | 1003 |
def rqlexec(self, rql, kwargs=None, cachekey=None, ask_confirm=True): |
1004 |
"""rql action""" |
|
1005 |
if not isinstance(rql, (tuple, list)): |
|
1006 |
rql = ( (rql, kwargs), ) |
|
1007 |
res = None |
|
1008 |
for rql, kwargs in rql: |
|
1009 |
if kwargs: |
|
1010 |
msg = '%s (%s)' % (rql, kwargs) |
|
1011 |
else: |
|
1012 |
msg = rql |
|
1013 |
if not ask_confirm or self.confirm('execute rql: %s ?' % msg): |
|
1014 |
try: |
|
1015 |
res = self.rqlcursor.execute(rql, kwargs, cachekey) |
|
1016 |
except Exception, ex: |
|
1017 |
if self.confirm('error: %s\nabort?' % ex): |
|
1018 |
raise |
|
1019 |
return res |
|
1020 |
||
1021 |
def rqliter(self, rql, kwargs=None, ask_confirm=True): |
|
1022 |
return ForRqlIterator(self, rql, None, ask_confirm) |
|
1023 |
||
1024 |
def cmd_deactivate_verification_hooks(self): |
|
1025 |
self.repo.hm.deactivate_verification_hooks() |
|
1026 |
||
1027 |
def cmd_reactivate_verification_hooks(self): |
|
1028 |
self.repo.hm.reactivate_verification_hooks() |
|
1477 | 1029 |
|
0 | 1030 |
# broken db commands ###################################################### |
1031 |
||
1032 |
def cmd_change_attribute_type(self, etype, attr, newtype, commit=True): |
|
1033 |
"""low level method to change the type of an entity attribute. This is |
|
1034 |
a quick hack which has some drawback: |
|
1035 |
* only works when the old type can be changed to the new type by the |
|
1036 |
underlying rdbms (eg using ALTER TABLE) |
|
1037 |
* the actual schema won't be updated until next startup |
|
1038 |
""" |
|
1039 |
rschema = self.repo.schema.rschema(attr) |
|
1040 |
oldtype = rschema.objects(etype)[0] |
|
1041 |
rdefeid = rschema.rproperty(etype, oldtype, 'eid') |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1042 |
sql = ("UPDATE CWAttribute " |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1043 |
"SET to_entity=(SELECT eid FROM CWEType WHERE name='%s')" |
0 | 1044 |
"WHERE eid=%s") % (newtype, rdefeid) |
1045 |
self.sqlexec(sql, ask_confirm=False) |
|
1046 |
dbhelper = self.repo.system_source.dbhelper |
|
1047 |
sqltype = dbhelper.TYPE_MAPPING[newtype] |
|
1048 |
sql = 'ALTER TABLE %s ALTER COLUMN %s TYPE %s' % (etype, attr, sqltype) |
|
1049 |
self.sqlexec(sql, ask_confirm=False) |
|
1050 |
if commit: |
|
1051 |
self.commit() |
|
1477 | 1052 |
|
0 | 1053 |
def cmd_add_entity_type_table(self, etype, commit=True): |
1054 |
"""low level method to create the sql table for an existing entity. |
|
1055 |
This may be useful on accidental desync between the repository schema |
|
1056 |
and a sql database |
|
1057 |
""" |
|
1058 |
dbhelper = self.repo.system_source.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:
1240
diff
changeset
|
1059 |
tablesql = eschema2sql(dbhelper, self.repo.schema.eschema(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:
1240
diff
changeset
|
1060 |
prefix=SQL_PREFIX) |
0 | 1061 |
for sql in tablesql.split(';'): |
1062 |
if sql.strip(): |
|
1063 |
self.sqlexec(sql) |
|
1064 |
if commit: |
|
1065 |
self.commit() |
|
1477 | 1066 |
|
0 | 1067 |
def cmd_add_relation_type_table(self, rtype, commit=True): |
1068 |
"""low level method to create the sql table for an existing relation. |
|
1069 |
This may be useful on accidental desync between the repository schema |
|
1070 |
and a sql database |
|
1071 |
""" |
|
1072 |
dbhelper = self.repo.system_source.dbhelper |
|
1073 |
tablesql = rschema2sql(dbhelper, self.repo.schema.rschema(rtype)) |
|
1074 |
for sql in tablesql.split(';'): |
|
1075 |
if sql.strip(): |
|
1076 |
self.sqlexec(sql) |
|
1077 |
if commit: |
|
1078 |
self.commit() |
|
1477 | 1079 |
|
0 | 1080 |
|
1081 |
class ForRqlIterator: |
|
1082 |
"""specific rql iterator to make the loop skipable""" |
|
1083 |
def __init__(self, helper, rql, kwargs, ask_confirm): |
|
1084 |
self._h = helper |
|
1085 |
self.rql = rql |
|
1086 |
self.kwargs = kwargs |
|
1087 |
self.ask_confirm = ask_confirm |
|
1088 |
self._rsetit = None |
|
1477 | 1089 |
|
0 | 1090 |
def __iter__(self): |
1091 |
return self |
|
1477 | 1092 |
|
0 | 1093 |
def next(self): |
1094 |
if self._rsetit is not None: |
|
1095 |
return self._rsetit.next() |
|
1096 |
rql, kwargs = self.rql, self.kwargs |
|
1097 |
if kwargs: |
|
1098 |
msg = '%s (%s)' % (rql, kwargs) |
|
1099 |
else: |
|
1100 |
msg = rql |
|
1101 |
if self.ask_confirm: |
|
1102 |
if not self._h.confirm('execute rql: %s ?' % msg): |
|
1103 |
raise StopIteration |
|
1104 |
try: |
|
1105 |
rset = self._h.rqlcursor.execute(rql, kwargs) |
|
1106 |
except Exception, ex: |
|
1107 |
if self._h.confirm('error: %s\nabort?' % ex): |
|
1108 |
raise |
|
1109 |
else: |
|
1110 |
raise StopIteration |
|
1111 |
self._rsetit = iter(rset) |
|
1112 |
return self._rsetit.next() |