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