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