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