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