author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 11 Aug 2009 17:13:32 +0200 | |
changeset 2773 | b2530e3e0afb |
parent 2700 | ecf888c8a250 |
child 2759 | 23d7a75693f8 |
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 |
2617
89c62b855f2e
[R schema hooks] rename META_RELATIONS_TYPES into META_RTYPES, use it and other schema consts to avoid errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
33 |
from cubicweb.schema import META_RTYPES, 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 |
|
2617
89c62b855f2e
[R schema hooks] rename META_RELATIONS_TYPES into META_RTYPES, use it and other schema consts to avoid errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
590 |
if rschema.type in META_RTYPES: |
0 | 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 |
|
2617
89c62b855f2e
[R schema hooks] rename META_RELATIONS_TYPES into META_RTYPES, use it and other schema consts to avoid errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2616
diff
changeset
|
606 |
if rschema.final or rschema.type in META_RTYPES: |
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) |
|
2700
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
697 |
if rtype in META_RTYPES: |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
698 |
# if the relation is in META_RTYPES, ensure we're adding it for |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
699 |
# all entity types *in the persistent schema*, not only those in |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
700 |
# the fs schema |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
701 |
for etype in self.repo.schema.entities(): |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
702 |
if not etype in self.fs_schema: |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
703 |
# get sample object type and rproperties |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
704 |
objtypes = rschema.objects() |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
705 |
assert len(objtypes) == 1 |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
706 |
objtype = objtypes[0] |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
707 |
props = rschema.rproperties( |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
708 |
rschema.subjects(objtype)[0], objtype) |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
709 |
assert props |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
710 |
self.rqlexecall(ss.rdef2rql(rschema, etype, objtype, props), |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
711 |
ask_confirm=self.verbosity>=2) |
ecf888c8a250
[migration] when adding a core meta-relation, it should be added to all entities *in the persistent schema*, not only those in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2680
diff
changeset
|
712 |
|
0 | 713 |
if commit: |
714 |
self.commit() |
|
1477 | 715 |
|
0 | 716 |
def cmd_drop_relation_type(self, rtype, commit=True): |
717 |
"""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
|
718 |
# 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
|
719 |
self.rqlexec('DELETE CWRType X WHERE X name %r' % rtype, |
0 | 720 |
ask_confirm=self.verbosity>=2) |
721 |
if commit: |
|
722 |
self.commit() |
|
1477 | 723 |
|
0 | 724 |
def cmd_rename_relation(self, oldname, newname, commit=True): |
725 |
"""rename an existing relation |
|
1477 | 726 |
|
0 | 727 |
`oldname` is a string giving the name of the existing relation |
728 |
`newname` is a string giving the name of the renamed relation |
|
729 |
""" |
|
730 |
self.cmd_add_relation_type(newname, commit=True) |
|
731 |
self.rqlexec('SET X %s Y WHERE X %s Y' % (newname, oldname), |
|
732 |
ask_confirm=self.verbosity>=2) |
|
733 |
self.cmd_drop_relation_type(oldname, commit=commit) |
|
734 |
||
735 |
def cmd_add_relation_definition(self, subjtype, rtype, objtype, commit=True): |
|
736 |
"""register a new relation definition, from its definition found in the |
|
737 |
schema definition file |
|
738 |
""" |
|
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
|
739 |
rschema = self.fs_schema.rschema(rtype) |
0 | 740 |
if not rtype in self.repo.schema: |
741 |
self.cmd_add_relation_type(rtype, addrdef=False, commit=True) |
|
742 |
self.rqlexecall(ss.rdef2rql(rschema, subjtype, objtype), |
|
743 |
ask_confirm=self.verbosity>=2) |
|
744 |
if commit: |
|
745 |
self.commit() |
|
1477 | 746 |
|
0 | 747 |
def cmd_drop_relation_definition(self, subjtype, rtype, objtype, commit=True): |
748 |
"""unregister an existing relation definition""" |
|
749 |
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
|
750 |
# unregister the definition from CWAttribute or CWRelation |
0 | 751 |
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
|
752 |
etype = 'CWAttribute' |
0 | 753 |
else: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
754 |
etype = 'CWRelation' |
0 | 755 |
rql = ('DELETE %s X WHERE X from_entity FE, FE name "%s",' |
756 |
'X relation_type RT, RT name "%s", X to_entity TE, TE name "%s"') |
|
757 |
self.rqlexec(rql % (etype, subjtype, rtype, objtype), |
|
758 |
ask_confirm=self.verbosity>=2) |
|
759 |
if commit: |
|
760 |
self.commit() |
|
1477 | 761 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
762 |
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
|
763 |
syncprops=True, syncrdefs=True, commit=True): |
0 | 764 |
"""synchronize the persistent schema against the current definition |
765 |
schema. |
|
1477 | 766 |
|
0 | 767 |
It will synch common stuff between the definition schema and the |
768 |
actual persistent schema, it won't add/remove any entity or relation. |
|
769 |
""" |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
770 |
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
|
771 |
if ertype is not None: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
772 |
if isinstance(ertype, (tuple, list)): |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
773 |
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
|
774 |
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
|
775 |
self._synchronize_rdef_schema(*ertype) |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
776 |
elif syncprops: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
777 |
erschema = self.repo.schema[ertype] |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
778 |
if isinstance(erschema, CubicWebRelationSchema): |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
779 |
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
|
780 |
syncrdefs=syncrdefs) |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
781 |
else: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
782 |
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
|
783 |
else: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
784 |
self._synchronize_permissions(ertype) |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
785 |
else: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
786 |
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
|
787 |
if syncprops: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
788 |
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
|
789 |
else: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
790 |
self._synchronize_permissions(etype) |
0 | 791 |
if commit: |
792 |
self.commit() |
|
1477 | 793 |
|
0 | 794 |
def cmd_change_relation_props(self, subjtype, rtype, objtype, |
795 |
commit=True, **kwargs): |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
796 |
"""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
|
797 |
|
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
798 |
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
|
799 |
""" |
0 | 800 |
assert kwargs |
801 |
restriction = [] |
|
802 |
if subjtype and subjtype != 'Any': |
|
803 |
restriction.append('X from_entity FE, FE name "%s"' % subjtype) |
|
804 |
if objtype and objtype != 'Any': |
|
805 |
restriction.append('X to_entity TE, TE name "%s"' % objtype) |
|
806 |
if rtype and rtype != 'Any': |
|
807 |
restriction.append('X relation_type RT, RT name "%s"' % rtype) |
|
808 |
assert restriction |
|
809 |
values = [] |
|
810 |
for k, v in kwargs.items(): |
|
811 |
values.append('X %s %%(%s)s' % (k, k)) |
|
812 |
if isinstance(v, str): |
|
813 |
kwargs[k] = unicode(v) |
|
814 |
rql = 'SET %s WHERE %s' % (','.join(values), ','.join(restriction)) |
|
815 |
self.rqlexec(rql, kwargs, ask_confirm=self.verbosity>=2) |
|
816 |
if commit: |
|
817 |
self.commit() |
|
818 |
||
819 |
def cmd_set_size_constraint(self, etype, rtype, size, commit=True): |
|
820 |
"""set change size constraint of a string attribute |
|
821 |
||
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
822 |
if size is None any size constraint will be removed. |
1477 | 823 |
|
824 |
you usually want to use sync_schema_props_perms instead. |
|
0 | 825 |
""" |
826 |
oldvalue = None |
|
827 |
for constr in self.repo.schema.eschema(etype).constraints(rtype): |
|
828 |
if isinstance(constr, SizeConstraint): |
|
829 |
oldvalue = constr.max |
|
830 |
if oldvalue == size: |
|
831 |
return |
|
832 |
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
|
833 |
ceid = self.rqlexec('INSERT CWConstraint C: C value %(v)s, C cstrtype CT ' |
0 | 834 |
'WHERE CT name "SizeConstraint"', |
835 |
{'v': SizeConstraint(size).serialize()}, |
|
836 |
ask_confirm=self.verbosity>=2)[0][0] |
|
837 |
self.rqlexec('SET X constrained_by C WHERE X from_entity S, X relation_type R, ' |
|
838 |
'S name "%s", R name "%s", C eid %s' % (etype, rtype, ceid), |
|
839 |
ask_confirm=self.verbosity>=2) |
|
840 |
elif not oldvalue is None: |
|
841 |
if not size is None: |
|
842 |
self.rqlexec('SET C value %%(v)s WHERE X from_entity S, X relation_type R,' |
|
843 |
'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
|
844 |
'S name "%s", R name "%s"' % (etype, rtype), |
|
845 |
{'v': unicode(SizeConstraint(size).serialize())}, |
|
846 |
ask_confirm=self.verbosity>=2) |
|
847 |
else: |
|
848 |
self.rqlexec('DELETE X constrained_by C WHERE X from_entity S, X relation_type R,' |
|
849 |
'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
|
850 |
'S name "%s", R name "%s"' % (etype, rtype), |
|
851 |
ask_confirm=self.verbosity>=2) |
|
852 |
# cleanup unused constraints |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
853 |
self.rqlexec('DELETE CWConstraint C WHERE NOT X constrained_by C') |
0 | 854 |
if commit: |
855 |
self.commit() |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
856 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2493
diff
changeset
|
857 |
@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
|
858 |
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
|
859 |
self.cmd_sync_schema_props_perms(ertype, syncprops=False, commit=commit) |
1477 | 860 |
|
0 | 861 |
# Workflows handling ###################################################### |
1477 | 862 |
|
0 | 863 |
def cmd_add_state(self, name, stateof, initial=False, commit=False, **kwargs): |
864 |
"""method to ease workflow definition: add a state for one or more |
|
865 |
entity type(s) |
|
866 |
""" |
|
867 |
stateeid = self.cmd_add_entity('State', name=name, **kwargs) |
|
868 |
if not isinstance(stateof, (list, tuple)): |
|
869 |
stateof = (stateof,) |
|
870 |
for etype in stateof: |
|
871 |
# XXX ensure etype validity |
|
872 |
self.rqlexec('SET X state_of Y WHERE X eid %(x)s, Y name %(et)s', |
|
873 |
{'x': stateeid, 'et': etype}, 'x', ask_confirm=False) |
|
874 |
if initial: |
|
875 |
self.rqlexec('SET ET initial_state S WHERE ET name %(et)s, S eid %(x)s', |
|
876 |
{'x': stateeid, 'et': etype}, 'x', ask_confirm=False) |
|
877 |
if commit: |
|
878 |
self.commit() |
|
879 |
return stateeid |
|
1477 | 880 |
|
0 | 881 |
def cmd_add_transition(self, name, transitionof, fromstates, tostate, |
882 |
requiredgroups=(), conditions=(), commit=False, **kwargs): |
|
883 |
"""method to ease workflow definition: add a transition for one or more |
|
884 |
entity type(s), from one or more state and to a single state |
|
885 |
""" |
|
886 |
treid = self.cmd_add_entity('Transition', name=name, **kwargs) |
|
887 |
if not isinstance(transitionof, (list, tuple)): |
|
888 |
transitionof = (transitionof,) |
|
889 |
for etype in transitionof: |
|
890 |
# XXX ensure etype validity |
|
891 |
self.rqlexec('SET X transition_of Y WHERE X eid %(x)s, Y name %(et)s', |
|
892 |
{'x': treid, 'et': etype}, 'x', ask_confirm=False) |
|
893 |
for stateeid in fromstates: |
|
894 |
self.rqlexec('SET X allowed_transition Y WHERE X eid %(x)s, Y eid %(y)s', |
|
895 |
{'x': stateeid, 'y': treid}, 'x', ask_confirm=False) |
|
896 |
self.rqlexec('SET X destination_state Y WHERE X eid %(x)s, Y eid %(y)s', |
|
897 |
{'x': treid, 'y': tostate}, 'x', ask_confirm=False) |
|
898 |
self.cmd_set_transition_permissions(treid, requiredgroups, conditions, |
|
899 |
reset=False) |
|
900 |
if commit: |
|
901 |
self.commit() |
|
902 |
return treid |
|
903 |
||
904 |
def cmd_set_transition_permissions(self, treid, |
|
905 |
requiredgroups=(), conditions=(), |
|
906 |
reset=True, commit=False): |
|
907 |
"""set or add (if `reset` is False) groups and conditions for a |
|
908 |
transition |
|
909 |
""" |
|
910 |
if reset: |
|
911 |
self.rqlexec('DELETE T require_group G WHERE T eid %(x)s', |
|
912 |
{'x': treid}, 'x', ask_confirm=False) |
|
913 |
self.rqlexec('DELETE T condition R WHERE T eid %(x)s', |
|
914 |
{'x': treid}, 'x', ask_confirm=False) |
|
915 |
for gname in requiredgroups: |
|
916 |
### XXX ensure gname validity |
|
917 |
self.rqlexec('SET T require_group G WHERE T eid %(x)s, G name %(gn)s', |
|
918 |
{'x': treid, 'gn': gname}, 'x', ask_confirm=False) |
|
919 |
if isinstance(conditions, basestring): |
|
920 |
conditions = (conditions,) |
|
921 |
for expr in conditions: |
|
922 |
if isinstance(expr, str): |
|
923 |
expr = unicode(expr) |
|
924 |
self.rqlexec('INSERT RQLExpression X: X exprtype "ERQLExpression", ' |
|
925 |
'X expression %(expr)s, T condition X ' |
|
926 |
'WHERE T eid %(x)s', |
|
927 |
{'x': treid, 'expr': expr}, 'x', ask_confirm=False) |
|
928 |
if commit: |
|
929 |
self.commit() |
|
930 |
||
972 | 931 |
def cmd_set_state(self, eid, statename, commit=False): |
932 |
self.session.set_pool() # ensure pool is set |
|
2680
66472d85d548
[R] use req.entity_from_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2617
diff
changeset
|
933 |
entity = self.session.entity_from_eid(eid) |
972 | 934 |
entity.change_state(entity.wf_state(statename).eid) |
935 |
if commit: |
|
936 |
self.commit() |
|
1477 | 937 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
938 |
# CWProperty handling ###################################################### |
0 | 939 |
|
940 |
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
|
941 |
rql = 'Any V WHERE X is CWProperty, X pkey %(k)s, X value V' |
0 | 942 |
rset = self.rqlexec(rql, {'k': pkey}, ask_confirm=False) |
943 |
return rset[0][0] |
|
944 |
||
945 |
def cmd_set_property(self, pkey, value): |
|
946 |
value = unicode(value) |
|
947 |
try: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
948 |
prop = self.rqlexec('CWProperty X WHERE X pkey %(k)s', {'k': pkey}, |
0 | 949 |
ask_confirm=False).get_entity(0, 0) |
950 |
except: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
951 |
self.cmd_add_entity('CWProperty', pkey=unicode(pkey), value=value) |
0 | 952 |
else: |
953 |
self.rqlexec('SET X value %(v)s WHERE X pkey %(k)s', |
|
954 |
{'k': pkey, 'v': value}, ask_confirm=False) |
|
955 |
||
956 |
# other data migration commands ########################################### |
|
1477 | 957 |
|
0 | 958 |
def cmd_add_entity(self, etype, *args, **kwargs): |
959 |
"""add a new entity of the given type""" |
|
960 |
rql = 'INSERT %s X' % etype |
|
961 |
relations = [] |
|
962 |
restrictions = [] |
|
963 |
for rtype, rvar in args: |
|
964 |
relations.append('X %s %s' % (rtype, rvar)) |
|
965 |
restrictions.append('%s eid %s' % (rvar, kwargs.pop(rvar))) |
|
966 |
commit = kwargs.pop('commit', False) |
|
967 |
for attr in kwargs: |
|
968 |
relations.append('X %s %%(%s)s' % (attr, attr)) |
|
969 |
if relations: |
|
970 |
rql = '%s: %s' % (rql, ', '.join(relations)) |
|
971 |
if restrictions: |
|
972 |
rql = '%s WHERE %s' % (rql, ', '.join(restrictions)) |
|
973 |
eid = self.rqlexec(rql, kwargs, ask_confirm=self.verbosity>=2).rows[0][0] |
|
974 |
if commit: |
|
975 |
self.commit() |
|
976 |
return eid |
|
1477 | 977 |
|
0 | 978 |
def sqlexec(self, sql, args=None, ask_confirm=True): |
979 |
"""execute the given sql if confirmed |
|
1477 | 980 |
|
0 | 981 |
should only be used for low level stuff undoable with existing higher |
982 |
level actions |
|
983 |
""" |
|
984 |
if not ask_confirm or self.confirm('execute sql: %s ?' % sql): |
|
985 |
self.session.set_pool() # ensure pool is set |
|
986 |
try: |
|
987 |
cu = self.session.system_sql(sql, args) |
|
988 |
except: |
|
989 |
ex = sys.exc_info()[1] |
|
990 |
if self.confirm('error: %s\nabort?' % ex): |
|
991 |
raise |
|
992 |
return |
|
993 |
try: |
|
994 |
return cu.fetchall() |
|
995 |
except: |
|
996 |
# no result to fetch |
|
997 |
return |
|
1477 | 998 |
|
0 | 999 |
def rqlexec(self, rql, kwargs=None, cachekey=None, ask_confirm=True): |
1000 |
"""rql action""" |
|
1001 |
if not isinstance(rql, (tuple, list)): |
|
1002 |
rql = ( (rql, kwargs), ) |
|
1003 |
res = None |
|
1004 |
for rql, kwargs in rql: |
|
1005 |
if kwargs: |
|
1006 |
msg = '%s (%s)' % (rql, kwargs) |
|
1007 |
else: |
|
1008 |
msg = rql |
|
1009 |
if not ask_confirm or self.confirm('execute rql: %s ?' % msg): |
|
1010 |
try: |
|
1011 |
res = self.rqlcursor.execute(rql, kwargs, cachekey) |
|
1012 |
except Exception, ex: |
|
1013 |
if self.confirm('error: %s\nabort?' % ex): |
|
1014 |
raise |
|
1015 |
return res |
|
1016 |
||
1017 |
def rqliter(self, rql, kwargs=None, ask_confirm=True): |
|
1018 |
return ForRqlIterator(self, rql, None, ask_confirm) |
|
1019 |
||
1020 |
def cmd_deactivate_verification_hooks(self): |
|
1021 |
self.repo.hm.deactivate_verification_hooks() |
|
1022 |
||
1023 |
def cmd_reactivate_verification_hooks(self): |
|
1024 |
self.repo.hm.reactivate_verification_hooks() |
|
1477 | 1025 |
|
0 | 1026 |
# broken db commands ###################################################### |
1027 |
||
1028 |
def cmd_change_attribute_type(self, etype, attr, newtype, commit=True): |
|
1029 |
"""low level method to change the type of an entity attribute. This is |
|
1030 |
a quick hack which has some drawback: |
|
1031 |
* only works when the old type can be changed to the new type by the |
|
1032 |
underlying rdbms (eg using ALTER TABLE) |
|
1033 |
* the actual schema won't be updated until next startup |
|
1034 |
""" |
|
1035 |
rschema = self.repo.schema.rschema(attr) |
|
1036 |
oldtype = rschema.objects(etype)[0] |
|
1037 |
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
|
1038 |
sql = ("UPDATE CWAttribute " |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1039 |
"SET to_entity=(SELECT eid FROM CWEType WHERE name='%s')" |
0 | 1040 |
"WHERE eid=%s") % (newtype, rdefeid) |
1041 |
self.sqlexec(sql, ask_confirm=False) |
|
1042 |
dbhelper = self.repo.system_source.dbhelper |
|
1043 |
sqltype = dbhelper.TYPE_MAPPING[newtype] |
|
1044 |
sql = 'ALTER TABLE %s ALTER COLUMN %s TYPE %s' % (etype, attr, sqltype) |
|
1045 |
self.sqlexec(sql, ask_confirm=False) |
|
1046 |
if commit: |
|
1047 |
self.commit() |
|
1477 | 1048 |
|
0 | 1049 |
def cmd_add_entity_type_table(self, etype, commit=True): |
1050 |
"""low level method to create the sql table for an existing entity. |
|
1051 |
This may be useful on accidental desync between the repository schema |
|
1052 |
and a sql database |
|
1053 |
""" |
|
1054 |
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
|
1055 |
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
|
1056 |
prefix=SQL_PREFIX) |
0 | 1057 |
for sql in tablesql.split(';'): |
1058 |
if sql.strip(): |
|
1059 |
self.sqlexec(sql) |
|
1060 |
if commit: |
|
1061 |
self.commit() |
|
1477 | 1062 |
|
0 | 1063 |
def cmd_add_relation_type_table(self, rtype, commit=True): |
1064 |
"""low level method to create the sql table for an existing relation. |
|
1065 |
This may be useful on accidental desync between the repository schema |
|
1066 |
and a sql database |
|
1067 |
""" |
|
1068 |
dbhelper = self.repo.system_source.dbhelper |
|
1069 |
tablesql = rschema2sql(dbhelper, self.repo.schema.rschema(rtype)) |
|
1070 |
for sql in tablesql.split(';'): |
|
1071 |
if sql.strip(): |
|
1072 |
self.sqlexec(sql) |
|
1073 |
if commit: |
|
1074 |
self.commit() |
|
1477 | 1075 |
|
0 | 1076 |
|
1077 |
class ForRqlIterator: |
|
1078 |
"""specific rql iterator to make the loop skipable""" |
|
1079 |
def __init__(self, helper, rql, kwargs, ask_confirm): |
|
1080 |
self._h = helper |
|
1081 |
self.rql = rql |
|
1082 |
self.kwargs = kwargs |
|
1083 |
self.ask_confirm = ask_confirm |
|
1084 |
self._rsetit = None |
|
1477 | 1085 |
|
0 | 1086 |
def __iter__(self): |
1087 |
return self |
|
1477 | 1088 |
|
0 | 1089 |
def next(self): |
1090 |
if self._rsetit is not None: |
|
1091 |
return self._rsetit.next() |
|
1092 |
rql, kwargs = self.rql, self.kwargs |
|
1093 |
if kwargs: |
|
1094 |
msg = '%s (%s)' % (rql, kwargs) |
|
1095 |
else: |
|
1096 |
msg = rql |
|
1097 |
if self.ask_confirm: |
|
1098 |
if not self._h.confirm('execute rql: %s ?' % msg): |
|
1099 |
raise StopIteration |
|
1100 |
try: |
|
1101 |
rset = self._h.rqlcursor.execute(rql, kwargs) |
|
1102 |
except Exception, ex: |
|
1103 |
if self._h.confirm('error: %s\nabort?' % ex): |
|
1104 |
raise |
|
1105 |
else: |
|
1106 |
raise StopIteration |
|
1107 |
self._rsetit = iter(rset) |
|
1108 |
return self._rsetit.next() |