author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 21 Apr 2010 16:53:47 +0200 | |
branch | stable |
changeset 5368 | d321e4b62a10 |
parent 5293 | 72e102a06709 |
child 5302 | dfd147de06b2 |
child 5399 | 03c641ae00a6 |
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 |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4195
diff
changeset
|
14 |
:copyright: 2001-2010 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 |
""" |
4925
0d66fbe050c6
[migration] disable notification by default during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4905
diff
changeset
|
18 |
from __future__ import with_statement |
0d66fbe050c6
[migration] disable notification by default during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4905
diff
changeset
|
19 |
|
0 | 20 |
__docformat__ = "restructuredtext en" |
21 |
||
22 |
import sys |
|
23 |
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
|
24 |
import tarfile |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
25 |
import tempfile |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
26 |
import shutil |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
27 |
import os.path as osp |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
972
diff
changeset
|
28 |
from datetime import datetime |
3903
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
29 |
from glob import glob |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
30 |
from copy import copy |
3903
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
31 |
from warnings import warn |
0 | 32 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2493
diff
changeset
|
33 |
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
|
34 |
from logilab.common.decorators import cached, clear_cache |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
35 |
from logilab.common.testlib import mock_object |
0 | 36 |
|
37 |
from yams.constraints import SizeConstraint |
|
38 |
from yams.schema2sql import eschema2sql, rschema2sql |
|
39 |
||
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4716
diff
changeset
|
40 |
from cubicweb import AuthenticationError |
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
|
41 |
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
|
42 |
CubicWebRelationSchema, order_eschemas) |
0 | 43 |
from cubicweb.dbapi import get_repository, repo_connect |
4021
280c910c8710
move i18n / migration modules from cw.common to cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4017
diff
changeset
|
44 |
from cubicweb.migration import MigrationHelper, yes |
4925
0d66fbe050c6
[migration] disable notification by default during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4905
diff
changeset
|
45 |
from cubicweb.server.session import hooks_control |
0 | 46 |
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
|
47 |
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
|
48 |
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
|
49 |
from cubicweb.server.sqlutils import sqlexec, SQL_PREFIX |
0 | 50 |
except ImportError: # LAX |
51 |
pass |
|
52 |
||
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
|
53 |
|
0 | 54 |
class ServerMigrationHelper(MigrationHelper): |
55 |
"""specific migration helper for server side migration scripts, |
|
56 |
providind actions related to schema/data migration |
|
57 |
""" |
|
58 |
||
59 |
def __init__(self, config, schema, interactive=True, |
|
60 |
repo=None, cnx=None, verbosity=1, connect=True): |
|
61 |
MigrationHelper.__init__(self, config, interactive, verbosity) |
|
62 |
if not interactive: |
|
63 |
assert cnx |
|
64 |
assert repo |
|
65 |
if cnx is not None: |
|
66 |
assert repo |
|
67 |
self._cnx = cnx |
|
68 |
self.repo = repo |
|
69 |
elif connect: |
|
70 |
self.repo_connect() |
|
5019
72734c210836
[c-c] new server_maintenance hook, called on c-c shell / upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
71 |
# no config on shell to a remote instance |
5034
4781870e97d9
[maintainance] don't crash if we've no in-memory repository
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5019
diff
changeset
|
72 |
if config is not None and (cnx or connect): |
5019
72734c210836
[c-c] new server_maintenance hook, called on c-c shell / upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
73 |
self.session.data['rebuild-infered'] = False |
72734c210836
[c-c] new server_maintenance hook, called on c-c shell / upgrade
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4951
diff
changeset
|
74 |
self.repo.hm.call_hooks('server_maintenance', repo=self.repo) |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5034
diff
changeset
|
75 |
if not schema and not getattr(config, 'quick_start', False): |
0 | 76 |
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
|
77 |
self.fs_schema = schema |
0 | 78 |
self._synchronized = set() |
79 |
||
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
80 |
# overriden from base MigrationHelper ###################################### |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
81 |
|
0 | 82 |
@cached |
83 |
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
|
84 |
self.repo = get_repository(method='inmemory', config=self.config) |
0 | 85 |
return self.repo |
1477 | 86 |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
87 |
def 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
|
88 |
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
|
89 |
unicode(version)) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
90 |
self.commit() |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
91 |
|
0 | 92 |
def shutdown(self): |
93 |
if self.repo is not None: |
|
94 |
self.repo.shutdown() |
|
1477 | 95 |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
96 |
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
|
97 |
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
|
98 |
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
|
99 |
self.backup_database() |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
100 |
elif options.backup_db: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
101 |
self.backup_database(askconfirm=False) |
4925
0d66fbe050c6
[migration] disable notification by default during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4905
diff
changeset
|
102 |
# disable notification during migration |
0d66fbe050c6
[migration] disable notification by default during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4905
diff
changeset
|
103 |
with hooks_control(self.session, self.session.HOOKS_ALLOW_ALL, 'notification'): |
0d66fbe050c6
[migration] disable notification by default during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4905
diff
changeset
|
104 |
super(ServerMigrationHelper, self).migrate(vcconf, toupgrade, options) |
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
105 |
|
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
|
106 |
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
|
107 |
"""execute a migration script |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
108 |
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
|
109 |
confirmation and execute it if confirmed |
0 | 110 |
""" |
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
111 |
try: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
112 |
if migrscript.endswith('.sql'): |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
113 |
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
|
114 |
sqlexec(open(migrscript).read(), self.session.system_sql) |
3935
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3903
diff
changeset
|
115 |
elif migrscript.endswith('.py') or migrscript.endswith('.txt'): |
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
|
116 |
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
|
117 |
migrscript, funcname, *args, **kwargs) |
3872
8b1b9179f100
[migration] avoid AssertionError / crashes if migration script is neither a py nor a sql file
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3836
diff
changeset
|
118 |
else: |
8b1b9179f100
[migration] avoid AssertionError / crashes if migration script is neither a py nor a sql file
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3836
diff
changeset
|
119 |
print |
3935
2fbb79054a1a
imported patch cwctl-shell-textfile
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3903
diff
changeset
|
120 |
print ('-> ignoring %s, only .py .sql and .txt scripts are considered' % |
3872
8b1b9179f100
[migration] avoid AssertionError / crashes if migration script is neither a py nor a sql file
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3836
diff
changeset
|
121 |
migrscript) |
8b1b9179f100
[migration] avoid AssertionError / crashes if migration script is neither a py nor a sql file
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3836
diff
changeset
|
122 |
print |
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
123 |
self.commit() |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
124 |
except: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
125 |
self.rollback() |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
126 |
raise |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
127 |
|
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
128 |
# server specific migration methods ######################################## |
1477 | 129 |
|
0 | 130 |
def backup_database(self, backupfile=None, askconfirm=True): |
131 |
config = self.config |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2489
diff
changeset
|
132 |
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
|
133 |
# paths |
3192 | 134 |
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
|
135 |
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
|
136 |
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
|
137 |
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
|
138 |
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
|
139 |
% (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
|
140 |
# 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
|
141 |
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
|
142 |
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
|
143 |
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
|
144 |
return |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
145 |
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
|
146 |
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
|
147 |
return |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
148 |
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
|
149 |
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
|
150 |
# backup |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
151 |
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
|
152 |
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
|
153 |
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
|
154 |
try: |
4904
a5250dafdcac
[backup] fix misplaced paren
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4893
diff
changeset
|
155 |
source.backup(osp.join(tmpdir, source.uri), self.confirm) |
4893
15ae9a33a7f2
[db backup] fix name error in backup_to_file: we've to pass .confirm all along the chain as for restore
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
156 |
except Exception, ex: |
15ae9a33a7f2
[db backup] fix name error in backup_to_file: we've to pass .confirm all along the chain as for restore
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
157 |
print '-> error trying to backup %s [%s]' % (source.uri, ex) |
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
|
158 |
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
|
159 |
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
|
160 |
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
|
161 |
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
|
162 |
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
|
163 |
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
|
164 |
for filename in os.listdir(tmpdir): |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
165 |
bkup.add(osp.join(tmpdir, filename), filename) |
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
|
166 |
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
|
167 |
# 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
|
168 |
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
|
169 |
# 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
|
170 |
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
|
171 |
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
|
172 |
shutil.rmtree(tmpdir) |
1477 | 173 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2489
diff
changeset
|
174 |
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
|
175 |
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
|
176 |
# check |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
177 |
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
|
178 |
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
|
179 |
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
|
180 |
% (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
|
181 |
return |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
182 |
# unpack backup |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
183 |
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
|
184 |
try: |
b788903e77d5
be able to restore pre cw 3.4 database dumps #370595
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2963
diff
changeset
|
185 |
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
|
186 |
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
|
187 |
# assume restoring old backup |
4344
066e7884e57d
add source in backup/restore failure message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
188 |
shutil.copy(backupfile, osp.join(tmpdir, 'system')) |
3105
b788903e77d5
be able to restore pre cw 3.4 database dumps #370595
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2963
diff
changeset
|
189 |
else: |
b788903e77d5
be able to restore pre cw 3.4 database dumps #370595
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2963
diff
changeset
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
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
|
197 |
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
|
198 |
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
|
199 |
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
|
200 |
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
|
201 |
continue |
9cb3027407aa
B offer to continue even when failed to restore systemonly
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2759
diff
changeset
|
202 |
try: |
4195
86dcaf6bb92f
closes #601987
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3935
diff
changeset
|
203 |
source.restore(osp.join(tmpdir, source.uri), self.confirm, drop) |
2861
9cb3027407aa
B offer to continue even when failed to restore systemonly
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2759
diff
changeset
|
204 |
except Exception, exc: |
4344
066e7884e57d
add source in backup/restore failure message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
205 |
print '-> error trying to restore %s [%s]' % (source.uri, exc) |
2861
9cb3027407aa
B offer to continue even when failed to restore systemonly
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2759
diff
changeset
|
206 |
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
|
207 |
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
|
208 |
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
|
209 |
# 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
|
210 |
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
|
211 |
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
|
212 |
print '-> database restored.' |
1477 | 213 |
|
0 | 214 |
@property |
215 |
def cnx(self): |
|
216 |
"""lazy connection""" |
|
217 |
try: |
|
218 |
return self._cnx |
|
219 |
except AttributeError: |
|
220 |
sourcescfg = self.repo.config.sources() |
|
221 |
try: |
|
222 |
login = sourcescfg['admin']['login'] |
|
223 |
pwd = sourcescfg['admin']['password'] |
|
224 |
except KeyError: |
|
225 |
login, pwd = manager_userpasswd() |
|
226 |
while True: |
|
227 |
try: |
|
3659 | 228 |
self._cnx = repo_connect(self.repo, login, password=pwd) |
0 | 229 |
if not 'managers' in self._cnx.user(self.session).groups: |
230 |
print 'migration need an account in the managers group' |
|
231 |
else: |
|
232 |
break |
|
233 |
except AuthenticationError: |
|
234 |
print 'wrong user/password' |
|
235 |
except (KeyboardInterrupt, EOFError): |
|
236 |
print 'aborting...' |
|
237 |
sys.exit(0) |
|
238 |
try: |
|
239 |
login, pwd = manager_userpasswd() |
|
240 |
except (KeyboardInterrupt, EOFError): |
|
241 |
print 'aborting...' |
|
242 |
sys.exit(0) |
|
2571
5c837feca73a
oops, this is a method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2570
diff
changeset
|
243 |
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
|
244 |
self.session.data['rebuild-infered'] = False |
0 | 245 |
return self._cnx |
246 |
||
247 |
@property |
|
248 |
def session(self): |
|
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
249 |
if self.config is not None: |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
250 |
session = self.repo._get_session(self.cnx.sessionid) |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
251 |
if session.pool is None: |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
252 |
session.set_read_security(False) |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
253 |
session.set_write_security(False) |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
254 |
session.set_pool() |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
255 |
return session |
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
256 |
# 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
|
257 |
return None |
1477 | 258 |
|
0 | 259 |
def commit(self): |
260 |
if hasattr(self, '_cnx'): |
|
261 |
self._cnx.commit() |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
262 |
if self.session: |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
263 |
self.session.set_pool() |
1477 | 264 |
|
0 | 265 |
def rollback(self): |
266 |
if hasattr(self, '_cnx'): |
|
267 |
self._cnx.rollback() |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
268 |
if self.session: |
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
269 |
self.session.set_pool() |
1477 | 270 |
|
0 | 271 |
def rqlexecall(self, rqliter, cachekey=None, ask_confirm=True): |
272 |
for rql, kwargs in rqliter: |
|
4945
356662a6f06c
[migration] new build_descr argument to rqlexec on the migration helper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4925
diff
changeset
|
273 |
self.rqlexec(rql, kwargs, cachekey, ask_confirm=ask_confirm) |
0 | 274 |
|
275 |
@cached |
|
276 |
def _create_context(self): |
|
277 |
"""return a dictionary to use as migration script execution context""" |
|
278 |
context = super(ServerMigrationHelper, self)._create_context() |
|
4017
a5b4d4f2a1c7
use commit in migration script instead of checkpoint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4011
diff
changeset
|
279 |
context.update({'commit': self.checkpoint, |
4330
cb7b68679501
make rollback available in shell (as commit is)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
280 |
'rollback': self.rollback, |
4017
a5b4d4f2a1c7
use commit in migration script instead of checkpoint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4011
diff
changeset
|
281 |
'checkpoint': deprecated('[3.6] use commit')(self.checkpoint), |
0 | 282 |
'sql': self.sqlexec, |
283 |
'rql': self.rqlexec, |
|
284 |
'rqliter': self.rqliter, |
|
3707
78596919ede3
[c-c] fixes for shell w/ pyro instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3700
diff
changeset
|
285 |
'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
|
286 |
'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
|
287 |
'fsschema': self.fs_schema, |
0 | 288 |
'session' : self.session, |
289 |
'repo' : self.repo, |
|
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2759
diff
changeset
|
290 |
'synchronize_schema': deprecated()(self.cmd_sync_schema_props_perms), # 3.4 |
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2759
diff
changeset
|
291 |
'synchronize_eschema': deprecated()(self.cmd_sync_schema_props_perms), # 3.4 |
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2759
diff
changeset
|
292 |
'synchronize_rschema': deprecated()(self.cmd_sync_schema_props_perms), # 3.4 |
0 | 293 |
}) |
294 |
return context |
|
295 |
||
296 |
@cached |
|
297 |
def group_mapping(self): |
|
298 |
"""cached group mapping""" |
|
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
299 |
return ss.group_mapping(self._cw) |
1477 | 300 |
|
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
301 |
@cached |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
302 |
def cstrtype_mapping(self): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
303 |
"""cached constraint types mapping""" |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
304 |
return ss.cstrtype_mapping(self._cw) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
305 |
|
0 | 306 |
def exec_event_script(self, event, cubepath=None, funcname=None, |
1477 | 307 |
*args, **kwargs): |
0 | 308 |
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
|
309 |
apc = osp.join(cubepath, 'migration', '%s.py' % event) |
0 | 310 |
else: |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
311 |
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
|
312 |
if osp.exists(apc): |
0 | 313 |
if self.config.free_wheel: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
314 |
self.cmd_deactivate_verification_hooks() |
0 | 315 |
self.info('executing %s', apc) |
316 |
confirm = self.confirm |
|
317 |
execscript_confirm = self.execscript_confirm |
|
318 |
self.confirm = yes |
|
319 |
self.execscript_confirm = yes |
|
320 |
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
|
321 |
return self.cmd_process_script(apc, funcname, *args, **kwargs) |
0 | 322 |
finally: |
323 |
self.confirm = confirm |
|
324 |
self.execscript_confirm = execscript_confirm |
|
325 |
if self.config.free_wheel: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
326 |
self.cmd_reactivate_verification_hooks() |
0 | 327 |
|
3903
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
328 |
def install_custom_sql_scripts(self, directory, driver): |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
329 |
for fpath in glob(osp.join(directory, '*.sql.%s' % driver)): |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
330 |
newname = osp.basename(fpath).replace('.sql.%s' % driver, |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
331 |
'.%s.sql' % driver) |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
332 |
warn('[3.5.6] rename %s into %s' % (fpath, newname), |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
333 |
DeprecationWarning) |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
334 |
print '-> installing', fpath |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
335 |
sqlexec(open(fpath).read(), self.session.system_sql, False, |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
336 |
delimiter=';;') |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
337 |
for fpath in glob(osp.join(directory, '*.%s.sql' % driver)): |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
338 |
print '-> installing', fpath |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
339 |
sqlexec(open(fpath).read(), self.session.system_sql, False, |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
340 |
delimiter=';;') |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
341 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
342 |
# schema synchronization internals ######################################## |
0 | 343 |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
344 |
def _synchronize_permissions(self, erschema, teid): |
0 | 345 |
"""permission synchronization for an entity or relation type""" |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
346 |
assert teid, erschema |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
347 |
if 'update' in erschema.ACTIONS or erschema.final: |
0 | 348 |
# entity type |
349 |
exprtype = u'ERQLExpression' |
|
350 |
else: |
|
351 |
# relation type |
|
352 |
exprtype = u'RRQLExpression' |
|
353 |
gm = self.group_mapping() |
|
354 |
confirm = self.verbosity >= 2 |
|
355 |
# * remove possibly deprecated permission (eg in the persistent schema |
|
356 |
# but not in the new schema) |
|
357 |
# * synchronize existing expressions |
|
358 |
# * add new groups/expressions |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
359 |
for action in erschema.ACTIONS: |
0 | 360 |
perm = '%s_permission' % action |
361 |
# handle groups |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
362 |
newgroups = list(erschema.get_groups(action)) |
0 | 363 |
for geid, gname in self.rqlexec('Any G, GN WHERE T %s G, G name GN, ' |
364 |
'T eid %%(x)s' % perm, {'x': teid}, 'x', |
|
365 |
ask_confirm=False): |
|
366 |
if not gname in newgroups: |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
367 |
if not confirm or self.confirm('Remove %s permission of %s to %s?' |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
368 |
% (action, erschema, gname)): |
0 | 369 |
self.rqlexec('DELETE T %s G WHERE G eid %%(x)s, T eid %s' |
370 |
% (perm, teid), |
|
371 |
{'x': geid}, 'x', ask_confirm=False) |
|
372 |
else: |
|
373 |
newgroups.remove(gname) |
|
374 |
for gname in newgroups: |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
375 |
if not confirm or self.confirm('Grant %s permission of %s to %s?' |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
376 |
% (action, erschema, gname)): |
0 | 377 |
self.rqlexec('SET T %s G WHERE G eid %%(x)s, T eid %s' |
378 |
% (perm, teid), |
|
379 |
{'x': gm[gname]}, 'x', ask_confirm=False) |
|
380 |
# handle rql expressions |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
381 |
newexprs = dict((expr.expression, expr) for expr in erschema.get_rqlexprs(action)) |
0 | 382 |
for expreid, expression in self.rqlexec('Any E, EX WHERE T %s E, E expression EX, ' |
383 |
'T eid %s' % (perm, teid), |
|
384 |
ask_confirm=False): |
|
385 |
if not expression in newexprs: |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
386 |
if not confirm or self.confirm('Remove %s expression for %s permission of %s?' |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
387 |
% (expression, action, erschema)): |
0 | 388 |
# deleting the relation will delete the expression entity |
389 |
self.rqlexec('DELETE T %s E WHERE E eid %%(x)s, T eid %s' |
|
390 |
% (perm, teid), |
|
391 |
{'x': expreid}, 'x', ask_confirm=False) |
|
392 |
else: |
|
393 |
newexprs.pop(expression) |
|
394 |
for expression in newexprs.values(): |
|
395 |
expr = expression.expression |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
396 |
if not confirm or self.confirm('Add %s expression for %s permission of %s?' |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
397 |
% (expr, action, erschema)): |
0 | 398 |
self.rqlexec('INSERT RQLExpression X: X exprtype %%(exprtype)s, ' |
399 |
'X expression %%(expr)s, X mainvars %%(vars)s, T %s X ' |
|
400 |
'WHERE T eid %%(x)s' % perm, |
|
401 |
{'expr': expr, 'exprtype': exprtype, |
|
402 |
'vars': expression.mainvars, 'x': teid}, 'x', |
|
403 |
ask_confirm=False) |
|
1477 | 404 |
|
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
405 |
def _synchronize_rschema(self, rtype, syncrdefs=True, syncperms=True, syncprops=True): |
0 | 406 |
"""synchronize properties of the persistent relation schema against its |
407 |
current definition: |
|
1477 | 408 |
|
0 | 409 |
* description |
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
410 |
* symmetric, meta |
0 | 411 |
* inlined |
412 |
* relation definitions if `syncrdefs` |
|
413 |
* permissions if `syncperms` |
|
1477 | 414 |
|
0 | 415 |
physical schema changes should be handled by repository's schema hooks |
416 |
""" |
|
417 |
rtype = str(rtype) |
|
418 |
if rtype in self._synchronized: |
|
419 |
return |
|
420 |
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
|
421 |
rschema = self.fs_schema.rschema(rtype) |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
422 |
reporschema = self.repo.schema.rschema(rtype) |
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
423 |
if syncprops: |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
424 |
assert reporschema.eid, reporschema |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
425 |
self.rqlexecall(ss.updaterschema2rql(rschema, reporschema.eid), |
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
426 |
ask_confirm=self.verbosity>=2) |
0 | 427 |
if syncrdefs: |
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
428 |
for subj, obj in rschema.rdefs: |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
429 |
if (subj, obj) not in reporschema.rdefs: |
0 | 430 |
continue |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
431 |
if rschema in VIRTUAL_RTYPES: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
432 |
continue |
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
433 |
self._synchronize_rdef_schema(subj, rschema, obj, |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
434 |
syncprops=syncprops, |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
435 |
syncperms=syncperms) |
1477 | 436 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
437 |
def _synchronize_eschema(self, etype, syncperms=True): |
0 | 438 |
"""synchronize properties of the persistent entity schema against |
439 |
its current definition: |
|
1477 | 440 |
|
0 | 441 |
* description |
442 |
* internationalizable, fulltextindexed, indexed, meta |
|
443 |
* relations from/to this entity |
|
444 |
* permissions if `syncperms` |
|
445 |
""" |
|
446 |
etype = str(etype) |
|
447 |
if etype in self._synchronized: |
|
448 |
return |
|
449 |
self._synchronized.add(etype) |
|
450 |
repoeschema = self.repo.schema.eschema(etype) |
|
451 |
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
|
452 |
eschema = self.fs_schema.eschema(etype) |
0 | 453 |
except KeyError: |
454 |
return |
|
455 |
repospschema = repoeschema.specializes() |
|
456 |
espschema = eschema.specializes() |
|
457 |
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
|
458 |
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
|
459 |
{'x': str(repoeschema)}, ask_confirm=False) |
0 | 460 |
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
|
461 |
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
|
462 |
'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
|
463 |
{'x': str(repoeschema), 'y': str(espschema)}, |
36a2357ccbc4
[migration] don't ask confirm here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3192
diff
changeset
|
464 |
ask_confirm=False) |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
465 |
self.rqlexecall(ss.updateeschema2rql(eschema, repoeschema.eid), |
0 | 466 |
ask_confirm=self.verbosity >= 2) |
2962
5e2239672e16
[migraction] use role instead of x
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2960
diff
changeset
|
467 |
for rschema, targettypes, role in eschema.relation_definitions(True): |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
468 |
if rschema in VIRTUAL_RTYPES: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
469 |
continue |
2962
5e2239672e16
[migraction] use role instead of x
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2960
diff
changeset
|
470 |
if role == 'subject': |
0 | 471 |
if not rschema in repoeschema.subject_relations(): |
472 |
continue |
|
473 |
subjtypes, objtypes = [etype], targettypes |
|
2962
5e2239672e16
[migraction] use role instead of x
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2960
diff
changeset
|
474 |
else: # role == 'object' |
0 | 475 |
if not rschema in repoeschema.object_relations(): |
476 |
continue |
|
477 |
subjtypes, objtypes = targettypes, [etype] |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
478 |
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
|
479 |
syncrdefs=False) |
0 | 480 |
reporschema = self.repo.schema.rschema(rschema) |
481 |
for subj in subjtypes: |
|
482 |
for obj in objtypes: |
|
4042
f85a4c846aad
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4041
diff
changeset
|
483 |
if (subj, obj) not in reporschema.rdefs: |
0 | 484 |
continue |
1414
448c9802d7df
does not apply any more
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1409
diff
changeset
|
485 |
self._synchronize_rdef_schema(subj, rschema, obj) |
0 | 486 |
if syncperms: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
487 |
self._synchronize_permissions(eschema, repoeschema.eid) |
0 | 488 |
|
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
489 |
def _synchronize_rdef_schema(self, subjtype, rtype, objtype, |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
490 |
syncperms=True, syncprops=True): |
0 | 491 |
"""synchronize properties of the persistent relation definition schema |
492 |
against its current definition: |
|
493 |
* order and other properties |
|
494 |
* constraints |
|
495 |
""" |
|
496 |
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
|
497 |
rschema = self.fs_schema.rschema(rtype) |
0 | 498 |
reporschema = self.repo.schema.rschema(rschema) |
499 |
if (subjtype, rschema, objtype) in self._synchronized: |
|
500 |
return |
|
501 |
self._synchronized.add((subjtype, rschema, objtype)) |
|
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
502 |
if rschema.symmetric: |
0 | 503 |
self._synchronized.add((objtype, rschema, subjtype)) |
4949
f4dce73da26b
[schema sync] don't try to synchronize infered relation defs, fixing a name error on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4947
diff
changeset
|
504 |
rdef = rschema.rdef(subjtype, objtype) |
f4dce73da26b
[schema sync] don't try to synchronize infered relation defs, fixing a name error on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4947
diff
changeset
|
505 |
if rdef.infered: |
f4dce73da26b
[schema sync] don't try to synchronize infered relation defs, fixing a name error on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4947
diff
changeset
|
506 |
return # don't try to synchronize infered relation defs |
4950
bca0873c0d6e
[schema sync] fix another potential name error on synchronizing rdefs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4949
diff
changeset
|
507 |
repordef = reporschema.rdef(subjtype, objtype) |
0 | 508 |
confirm = self.verbosity >= 2 |
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
509 |
if syncprops: |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
510 |
# properties |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
511 |
self.rqlexecall(ss.updaterdef2rql(rdef, repordef.eid), |
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
512 |
ask_confirm=confirm) |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
513 |
# constraints |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
514 |
newconstraints = list(rdef.constraints) |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
515 |
# 1. remove old constraints and update constraints of the same type |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
516 |
# NOTE: don't use rschema.constraint_by_type because it may be |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
517 |
# out of sync with newconstraints when multiple |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
518 |
# constraints of the same type are used |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
519 |
for cstr in repordef.constraints: |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
520 |
for newcstr in newconstraints: |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
521 |
if newcstr.type() == cstr.type(): |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
522 |
break |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
523 |
else: |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
524 |
newcstr = None |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
525 |
if newcstr is None: |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
526 |
self.rqlexec('DELETE X constrained_by C WHERE C eid %(x)s', |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
527 |
{'x': cstr.eid}, 'x', |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
528 |
ask_confirm=confirm) |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
529 |
else: |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
530 |
newconstraints.remove(newcstr) |
4947
c3ded0287295
don't update constraints when it's not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4904
diff
changeset
|
531 |
value = unicode(newcstr.serialize()) |
c3ded0287295
don't update constraints when it's not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4904
diff
changeset
|
532 |
if value != unicode(cstr.serialize()): |
c3ded0287295
don't update constraints when it's not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4904
diff
changeset
|
533 |
self.rqlexec('SET X value %(v)s WHERE X eid %(x)s', |
c3ded0287295
don't update constraints when it's not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4904
diff
changeset
|
534 |
{'x': cstr.eid, 'v': value}, 'x', |
c3ded0287295
don't update constraints when it's not necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4904
diff
changeset
|
535 |
ask_confirm=confirm) |
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
536 |
# 2. add new constraints |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
537 |
cstrtype_map = self.cstrtype_mapping() |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
538 |
self.rqlexecall(ss.constraints2rql(cstrtype_map, newconstraints, |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
539 |
repordef.eid), |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
540 |
ask_confirm=confirm) |
4188
b3258d2afe04
fix virtual relation detection: erschema is no more a relation type but a relation def in _synchronize_permissions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4099
diff
changeset
|
541 |
if syncperms and not rschema in VIRTUAL_RTYPES: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
542 |
self._synchronize_permissions(rdef, repordef.eid) |
1477 | 543 |
|
0 | 544 |
# base actions ############################################################ |
545 |
||
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3998
diff
changeset
|
546 |
def checkpoint(self, ask_confirm=True): |
0 | 547 |
"""checkpoint action""" |
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
548 |
if not ask_confirm or self.confirm('Commit now ?', shell=False): |
0 | 549 |
self.commit() |
550 |
||
551 |
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
|
552 |
self.cmd_add_cubes( (cube,), update_database) |
1477 | 553 |
|
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
|
554 |
def cmd_add_cubes(self, cubes, update_database=True): |
0 | 555 |
"""update_database is telling if the database schema should be updated |
556 |
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
|
557 |
a cube has been extracted from an existing instance, so the |
0 | 558 |
cube schema is already in there) |
559 |
""" |
|
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
|
560 |
newcubes = super(ServerMigrationHelper, self).cmd_add_cubes(cubes) |
0 | 561 |
if not newcubes: |
562 |
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
|
563 |
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
|
564 |
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
|
565 |
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
|
566 |
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
|
567 |
# 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
|
568 |
# 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
|
569 |
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
|
570 |
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
|
571 |
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
|
572 |
clear_cache(self.config, 'read_sources_file') |
3836
58c09f21f503
fix necessary when adding a cube which isn't listed in fs dependencies of the instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3715
diff
changeset
|
573 |
# ensure added cube is in config cubes |
58c09f21f503
fix necessary when adding a cube which isn't listed in fs dependencies of the instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3715
diff
changeset
|
574 |
# XXX worth restoring on error? |
58c09f21f503
fix necessary when adding a cube which isn't listed in fs dependencies of the instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3715
diff
changeset
|
575 |
if not cube in self.config._cubes: |
58c09f21f503
fix necessary when adding a cube which isn't listed in fs dependencies of the instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3715
diff
changeset
|
576 |
self.config._cubes += (cube,) |
0 | 577 |
if not update_database: |
578 |
self.commit() |
|
579 |
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
|
580 |
newcubes_schema = self.config.load_schema(construction_mode='non-strict') |
3836
58c09f21f503
fix necessary when adding a cube which isn't listed in fs dependencies of the instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3715
diff
changeset
|
581 |
# XXX we have to replace fs_schema, used in cmd_add_relation_type |
58c09f21f503
fix necessary when adding a cube which isn't listed in fs dependencies of the instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3715
diff
changeset
|
582 |
# etc. and fsschema of migration script contexts |
58c09f21f503
fix necessary when adding a cube which isn't listed in fs dependencies of the instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3715
diff
changeset
|
583 |
self.fs_schema = self._create_context()['fsschema'] = newcubes_schema |
0 | 584 |
new = set() |
585 |
# execute pre-create files |
|
3903
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
586 |
driver = self.repo.system_source.dbdriver |
0 | 587 |
for pack in reversed(newcubes): |
3903
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
588 |
cubedir = self.config.cube_dir(pack) |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
589 |
self.install_custom_sql_scripts(osp.join(cubedir, 'schema'), driver) |
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
590 |
self.exec_event_script('precreate', cubedir) |
0 | 591 |
# 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
|
592 |
for rschema in newcubes_schema.relations(): |
0 | 593 |
if not rschema in self.repo.schema: |
594 |
self.cmd_add_relation_type(rschema.type) |
|
595 |
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
|
596 |
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
|
597 |
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
|
598 |
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
|
599 |
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
|
600 |
new.add(eschema.type) |
0 | 601 |
# 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
|
602 |
for rschema in newcubes_schema.relations(): |
0 | 603 |
existingschema = self.repo.schema.rschema(rschema.type) |
4099
59ff385f7348
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4043
diff
changeset
|
604 |
for (fromtype, totype) in rschema.rdefs: |
59ff385f7348
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4043
diff
changeset
|
605 |
if (fromtype, totype) in existingschema.rdefs: |
0 | 606 |
continue |
607 |
# check we should actually add the relation definition |
|
608 |
if not (fromtype in new or totype in new or rschema in new): |
|
609 |
continue |
|
1477 | 610 |
self.cmd_add_relation_definition(str(fromtype), rschema.type, |
0 | 611 |
str(totype)) |
612 |
# execute post-create files |
|
613 |
for pack in reversed(newcubes): |
|
614 |
self.exec_event_script('postcreate', self.config.cube_dir(pack)) |
|
1477 | 615 |
self.commit() |
616 |
||
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
|
617 |
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
|
618 |
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
|
619 |
cube, removedeps) |
0 | 620 |
if not removedcubes: |
621 |
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
|
622 |
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
|
623 |
removedcubes_schema = self.config.load_schema(construction_mode='non-strict') |
0 | 624 |
reposchema = self.repo.schema |
625 |
# execute pre-remove files |
|
626 |
for pack in reversed(removedcubes): |
|
627 |
self.exec_event_script('preremove', self.config.cube_dir(pack)) |
|
628 |
# 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
|
629 |
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
|
630 |
if not rschema in removedcubes_schema and rschema in reposchema: |
0 | 631 |
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
|
632 |
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
|
633 |
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
|
634 |
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
|
635 |
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
|
636 |
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
|
637 |
for rschema in fsschema.relations(): |
1477 | 638 |
if rschema in removedcubes_schema and rschema in reposchema: |
639 |
# check if attributes/relations has been added to entities from |
|
0 | 640 |
# other cubes |
4099
59ff385f7348
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4043
diff
changeset
|
641 |
for fromtype, totype in rschema.rdefs: |
59ff385f7348
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4043
diff
changeset
|
642 |
if (fromtype, totype) not in removedcubes_schema[rschema.type].rdefs and \ |
59ff385f7348
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4043
diff
changeset
|
643 |
(fromtype, totype) in reposchema[rschema.type].rdefs: |
0 | 644 |
self.cmd_drop_relation_definition( |
645 |
str(fromtype), rschema.type, str(totype)) |
|
646 |
# execute post-remove files |
|
647 |
for pack in reversed(removedcubes): |
|
648 |
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
|
649 |
self.rqlexec('DELETE CWProperty X WHERE X pkey %(pk)s', |
0 | 650 |
{'pk': u'system.version.'+pack}, ask_confirm=False) |
651 |
self.commit() |
|
1477 | 652 |
|
0 | 653 |
# schema migration actions ################################################ |
1477 | 654 |
|
0 | 655 |
def cmd_add_attribute(self, etype, attrname, attrtype=None, commit=True): |
656 |
"""add a new attribute on the given entity type""" |
|
657 |
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
|
658 |
rschema = self.fs_schema.rschema(attrname) |
0 | 659 |
attrtype = rschema.objects(etype)[0] |
660 |
self.cmd_add_relation_definition(etype, attrname, attrtype, commit=commit) |
|
1477 | 661 |
|
0 | 662 |
def cmd_drop_attribute(self, etype, attrname, commit=True): |
663 |
"""drop an existing attribute from the given entity type |
|
1477 | 664 |
|
0 | 665 |
`attrname` is a string giving the name of the attribute to drop |
666 |
""" |
|
667 |
rschema = self.repo.schema.rschema(attrname) |
|
668 |
attrtype = rschema.objects(etype)[0] |
|
669 |
self.cmd_drop_relation_definition(etype, attrname, attrtype, commit=commit) |
|
670 |
||
671 |
def cmd_rename_attribute(self, etype, oldname, newname, commit=True): |
|
672 |
"""rename an existing attribute of the given entity type |
|
1477 | 673 |
|
0 | 674 |
`oldname` is a string giving the name of the existing attribute |
675 |
`newname` is a string giving the name of the renamed attribute |
|
676 |
""" |
|
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
|
677 |
eschema = self.fs_schema.eschema(etype) |
0 | 678 |
attrtype = eschema.destination(newname) |
679 |
# have to commit this first step anyway to get the definition |
|
680 |
# actually in the schema |
|
681 |
self.cmd_add_attribute(etype, newname, attrtype, commit=True) |
|
682 |
# skipp NULL values if the attribute is required |
|
683 |
rql = 'SET X %s VAL WHERE X is %s, X %s VAL' % (newname, etype, oldname) |
|
4633
a85b4361fb22
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4580
diff
changeset
|
684 |
card = eschema.rdef(newname).cardinality[0] |
0 | 685 |
if card == '1': |
686 |
rql += ', NOT X %s NULL' % oldname |
|
687 |
self.rqlexec(rql, ask_confirm=self.verbosity>=2) |
|
3548
4cf5a360952e
add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3213
diff
changeset
|
688 |
# 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
|
689 |
# 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
|
690 |
# 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
|
691 |
# fulltextindexed change...) |
0 | 692 |
self.cmd_drop_attribute(etype, oldname, commit=commit) |
1477 | 693 |
|
0 | 694 |
def cmd_add_entity_type(self, etype, auto=True, commit=True): |
695 |
"""register a new entity type |
|
1477 | 696 |
|
0 | 697 |
in auto mode, automatically register entity's relation where the |
698 |
targeted type is known |
|
699 |
""" |
|
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
|
700 |
instschema = self.repo.schema |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
701 |
assert not etype in instschema |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
702 |
# # XXX (syt) plz explain: if we're adding an entity type, it should |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
703 |
# # not be there... |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
704 |
# eschema = instschema[etype] |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
705 |
# if eschema.final: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
706 |
# instschema.del_entity_type(etype) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
707 |
# else: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
708 |
eschema = self.fs_schema.eschema(etype) |
0 | 709 |
confirm = self.verbosity >= 2 |
4043
39ae94e0c8b8
give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4042
diff
changeset
|
710 |
groupmap = self.group_mapping() |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
711 |
cstrtypemap = self.cstrtype_mapping() |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
712 |
# register the entity into CWEType |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
713 |
execute = self._cw.execute |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
714 |
ss.execschemarql(execute, eschema, ss.eschema2rql(eschema, groupmap)) |
0 | 715 |
# add specializes relation if needed |
5293
72e102a06709
[migration] fix bug when trying to add a new entity type that inherit from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5289
diff
changeset
|
716 |
specialized = eschema.specializes() |
72e102a06709
[migration] fix bug when trying to add a new entity type that inherit from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5289
diff
changeset
|
717 |
if specialized: |
72e102a06709
[migration] fix bug when trying to add a new entity type that inherit from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5289
diff
changeset
|
718 |
try: |
72e102a06709
[migration] fix bug when trying to add a new entity type that inherit from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5289
diff
changeset
|
719 |
specialized.eid = instschema[specialized].eid |
72e102a06709
[migration] fix bug when trying to add a new entity type that inherit from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5289
diff
changeset
|
720 |
except KeyError: |
72e102a06709
[migration] fix bug when trying to add a new entity type that inherit from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5289
diff
changeset
|
721 |
raise Exception('trying to add entity type but parent type is ' |
72e102a06709
[migration] fix bug when trying to add a new entity type that inherit from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5289
diff
changeset
|
722 |
'not yet in the database schema') |
72e102a06709
[migration] fix bug when trying to add a new entity type that inherit from another
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5289
diff
changeset
|
723 |
self.rqlexecall(ss.eschemaspecialize2rql(eschema), ask_confirm=confirm) |
0 | 724 |
# register entity's attributes |
725 |
for rschema, attrschema in eschema.attribute_definitions(): |
|
726 |
# 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
|
727 |
if rschema.type in META_RTYPES: |
0 | 728 |
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
|
729 |
if not rschema.type in instschema: |
0 | 730 |
# need to add the relation type and to commit to get it |
731 |
# actually in the schema |
|
732 |
self.cmd_add_relation_type(rschema.type, False, commit=True) |
|
733 |
# register relation definition |
|
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
734 |
rdef = self._get_rdef(rschema, eschema, eschema.destination(rschema)) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
735 |
ss.execschemarql(execute, rdef, ss.rdef2rql(rdef, cstrtypemap, groupmap),) |
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
|
736 |
# 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
|
737 |
# 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
|
738 |
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
|
739 |
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
|
740 |
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
|
741 |
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
|
742 |
# 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
|
743 |
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
|
744 |
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
|
745 |
self.rqlexec('SET D specializes P WHERE D eid %(d)s, P name %(pn)s', |
3412 | 746 |
{'d': instspschema.eid, |
747 |
'pn': eschema.type}, ask_confirm=confirm) |
|
2963
12ad88615a12
test and fix migration introducing base classes (w/ regard to yams inheritance)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2962
diff
changeset
|
748 |
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
|
749 |
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
|
750 |
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
|
751 |
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
|
752 |
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
|
753 |
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
|
754 |
objschema = tschema |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3582
diff
changeset
|
755 |
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
|
756 |
# 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
|
757 |
# 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
|
758 |
# 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
|
759 |
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
|
760 |
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
|
761 |
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
|
762 |
objschema = spschema |
4633
a85b4361fb22
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4580
diff
changeset
|
763 |
if (rschema.rdef(subjschema, objschema).infered |
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
|
764 |
or (instschema.has_relation(rschema) and |
4633
a85b4361fb22
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4580
diff
changeset
|
765 |
(subjschema, objschema) in instschema[rschema].rdefs)): |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
766 |
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
|
767 |
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
|
768 |
subjschema.type, rschema.type, objschema.type) |
0 | 769 |
if auto: |
770 |
# we have commit here to get relation types actually in the schema |
|
771 |
self.commit() |
|
772 |
added = [] |
|
773 |
for rschema in eschema.subject_relations(): |
|
774 |
# attribute relation have already been processed and |
|
775 |
# '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
|
776 |
if rschema.final or rschema.type in META_RTYPES: |
0 | 777 |
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
|
778 |
rtypeadded = rschema.type in instschema |
0 | 779 |
for targetschema in rschema.objects(etype): |
780 |
# 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
|
781 |
# current instance schema |
0 | 782 |
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
|
783 |
if not targettype in instschema and targettype != etype: |
0 | 784 |
continue |
785 |
if not rtypeadded: |
|
786 |
# need to add the relation type and to commit to get it |
|
787 |
# actually in the schema |
|
788 |
added.append(rschema.type) |
|
789 |
self.cmd_add_relation_type(rschema.type, False, commit=True) |
|
790 |
rtypeadded = True |
|
791 |
# register relation definition |
|
4467
0e73d299730a
fix long-waiting symetric typo: should be spelled symmetric. Add auto database migration on schema deserialization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4367
diff
changeset
|
792 |
# remember this two avoid adding twice non symmetric relation |
0 | 793 |
# such as "Emailthread forked_from Emailthread" |
794 |
added.append((etype, rschema.type, targettype)) |
|
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
795 |
rdef = self._get_rdef(rschema, eschema, targetschema) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
796 |
ss.execschemarql(execute, rdef, |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
797 |
ss.rdef2rql(rdef, cstrtypemap, groupmap)) |
0 | 798 |
for rschema in eschema.object_relations(): |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
799 |
if rschema.type in META_RTYPES: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
800 |
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
|
801 |
rtypeadded = rschema.type in instschema or rschema.type in added |
0 | 802 |
for targetschema in rschema.subjects(etype): |
803 |
# 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
|
804 |
# current instance schema |
0 | 805 |
targettype = targetschema.type |
806 |
# don't check targettype != etype since in this case the |
|
807 |
# 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
|
808 |
if not targettype in instschema: |
0 | 809 |
continue |
810 |
if not rtypeadded: |
|
811 |
# need to add the relation type and to commit to get it |
|
812 |
# actually in the schema |
|
813 |
self.cmd_add_relation_type(rschema.type, False, commit=True) |
|
814 |
rtypeadded = True |
|
815 |
elif (targettype, rschema.type, etype) in added: |
|
816 |
continue |
|
817 |
# register relation definition |
|
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
818 |
rdef = self._get_rdef(rschema, targetschema, eschema) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
819 |
ss.execschemarql(execute, rdef, |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
820 |
ss.rdef2rql(rdef, cstrtypemap, groupmap)) |
0 | 821 |
if commit: |
822 |
self.commit() |
|
1477 | 823 |
|
0 | 824 |
def cmd_drop_entity_type(self, etype, commit=True): |
825 |
"""unregister an existing entity type |
|
1477 | 826 |
|
0 | 827 |
This will trigger deletion of necessary relation types and definitions |
828 |
""" |
|
829 |
# 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
|
830 |
# 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
|
831 |
self.rqlexec('DELETE CWEType X WHERE X name %(etype)s', {'etype': etype}, |
0 | 832 |
ask_confirm=self.verbosity>=2) |
833 |
if commit: |
|
834 |
self.commit() |
|
835 |
||
836 |
def cmd_rename_entity_type(self, oldname, newname, commit=True): |
|
837 |
"""rename an existing entity type in the persistent schema |
|
1477 | 838 |
|
0 | 839 |
`oldname` is a string giving the name of the existing entity type |
840 |
`newname` is a string giving the name of the renamed entity type |
|
841 |
""" |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
842 |
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
|
843 |
{'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
|
844 |
ask_confirm=False) |
0 | 845 |
if commit: |
846 |
self.commit() |
|
1477 | 847 |
|
0 | 848 |
def cmd_add_relation_type(self, rtype, addrdef=True, commit=True): |
849 |
"""register a new relation type named `rtype`, as described in the |
|
850 |
schema description file. |
|
851 |
||
852 |
`addrdef` is a boolean value; when True, it will also add all relations |
|
853 |
of the type just added found in the schema definition file. Note that it |
|
854 |
implies an intermediate "commit" which commits the relation type |
|
855 |
creation (but not the relation definitions themselves, for which |
|
856 |
committing depends on the `commit` argument value). |
|
1477 | 857 |
|
0 | 858 |
""" |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
859 |
reposchema = self.repo.schema |
934
f94e34795586
better variable/attribute names; fix remove_cube, it should not alter the .fs_schema attribute (former .new_schema)
sylvain.thenault@logilab.fr
parents:
676
diff
changeset
|
860 |
rschema = self.fs_schema.rschema(rtype) |
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
861 |
execute = self._cw.execute |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
862 |
# register the relation into CWRType and insert necessary relation |
0 | 863 |
# definitions |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
864 |
ss.execschemarql(execute, rschema, ss.rschema2rql(rschema, addrdef=False)) |
0 | 865 |
if addrdef: |
866 |
self.commit() |
|
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
867 |
gmap = self.group_mapping() |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
868 |
cmap = self.cstrtype_mapping() |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
869 |
for rdef in rschema.rdefs.itervalues(): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
870 |
if not (reposchema.has_entity(rdef.subject) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
871 |
and reposchema.has_entity(rdef.object)): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
872 |
continue |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
873 |
self._set_rdef_eid(rdef) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
874 |
ss.execschemarql(execute, rdef, |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
875 |
ss.rdef2rql(rdef, cmap, gmap)) |
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
|
876 |
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
|
877 |
# 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
|
878 |
# 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
|
879 |
# 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
|
880 |
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
|
881 |
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
|
882 |
# 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
|
883 |
objtypes = rschema.objects() |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
884 |
assert len(objtypes) == 1, objtypes |
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
|
885 |
objtype = objtypes[0] |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
886 |
rdef = copy(rschema.rdef(rschema.subjects(objtype)[0], objtype)) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
887 |
rdef.subject = etype |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
888 |
rdef.rtype = self.repo.schema.rschema(rschema) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
889 |
rdef.object = self.repo.schema.rschema(objtype) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
890 |
ss.execschemarql(execute, rdef, |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
891 |
ss.rdef2rql(rdef, cmap, gmap)) |
0 | 892 |
if commit: |
893 |
self.commit() |
|
1477 | 894 |
|
0 | 895 |
def cmd_drop_relation_type(self, rtype, commit=True): |
896 |
"""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
|
897 |
# 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
|
898 |
self.rqlexec('DELETE CWRType X WHERE X name %r' % rtype, |
0 | 899 |
ask_confirm=self.verbosity>=2) |
900 |
if commit: |
|
901 |
self.commit() |
|
1477 | 902 |
|
0 | 903 |
def cmd_rename_relation(self, oldname, newname, commit=True): |
904 |
"""rename an existing relation |
|
1477 | 905 |
|
0 | 906 |
`oldname` is a string giving the name of the existing relation |
907 |
`newname` is a string giving the name of the renamed relation |
|
908 |
""" |
|
909 |
self.cmd_add_relation_type(newname, commit=True) |
|
910 |
self.rqlexec('SET X %s Y WHERE X %s Y' % (newname, oldname), |
|
911 |
ask_confirm=self.verbosity>=2) |
|
912 |
self.cmd_drop_relation_type(oldname, commit=commit) |
|
913 |
||
914 |
def cmd_add_relation_definition(self, subjtype, rtype, objtype, commit=True): |
|
915 |
"""register a new relation definition, from its definition found in the |
|
916 |
schema definition file |
|
917 |
""" |
|
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
|
918 |
rschema = self.fs_schema.rschema(rtype) |
0 | 919 |
if not rtype in self.repo.schema: |
920 |
self.cmd_add_relation_type(rtype, addrdef=False, commit=True) |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
921 |
execute = self._cw.execute |
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
922 |
rdef = self._get_rdef(rschema, subjtype, objtype) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
923 |
ss.execschemarql(execute, rdef, |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
924 |
ss.rdef2rql(rdef, self.cstrtype_mapping(), |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
925 |
self.group_mapping())) |
0 | 926 |
if commit: |
927 |
self.commit() |
|
1477 | 928 |
|
4763
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
929 |
def _get_rdef(self, rschema, subjtype, objtype): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
930 |
return self._set_rdef_eid(rschema.rdefs[(subjtype, objtype)]) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
931 |
|
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
932 |
def _set_rdef_eid(self, rdef): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
933 |
for attr in ('rtype', 'subject', 'object'): |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
934 |
schemaobj = getattr(rdef, attr) |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
935 |
if getattr(schemaobj, 'eid', None) is None: |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
936 |
schemaobj.eid = self.repo.schema[schemaobj].eid |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
937 |
assert schemaobj.eid is not None |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
938 |
return rdef |
81b0df087375
schema serialization optimization by using eids instead of type names. Heavy refactoring/cleanup on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4721
diff
changeset
|
939 |
|
0 | 940 |
def cmd_drop_relation_definition(self, subjtype, rtype, objtype, commit=True): |
941 |
"""unregister an existing relation definition""" |
|
942 |
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
|
943 |
# 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
|
944 |
if rschema.final: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
945 |
etype = 'CWAttribute' |
0 | 946 |
else: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
947 |
etype = 'CWRelation' |
0 | 948 |
rql = ('DELETE %s X WHERE X from_entity FE, FE name "%s",' |
949 |
'X relation_type RT, RT name "%s", X to_entity TE, TE name "%s"') |
|
950 |
self.rqlexec(rql % (etype, subjtype, rtype, objtype), |
|
951 |
ask_confirm=self.verbosity>=2) |
|
952 |
if commit: |
|
953 |
self.commit() |
|
1477 | 954 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
955 |
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
|
956 |
syncprops=True, syncrdefs=True, commit=True): |
0 | 957 |
"""synchronize the persistent schema against the current definition |
958 |
schema. |
|
1477 | 959 |
|
0 | 960 |
It will synch common stuff between the definition schema and the |
961 |
actual persistent schema, it won't add/remove any entity or relation. |
|
962 |
""" |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
963 |
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
|
964 |
if ertype is not None: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
965 |
if isinstance(ertype, (tuple, list)): |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
966 |
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
|
967 |
assert syncprops, 'can\'t update permission for a relation definition' |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
968 |
self._synchronize_rdef_schema(ertype[0], ertype[1], ertype[2], |
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
969 |
syncperms=syncperms, |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
970 |
syncprops=syncprops) |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
971 |
else: |
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
972 |
erschema = self.repo.schema[ertype] |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
973 |
if isinstance(erschema, CubicWebRelationSchema): |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
974 |
self._synchronize_rschema(erschema, syncperms=syncperms, |
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
975 |
syncprops=syncprops, |
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
976 |
syncrdefs=syncrdefs) |
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
977 |
elif syncprops: |
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
978 |
self._synchronize_eschema(erschema, syncperms=syncperms) |
4041
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
979 |
else: |
be6e473e6b43
fix sync_schema*: new syncprops argument to sync relation definition as well
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4021
diff
changeset
|
980 |
self._synchronize_permissions(self.fs_schema[ertype], erschema.eid) |
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
981 |
else: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
982 |
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
|
983 |
if syncprops: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
984 |
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
|
985 |
else: |
5289
540fa73caac6
[migration] fix error when synchronizing everything and some schema still in the db schema but not anymore in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5132
diff
changeset
|
986 |
try: |
540fa73caac6
[migration] fix error when synchronizing everything and some schema still in the db schema but not anymore in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5132
diff
changeset
|
987 |
fseschema = self.fs_schema[etype] |
540fa73caac6
[migration] fix error when synchronizing everything and some schema still in the db schema but not anymore in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5132
diff
changeset
|
988 |
except KeyError: |
540fa73caac6
[migration] fix error when synchronizing everything and some schema still in the db schema but not anymore in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5132
diff
changeset
|
989 |
# entity type in the repository schema but not anymore |
540fa73caac6
[migration] fix error when synchronizing everything and some schema still in the db schema but not anymore in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5132
diff
changeset
|
990 |
# on the fs schema |
540fa73caac6
[migration] fix error when synchronizing everything and some schema still in the db schema but not anymore in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5132
diff
changeset
|
991 |
continue |
540fa73caac6
[migration] fix error when synchronizing everything and some schema still in the db schema but not anymore in the fs schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5132
diff
changeset
|
992 |
self._synchronize_permissions(fseschema, etype.eid) |
0 | 993 |
if commit: |
994 |
self.commit() |
|
1477 | 995 |
|
0 | 996 |
def cmd_change_relation_props(self, subjtype, rtype, objtype, |
997 |
commit=True, **kwargs): |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
998 |
"""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
|
999 |
|
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1000 |
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
|
1001 |
""" |
0 | 1002 |
assert kwargs |
1003 |
restriction = [] |
|
1004 |
if subjtype and subjtype != 'Any': |
|
1005 |
restriction.append('X from_entity FE, FE name "%s"' % subjtype) |
|
1006 |
if objtype and objtype != 'Any': |
|
1007 |
restriction.append('X to_entity TE, TE name "%s"' % objtype) |
|
1008 |
if rtype and rtype != 'Any': |
|
1009 |
restriction.append('X relation_type RT, RT name "%s"' % rtype) |
|
1010 |
assert restriction |
|
1011 |
values = [] |
|
1012 |
for k, v in kwargs.items(): |
|
1013 |
values.append('X %s %%(%s)s' % (k, k)) |
|
1014 |
if isinstance(v, str): |
|
1015 |
kwargs[k] = unicode(v) |
|
1016 |
rql = 'SET %s WHERE %s' % (','.join(values), ','.join(restriction)) |
|
1017 |
self.rqlexec(rql, kwargs, ask_confirm=self.verbosity>=2) |
|
1018 |
if commit: |
|
1019 |
self.commit() |
|
1020 |
||
1021 |
def cmd_set_size_constraint(self, etype, rtype, size, commit=True): |
|
1022 |
"""set change size constraint of a string attribute |
|
1023 |
||
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1024 |
if size is None any size constraint will be removed. |
1477 | 1025 |
|
1026 |
you usually want to use sync_schema_props_perms instead. |
|
0 | 1027 |
""" |
1028 |
oldvalue = None |
|
4695
4aaf87e7f79e
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4650
diff
changeset
|
1029 |
for constr in self.repo.schema.eschema(etype).rdef(rtype).constraints: |
0 | 1030 |
if isinstance(constr, SizeConstraint): |
1031 |
oldvalue = constr.max |
|
1032 |
if oldvalue == size: |
|
1033 |
return |
|
1034 |
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
|
1035 |
ceid = self.rqlexec('INSERT CWConstraint C: C value %(v)s, C cstrtype CT ' |
0 | 1036 |
'WHERE CT name "SizeConstraint"', |
1037 |
{'v': SizeConstraint(size).serialize()}, |
|
1038 |
ask_confirm=self.verbosity>=2)[0][0] |
|
1039 |
self.rqlexec('SET X constrained_by C WHERE X from_entity S, X relation_type R, ' |
|
1040 |
'S name "%s", R name "%s", C eid %s' % (etype, rtype, ceid), |
|
1041 |
ask_confirm=self.verbosity>=2) |
|
1042 |
elif not oldvalue is None: |
|
1043 |
if not size is None: |
|
1044 |
self.rqlexec('SET C value %%(v)s WHERE X from_entity S, X relation_type R,' |
|
1045 |
'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
|
1046 |
'S name "%s", R name "%s"' % (etype, rtype), |
|
1047 |
{'v': unicode(SizeConstraint(size).serialize())}, |
|
1048 |
ask_confirm=self.verbosity>=2) |
|
1049 |
else: |
|
1050 |
self.rqlexec('DELETE X constrained_by C WHERE X from_entity S, X relation_type R,' |
|
1051 |
'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
|
1052 |
'S name "%s", R name "%s"' % (etype, rtype), |
|
1053 |
ask_confirm=self.verbosity>=2) |
|
1054 |
# cleanup unused constraints |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1055 |
self.rqlexec('DELETE CWConstraint C WHERE NOT X constrained_by C') |
0 | 1056 |
if commit: |
1057 |
self.commit() |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1058 |
|
3577
067610a0e12f
add version number to deprecation messages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3548
diff
changeset
|
1059 |
@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
|
1060 |
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
|
1061 |
self.cmd_sync_schema_props_perms(ertype, syncprops=False, commit=commit) |
1477 | 1062 |
|
0 | 1063 |
# Workflows handling ###################################################### |
1477 | 1064 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1065 |
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
|
1066 |
**kwargs): |
3582
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1067 |
""" |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1068 |
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
|
1069 |
:type name: unicode |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1070 |
:param name: name of the workflow |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1071 |
|
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1072 |
:type wfof: string or list/tuple of strings |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1073 |
:param wfof: entity type(s) having this workflow |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1074 |
|
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1075 |
:type default: bool |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1076 |
:param default: tells wether this is the default workflow |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1077 |
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
|
1078 |
the case of a subworkflow |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1079 |
|
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1080 |
:rtype: `Workflow` |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1081 |
""" |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1082 |
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
|
1083 |
**kwargs) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1084 |
if not isinstance(wfof, (list, tuple)): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1085 |
wfof = (wfof,) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1086 |
for etype in wfof: |
2956
6a57c0be0e58
[migration] don't ask confirm here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2943
diff
changeset
|
1087 |
rset = self.rqlexec( |
6a57c0be0e58
[migration] don't ask confirm here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2943
diff
changeset
|
1088 |
'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
|
1089 |
{'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
|
1090 |
assert rset, 'unexistant entity type %s' % etype |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1091 |
if default: |
2956
6a57c0be0e58
[migration] don't ask confirm here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2943
diff
changeset
|
1092 |
self.rqlexec( |
6a57c0be0e58
[migration] don't ask confirm here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2943
diff
changeset
|
1093 |
'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
|
1094 |
{'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
|
1095 |
if commit: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1096 |
self.commit() |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1097 |
return wf |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1098 |
|
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1099 |
# 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
|
1100 |
def _get_or_create_wf(self, etypes): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1101 |
if not isinstance(etypes, (list, tuple)): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1102 |
etypes = (etypes,) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1103 |
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
|
1104 |
{'et': etypes[0]}) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1105 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1106 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1107 |
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
|
1108 |
|
4339
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4330
diff
changeset
|
1109 |
@deprecated('[3.5] use add_workflow and Workflow.add_state method', |
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4330
diff
changeset
|
1110 |
stacklevel=3) |
0 | 1111 |
def cmd_add_state(self, name, stateof, initial=False, commit=False, **kwargs): |
1112 |
"""method to ease workflow definition: add a state for one or more |
|
1113 |
entity type(s) |
|
1114 |
""" |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1115 |
wf = self._get_or_create_wf(stateof) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1116 |
state = wf.add_state(name, initial, **kwargs) |
0 | 1117 |
if commit: |
1118 |
self.commit() |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1119 |
return state.eid |
1477 | 1120 |
|
4339
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4330
diff
changeset
|
1121 |
@deprecated('[3.5] use add_workflow and Workflow.add_transition method', |
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4330
diff
changeset
|
1122 |
stacklevel=3) |
0 | 1123 |
def cmd_add_transition(self, name, transitionof, fromstates, tostate, |
1124 |
requiredgroups=(), conditions=(), commit=False, **kwargs): |
|
1125 |
"""method to ease workflow definition: add a transition for one or more |
|
1126 |
entity type(s), from one or more state and to a single state |
|
1127 |
""" |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1128 |
wf = self._get_or_create_wf(transitionof) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1129 |
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
|
1130 |
conditions, **kwargs) |
0 | 1131 |
if commit: |
1132 |
self.commit() |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1133 |
return tr.eid |
0 | 1134 |
|
4339
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4330
diff
changeset
|
1135 |
@deprecated('[3.5] use Transition.set_transition_permissions method', |
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4330
diff
changeset
|
1136 |
stacklevel=3) |
0 | 1137 |
def cmd_set_transition_permissions(self, treid, |
1138 |
requiredgroups=(), conditions=(), |
|
1139 |
reset=True, commit=False): |
|
1140 |
"""set or add (if `reset` is False) groups and conditions for a |
|
1141 |
transition |
|
1142 |
""" |
|
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1143 |
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
|
1144 |
tr.set_transition_permissions(requiredgroups, conditions, reset) |
0 | 1145 |
if commit: |
1146 |
self.commit() |
|
1147 |
||
4339
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4330
diff
changeset
|
1148 |
@deprecated('[3.5] use entity.fire_transition("transition") or entity.change_state("state")', |
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4330
diff
changeset
|
1149 |
stacklevel=3) |
972 | 1150 |
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
|
1151 |
self._cw.entity_from_eid(eid).change_state(statename) |
972 | 1152 |
if commit: |
1153 |
self.commit() |
|
1477 | 1154 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1155 |
# CWProperty handling ###################################################### |
0 | 1156 |
|
1157 |
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
|
1158 |
rql = 'Any V WHERE X is CWProperty, X pkey %(k)s, X value V' |
0 | 1159 |
rset = self.rqlexec(rql, {'k': pkey}, ask_confirm=False) |
1160 |
return rset[0][0] |
|
1161 |
||
1162 |
def cmd_set_property(self, pkey, value): |
|
1163 |
value = unicode(value) |
|
1164 |
try: |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1165 |
prop = self.rqlexec('CWProperty X WHERE X pkey %(k)s', {'k': pkey}, |
0 | 1166 |
ask_confirm=False).get_entity(0, 0) |
1167 |
except: |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1168 |
self.cmd_create_entity('CWProperty', pkey=unicode(pkey), value=value) |
0 | 1169 |
else: |
1170 |
self.rqlexec('SET X value %(v)s WHERE X pkey %(k)s', |
|
1171 |
{'k': pkey, 'v': value}, ask_confirm=False) |
|
1172 |
||
1173 |
# other data migration commands ########################################### |
|
1477 | 1174 |
|
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1175 |
@property |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1176 |
def _cw(self): |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1177 |
session = self.session |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1178 |
if session is not None: |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1179 |
session.set_pool() |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1180 |
return session |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1181 |
return self.cnx.request() |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1182 |
|
5132
260d73ad4f24
[cleaning] simpler implementation of cmd_create_entity
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5043
diff
changeset
|
1183 |
def cmd_create_entity(self, etype, commit=False, **kwargs): |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1184 |
"""add a new entity of the given type""" |
3708
95e8c3a9698a
update prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3707
diff
changeset
|
1185 |
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
|
1186 |
if commit: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1187 |
self.commit() |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1188 |
return entity |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1189 |
|
4367
fa02f7dccfe4
fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4344
diff
changeset
|
1190 |
@deprecated('[3.5] use create_entity', stacklevel=3) |
0 | 1191 |
def cmd_add_entity(self, etype, *args, **kwargs): |
1192 |
"""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
|
1193 |
return self.cmd_create_entity(etype, *args, **kwargs).eid |
1477 | 1194 |
|
0 | 1195 |
def sqlexec(self, sql, args=None, ask_confirm=True): |
1196 |
"""execute the given sql if confirmed |
|
1477 | 1197 |
|
0 | 1198 |
should only be used for low level stuff undoable with existing higher |
1199 |
level actions |
|
1200 |
""" |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
1201 |
if not ask_confirm or self.confirm('Execute sql: %s ?' % sql): |
0 | 1202 |
try: |
1203 |
cu = self.session.system_sql(sql, args) |
|
1204 |
except: |
|
1205 |
ex = sys.exc_info()[1] |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
1206 |
if self.confirm('Error: %s\nabort?' % ex): |
0 | 1207 |
raise |
1208 |
return |
|
1209 |
try: |
|
1210 |
return cu.fetchall() |
|
1211 |
except: |
|
1212 |
# no result to fetch |
|
1213 |
return |
|
1477 | 1214 |
|
4945
356662a6f06c
[migration] new build_descr argument to rqlexec on the migration helper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4925
diff
changeset
|
1215 |
def rqlexec(self, rql, kwargs=None, cachekey=None, build_descr=True, |
356662a6f06c
[migration] new build_descr argument to rqlexec on the migration helper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4925
diff
changeset
|
1216 |
ask_confirm=True): |
0 | 1217 |
"""rql action""" |
1218 |
if not isinstance(rql, (tuple, list)): |
|
1219 |
rql = ( (rql, kwargs), ) |
|
1220 |
res = None |
|
4835
13b0b96d7982
[repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4834
diff
changeset
|
1221 |
execute = self._cw.execute |
0 | 1222 |
for rql, kwargs in rql: |
1223 |
if kwargs: |
|
1224 |
msg = '%s (%s)' % (rql, kwargs) |
|
1225 |
else: |
|
1226 |
msg = rql |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
1227 |
if not ask_confirm or self.confirm('Execute rql: %s ?' % msg): |
0 | 1228 |
try: |
4945
356662a6f06c
[migration] new build_descr argument to rqlexec on the migration helper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4925
diff
changeset
|
1229 |
res = execute(rql, kwargs, cachekey, build_descr=build_descr) |
0 | 1230 |
except Exception, ex: |
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
1231 |
if self.confirm('Error: %s\nabort?' % ex): |
0 | 1232 |
raise |
1233 |
return res |
|
1234 |
||
1235 |
def rqliter(self, rql, kwargs=None, ask_confirm=True): |
|
1236 |
return ForRqlIterator(self, rql, None, ask_confirm) |
|
1237 |
||
1238 |
# broken db commands ###################################################### |
|
1239 |
||
1240 |
def cmd_change_attribute_type(self, etype, attr, newtype, commit=True): |
|
1241 |
"""low level method to change the type of an entity attribute. This is |
|
1242 |
a quick hack which has some drawback: |
|
1243 |
* only works when the old type can be changed to the new type by the |
|
1244 |
underlying rdbms (eg using ALTER TABLE) |
|
1245 |
* the actual schema won't be updated until next startup |
|
1246 |
""" |
|
1247 |
rschema = self.repo.schema.rschema(attr) |
|
1248 |
oldtype = rschema.objects(etype)[0] |
|
1249 |
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
|
1250 |
sql = ("UPDATE CWAttribute " |
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1251 |
"SET to_entity=(SELECT eid FROM CWEType WHERE name='%s')" |
0 | 1252 |
"WHERE eid=%s") % (newtype, rdefeid) |
1253 |
self.sqlexec(sql, ask_confirm=False) |
|
1254 |
dbhelper = self.repo.system_source.dbhelper |
|
1255 |
sqltype = dbhelper.TYPE_MAPPING[newtype] |
|
1256 |
sql = 'ALTER TABLE %s ALTER COLUMN %s TYPE %s' % (etype, attr, sqltype) |
|
1257 |
self.sqlexec(sql, ask_confirm=False) |
|
1258 |
if commit: |
|
1259 |
self.commit() |
|
1477 | 1260 |
|
0 | 1261 |
def cmd_add_entity_type_table(self, etype, commit=True): |
1262 |
"""low level method to create the sql table for an existing entity. |
|
1263 |
This may be useful on accidental desync between the repository schema |
|
1264 |
and a sql database |
|
1265 |
""" |
|
1266 |
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
|
1267 |
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
|
1268 |
prefix=SQL_PREFIX) |
0 | 1269 |
for sql in tablesql.split(';'): |
1270 |
if sql.strip(): |
|
1271 |
self.sqlexec(sql) |
|
1272 |
if commit: |
|
1273 |
self.commit() |
|
1477 | 1274 |
|
0 | 1275 |
def cmd_add_relation_type_table(self, rtype, commit=True): |
1276 |
"""low level method to create the sql table for an existing relation. |
|
1277 |
This may be useful on accidental desync between the repository schema |
|
1278 |
and a sql database |
|
1279 |
""" |
|
1280 |
dbhelper = self.repo.system_source.dbhelper |
|
1281 |
tablesql = rschema2sql(dbhelper, self.repo.schema.rschema(rtype)) |
|
1282 |
for sql in tablesql.split(';'): |
|
1283 |
if sql.strip(): |
|
1284 |
self.sqlexec(sql) |
|
1285 |
if commit: |
|
1286 |
self.commit() |
|
1477 | 1287 |
|
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1288 |
@deprecated("[3.7] use session.disable_hook_categories('integrity')") |
4834
b718626a0e60
move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4763
diff
changeset
|
1289 |
def cmd_deactivate_verification_hooks(self): |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1290 |
self.session.disable_hook_categories('integrity') |
4834
b718626a0e60
move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4763
diff
changeset
|
1291 |
|
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1292 |
@deprecated("[3.7] use session.enable_hook_categories('integrity')") |
4834
b718626a0e60
move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4763
diff
changeset
|
1293 |
def cmd_reactivate_verification_hooks(self): |
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1294 |
self.session.enable_hook_categories('integrity') |
4834
b718626a0e60
move hooks activation control on session object, so we can have a per transaction control. Added a new `hooks_control` context manager for usual modification of hooks activation.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4763
diff
changeset
|
1295 |
|
0 | 1296 |
|
1297 |
class ForRqlIterator: |
|
1298 |
"""specific rql iterator to make the loop skipable""" |
|
1299 |
def __init__(self, helper, rql, kwargs, ask_confirm): |
|
1300 |
self._h = helper |
|
1301 |
self.rql = rql |
|
1302 |
self.kwargs = kwargs |
|
1303 |
self.ask_confirm = ask_confirm |
|
1304 |
self._rsetit = None |
|
1477 | 1305 |
|
0 | 1306 |
def __iter__(self): |
1307 |
return self |
|
1477 | 1308 |
|
0 | 1309 |
def next(self): |
1310 |
if self._rsetit is not None: |
|
1311 |
return self._rsetit.next() |
|
1312 |
rql, kwargs = self.rql, self.kwargs |
|
1313 |
if kwargs: |
|
1314 |
msg = '%s (%s)' % (rql, kwargs) |
|
1315 |
else: |
|
1316 |
msg = rql |
|
1317 |
if self.ask_confirm: |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
1318 |
if not self._h.confirm('Execute rql: %s ?' % msg): |
0 | 1319 |
raise StopIteration |
1320 |
try: |
|
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1321 |
rset = self._h._cw.execute(rql, kwargs) |
0 | 1322 |
except Exception, ex: |
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
1323 |
if self._h.confirm('Error: %s\nabort?' % ex): |
0 | 1324 |
raise |
1325 |
else: |
|
1326 |
raise StopIteration |
|
1327 |
self._rsetit = iter(rset) |
|
1328 |
return self._rsetit.next() |