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