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