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