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