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