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