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