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