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