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