author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 27 Jul 2011 14:22:32 +0200 | |
branch | stable |
changeset 7707 | 936530f8d32c |
parent 7562 | cdef82ca9eab |
child 7780 | a1d5365fefc1 |
permissions | -rw-r--r-- |
7116
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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
|
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 |
|
5850
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5666
diff
changeset
|
28 |
""" |
0 | 29 |
|
4925
0d66fbe050c6
[migration] disable notification by default during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4905
diff
changeset
|
30 |
from __future__ import with_statement |
0d66fbe050c6
[migration] disable notification by default during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4905
diff
changeset
|
31 |
|
0 | 32 |
__docformat__ = "restructuredtext en" |
33 |
||
34 |
import sys |
|
35 |
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
|
36 |
import tarfile |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
37 |
import tempfile |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
38 |
import shutil |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
39 |
import os.path as osp |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
972
diff
changeset
|
40 |
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
|
41 |
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
|
42 |
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
|
43 |
from warnings import warn |
6292
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
44 |
from contextlib import contextmanager |
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 |
0 | 48 |
|
49 |
from yams.constraints import SizeConstraint |
|
50 |
from yams.schema2sql import eschema2sql, rschema2sql |
|
6292
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
51 |
from yams.schema import RelationDefinitionSchema |
0 | 52 |
|
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5999
diff
changeset
|
53 |
from cubicweb import AuthenticationError, ExecutionError |
6200
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
54 |
from cubicweb.selectors import is_instance |
5558
afd1face1faf
[schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
55 |
from cubicweb.schema import (ETYPE_NAME_MAP, META_RTYPES, VIRTUAL_RTYPES, |
afd1face1faf
[schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
56 |
PURE_VIRTUAL_RTYPES, |
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
|
57 |
CubicWebRelationSchema, order_eschemas) |
6200
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
58 |
from cubicweb.cwvreg import CW_EVENT_MANAGER |
0 | 59 |
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
|
60 |
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
|
61 |
from cubicweb.server.session import hooks_control |
6190
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
62 |
from cubicweb.server import hook |
0 | 63 |
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
|
64 |
from cubicweb.server import SOURCE_TYPES, schemaserial as ss |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6426
diff
changeset
|
65 |
from cubicweb.server.utils import manager_userpasswd |
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 |
from cubicweb.server.sqlutils import sqlexec, SQL_PREFIX |
0 | 67 |
except ImportError: # LAX |
68 |
pass |
|
69 |
||
6571
71b446d1cf93
[dependancy] do not use testlib from outside test: recent lgc versions recommends unittest2 while lgc.testlib needs it, and we don't want it to be necessary to run cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6323
diff
changeset
|
70 |
|
71b446d1cf93
[dependancy] do not use testlib from outside test: recent lgc versions recommends unittest2 while lgc.testlib needs it, and we don't want it to be necessary to run cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6323
diff
changeset
|
71 |
def mock_object(**params): |
71b446d1cf93
[dependancy] do not use testlib from outside test: recent lgc versions recommends unittest2 while lgc.testlib needs it, and we don't want it to be necessary to run cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6323
diff
changeset
|
72 |
return type('Mock', (), params)() |
71b446d1cf93
[dependancy] do not use testlib from outside test: recent lgc versions recommends unittest2 while lgc.testlib needs it, and we don't want it to be necessary to run cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6323
diff
changeset
|
73 |
|
6200
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
74 |
class ClearGroupMap(hook.Hook): |
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
75 |
__regid__ = 'cw.migration.clear_group_mapping' |
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
76 |
__select__ = hook.Hook.__select__ & is_instance('CWGroup') |
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
77 |
events = ('after_add_entity', 'after_update_entity',) |
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
78 |
def __call__(self): |
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
79 |
clear_cache(self.mih, 'group_mapping') |
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
80 |
self.mih._synchronized.clear() |
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
|
81 |
|
6205
41e0e13e10b7
[test] fix in 6203:d3dea5f84404 wasn't enough
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6203
diff
changeset
|
82 |
@classmethod |
41e0e13e10b7
[test] fix in 6203:d3dea5f84404 wasn't enough
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6203
diff
changeset
|
83 |
def mih_register(cls, repo): |
41e0e13e10b7
[test] fix in 6203:d3dea5f84404 wasn't enough
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6203
diff
changeset
|
84 |
# may be already registered in tests (e.g. unittest_migractions at |
41e0e13e10b7
[test] fix in 6203:d3dea5f84404 wasn't enough
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6203
diff
changeset
|
85 |
# least) |
41e0e13e10b7
[test] fix in 6203:d3dea5f84404 wasn't enough
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6203
diff
changeset
|
86 |
if not cls.__regid__ in repo.vreg['after_add_entity_hooks']: |
41e0e13e10b7
[test] fix in 6203:d3dea5f84404 wasn't enough
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6203
diff
changeset
|
87 |
repo.vreg.register(ClearGroupMap) |
41e0e13e10b7
[test] fix in 6203:d3dea5f84404 wasn't enough
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6203
diff
changeset
|
88 |
|
0 | 89 |
class ServerMigrationHelper(MigrationHelper): |
90 |
"""specific migration helper for server side migration scripts, |
|
91 |
providind actions related to schema/data migration |
|
92 |
""" |
|
93 |
||
94 |
def __init__(self, config, schema, interactive=True, |
|
95 |
repo=None, cnx=None, verbosity=1, connect=True): |
|
96 |
MigrationHelper.__init__(self, config, interactive, verbosity) |
|
97 |
if not interactive: |
|
98 |
assert cnx |
|
99 |
assert repo |
|
100 |
if cnx is not None: |
|
101 |
assert repo |
|
102 |
self._cnx = cnx |
|
103 |
self.repo = repo |
|
104 |
elif connect: |
|
105 |
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
|
106 |
# 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
|
107 |
if config is not None and (cnx or connect): |
6203
d3dea5f84404
[test] during some tests (unittest_migrations), we should not register ClearGroupMap which is actually already there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6200
diff
changeset
|
108 |
repo = self.repo |
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
|
109 |
self.session.data['rebuild-infered'] = False |
6200
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
110 |
# register a hook to clear our group_mapping cache and the |
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
111 |
# self._synchronized set when some group is added or updated |
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
112 |
ClearGroupMap.mih = self |
6205
41e0e13e10b7
[test] fix in 6203:d3dea5f84404 wasn't enough
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6203
diff
changeset
|
113 |
ClearGroupMap.mih_register(repo) |
41e0e13e10b7
[test] fix in 6203:d3dea5f84404 wasn't enough
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6203
diff
changeset
|
114 |
CW_EVENT_MANAGER.bind('after-registry-reload', |
41e0e13e10b7
[test] fix in 6203:d3dea5f84404 wasn't enough
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6203
diff
changeset
|
115 |
ClearGroupMap.mih_register, repo) |
6200
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
116 |
# notify we're starting maintenance (called instead of server_start |
6e8c847ae397
[migration] when some CWGroup is added during migration, we should reset migration handler's group mapping cache and the set telling what have been synchronized (we may be able to add some new groups on perms syncing...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6190
diff
changeset
|
117 |
# which is called on regular start |
6203
d3dea5f84404
[test] during some tests (unittest_migrations), we should not register ClearGroupMap which is actually already there
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6200
diff
changeset
|
118 |
repo.hm.call_hooks('server_maintenance', repo=repo) |
5043
fe52dd3936cf
[repo config] cleanup read_instance_schema / bootstrap_schema / creating mess
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5034
diff
changeset
|
119 |
if not schema and not getattr(config, 'quick_start', False): |
0 | 120 |
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
|
121 |
self.fs_schema = schema |
0 | 122 |
self._synchronized = set() |
123 |
||
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
124 |
# overriden from base MigrationHelper ###################################### |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
125 |
|
0 | 126 |
@cached |
127 |
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
|
128 |
self.repo = get_repository(method='inmemory', config=self.config) |
0 | 129 |
return self.repo |
1477 | 130 |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
131 |
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
|
132 |
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
|
133 |
unicode(version)) |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
134 |
self.commit() |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
135 |
|
0 | 136 |
def shutdown(self): |
137 |
if self.repo is not None: |
|
138 |
self.repo.shutdown() |
|
1477 | 139 |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
self.backup_database() |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
144 |
elif options.backup_db: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
145 |
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
|
146 |
# disable notification during migration |
0d66fbe050c6
[migration] disable notification by default during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4905
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
|
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
|
150 |
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
|
151 |
try: |
6042
df9cafb8062c
[migration] commit only sql script migration
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6035
diff
changeset
|
152 |
return super(ServerMigrationHelper, self).cmd_process_script( |
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5999
diff
changeset
|
153 |
migrscript, funcname, *args, **kwargs) |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5999
diff
changeset
|
154 |
except ExecutionError, err: |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5999
diff
changeset
|
155 |
print >> sys.stderr, "-> %s" % err |
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
156 |
except: |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
157 |
self.rollback() |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
158 |
raise |
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
159 |
|
6035
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5999
diff
changeset
|
160 |
# Adjust docstring |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5999
diff
changeset
|
161 |
cmd_process_script.__doc__ = MigrationHelper.cmd_process_script.__doc__ |
f8c7aa251782
[migration] unify process_script command
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
5999
diff
changeset
|
162 |
|
2275
bc0bed0616a3
fix #344387, remember upgraded version step by step
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2124
diff
changeset
|
163 |
# server specific migration methods ######################################## |
1477 | 164 |
|
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
165 |
def backup_database(self, backupfile=None, askconfirm=True, format='native'): |
0 | 166 |
config = self.config |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2489
diff
changeset
|
167 |
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
|
168 |
# paths |
3192 | 169 |
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
|
170 |
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
|
171 |
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
|
172 |
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
|
173 |
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
|
174 |
% (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
|
175 |
# 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
|
176 |
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
|
177 |
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
|
178 |
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
|
179 |
return |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
180 |
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
|
181 |
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
|
182 |
return |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
183 |
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
|
184 |
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
|
185 |
# backup |
6918
9e607157d4cf
[cw-ctl] use default `mkdtemp()` behaviour to store backup file internally
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6816
diff
changeset
|
186 |
tmpdir = tempfile.mkdtemp() |
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
|
187 |
try: |
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
188 |
failed = False |
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
|
189 |
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
|
190 |
try: |
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
191 |
source.backup(osp.join(tmpdir, source.uri), self.confirm, format=format) |
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
|
192 |
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
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
else: |
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
197 |
failed = True |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
198 |
with open(osp.join(tmpdir, 'format.txt'), 'w') as format_file: |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
199 |
format_file.write('%s\n' % format) |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
200 |
with open(osp.join(tmpdir, 'versions.txt'), 'w') as version_file: |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
201 |
versions = repo.get_versions() |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
202 |
for cube, version in versions.iteritems(): |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
203 |
version_file.write('%s %s\n' % (cube, version)) |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
204 |
if not failed: |
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
|
205 |
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
|
206 |
for filename in os.listdir(tmpdir): |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
207 |
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
|
208 |
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
|
209 |
# 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
|
210 |
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
|
211 |
# 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
|
212 |
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
|
213 |
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
|
214 |
shutil.rmtree(tmpdir) |
1477 | 215 |
|
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2489
diff
changeset
|
216 |
def restore_database(self, backupfile, drop=True, systemonly=True, |
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
217 |
askconfirm=True, format='native'): |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
218 |
# check |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
219 |
if not osp.exists(backupfile): |
6323
a11c1e3c16c3
[c-c shell/upgrade] raise ExecutionError when traceback is not relevant
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6217
diff
changeset
|
220 |
raise ExecutionError("Backup file %s doesn't exist" % backupfile) |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
221 |
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
|
222 |
% (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
|
223 |
return |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
224 |
# unpack backup |
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
225 |
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
|
226 |
try: |
b788903e77d5
be able to restore pre cw 3.4 database dumps #370595
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2963
diff
changeset
|
227 |
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
|
228 |
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
|
229 |
# assume restoring old backup |
4344
066e7884e57d
add source in backup/restore failure message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
230 |
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
|
231 |
else: |
b788903e77d5
be able to restore pre cw 3.4 database dumps #370595
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2963
diff
changeset
|
232 |
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
|
233 |
if name[0] in '/.': |
6323
a11c1e3c16c3
[c-c shell/upgrade] raise ExecutionError when traceback is not relevant
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6217
diff
changeset
|
234 |
raise ExecutionError('Security check failed, path starts with "/" or "."') |
3105
b788903e77d5
be able to restore pre cw 3.4 database dumps #370595
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2963
diff
changeset
|
235 |
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
|
236 |
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
|
237 |
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
|
238 |
bkup.close() |
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
239 |
if osp.isfile(osp.join(tmpdir, 'format.txt')): |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
240 |
with open(osp.join(tmpdir, 'format.txt')) as format_file: |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
241 |
written_format = format_file.readline().strip() |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
242 |
if written_format in ('portable', 'native'): |
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
243 |
format = written_format |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
244 |
self.config.init_cnxset_pool = False |
2959
daabb9bc5233
make db-restore command work even with no/corrupted database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2926
diff
changeset
|
245 |
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
|
246 |
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
|
247 |
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
|
248 |
continue |
9cb3027407aa
B offer to continue even when failed to restore systemonly
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2759
diff
changeset
|
249 |
try: |
7342
d1c8b5b3531c
adds support for a portable db import/export format (closes: #1521112)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7295
diff
changeset
|
250 |
source.restore(osp.join(tmpdir, source.uri), self.confirm, drop, format) |
2861
9cb3027407aa
B offer to continue even when failed to restore systemonly
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2759
diff
changeset
|
251 |
except Exception, exc: |
4344
066e7884e57d
add source in backup/restore failure message
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4339
diff
changeset
|
252 |
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
|
253 |
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
|
254 |
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
|
255 |
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
|
256 |
# call hooks |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
257 |
repo.init_cnxset_pool() |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
258 |
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
|
259 |
print '-> database restored.' |
1477 | 260 |
|
0 | 261 |
@property |
262 |
def cnx(self): |
|
263 |
"""lazy connection""" |
|
264 |
try: |
|
265 |
return self._cnx |
|
266 |
except AttributeError: |
|
267 |
sourcescfg = self.repo.config.sources() |
|
268 |
try: |
|
269 |
login = sourcescfg['admin']['login'] |
|
270 |
pwd = sourcescfg['admin']['password'] |
|
271 |
except KeyError: |
|
272 |
login, pwd = manager_userpasswd() |
|
273 |
while True: |
|
274 |
try: |
|
3659 | 275 |
self._cnx = repo_connect(self.repo, login, password=pwd) |
0 | 276 |
if not 'managers' in self._cnx.user(self.session).groups: |
277 |
print 'migration need an account in the managers group' |
|
278 |
else: |
|
279 |
break |
|
280 |
except AuthenticationError: |
|
281 |
print 'wrong user/password' |
|
282 |
except (KeyboardInterrupt, EOFError): |
|
283 |
print 'aborting...' |
|
284 |
sys.exit(0) |
|
285 |
try: |
|
286 |
login, pwd = manager_userpasswd() |
|
287 |
except (KeyboardInterrupt, EOFError): |
|
288 |
print 'aborting...' |
|
289 |
sys.exit(0) |
|
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
290 |
self.session.keep_cnxset_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
|
291 |
self.session.data['rebuild-infered'] = False |
0 | 292 |
return self._cnx |
293 |
||
294 |
@property |
|
295 |
def session(self): |
|
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
296 |
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
|
297 |
session = self.repo._get_session(self.cnx.sessionid) |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
298 |
if session.cnxset is 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
|
299 |
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
|
300 |
session.set_write_security(False) |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
301 |
session.set_cnxset() |
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
|
302 |
return session |
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
303 |
# 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
|
304 |
return None |
1477 | 305 |
|
0 | 306 |
def commit(self): |
307 |
if hasattr(self, '_cnx'): |
|
308 |
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
|
309 |
if self.session: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
310 |
self.session.set_cnxset() |
1477 | 311 |
|
0 | 312 |
def rollback(self): |
313 |
if hasattr(self, '_cnx'): |
|
314 |
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
|
315 |
if self.session: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
316 |
self.session.set_cnxset() |
1477 | 317 |
|
6186
8ada7df95877
[migration] set ask_confirm=False by default on rqlexec
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6179
diff
changeset
|
318 |
def rqlexecall(self, rqliter, ask_confirm=False): |
0 | 319 |
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
|
320 |
self.rqlexec(rql, kwargs, ask_confirm=ask_confirm) |
0 | 321 |
|
322 |
@cached |
|
323 |
def _create_context(self): |
|
324 |
"""return a dictionary to use as migration script execution context""" |
|
325 |
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
|
326 |
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
|
327 |
'rollback': self.rollback, |
4017
a5b4d4f2a1c7
use commit in migration script instead of checkpoint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4011
diff
changeset
|
328 |
'checkpoint': deprecated('[3.6] use commit')(self.checkpoint), |
0 | 329 |
'sql': self.sqlexec, |
330 |
'rql': self.rqlexec, |
|
331 |
'rqliter': self.rqliter, |
|
3707
78596919ede3
[c-c] fixes for shell w/ pyro instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3700
diff
changeset
|
332 |
'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
|
333 |
'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
|
334 |
'fsschema': self.fs_schema, |
0 | 335 |
'session' : self.session, |
336 |
'repo' : self.repo, |
|
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2759
diff
changeset
|
337 |
'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
|
338 |
'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
|
339 |
'synchronize_rschema': deprecated()(self.cmd_sync_schema_props_perms), # 3.4 |
0 | 340 |
}) |
341 |
return context |
|
342 |
||
343 |
@cached |
|
344 |
def group_mapping(self): |
|
345 |
"""cached group mapping""" |
|
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
346 |
return ss.group_mapping(self._cw) |
1477 | 347 |
|
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
|
348 |
@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
|
349 |
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
|
350 |
"""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
|
351 |
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
|
352 |
|
0 | 353 |
def exec_event_script(self, event, cubepath=None, funcname=None, |
1477 | 354 |
*args, **kwargs): |
0 | 355 |
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
|
356 |
apc = osp.join(cubepath, 'migration', '%s.py' % event) |
0 | 357 |
else: |
2759
23d7a75693f8
R refactor backup and use tar.gz to store all sources
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2700
diff
changeset
|
358 |
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
|
359 |
if osp.exists(apc): |
0 | 360 |
if self.config.free_wheel: |
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
361 |
self.cmd_deactivate_verification_hooks() |
0 | 362 |
self.info('executing %s', apc) |
363 |
confirm = self.confirm |
|
364 |
execscript_confirm = self.execscript_confirm |
|
365 |
self.confirm = yes |
|
366 |
self.execscript_confirm = yes |
|
367 |
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
|
368 |
return self.cmd_process_script(apc, funcname, *args, **kwargs) |
0 | 369 |
finally: |
370 |
self.confirm = confirm |
|
371 |
self.execscript_confirm = execscript_confirm |
|
372 |
if self.config.free_wheel: |
|
2835
04034421b072
[hooks] major refactoring:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
373 |
self.cmd_reactivate_verification_hooks() |
0 | 374 |
|
3903
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
375 |
def install_custom_sql_scripts(self, directory, driver): |
7479
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7412
diff
changeset
|
376 |
sql_scripts = [] |
3903
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
377 |
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
|
378 |
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
|
379 |
'.%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
|
380 |
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
|
381 |
DeprecationWarning) |
7479
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7412
diff
changeset
|
382 |
sql_scripts.append(fpath) |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7412
diff
changeset
|
383 |
sql_scripts += glob(osp.join(directory, '*.%s.sql' % driver)) |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7412
diff
changeset
|
384 |
for fpath in sql_scripts: |
3903
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
385 |
print '-> installing', fpath |
7479
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7412
diff
changeset
|
386 |
try: |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7412
diff
changeset
|
387 |
sqlexec(open(fpath).read(), self.session.system_sql, False, |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7412
diff
changeset
|
388 |
delimiter=';;') |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7412
diff
changeset
|
389 |
except Exception, exc: |
b0603fc4ed6d
[sql] implement #1631319: ask user before creating postgresql languages
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
7412
diff
changeset
|
390 |
print '-> ERROR:', exc, ', skipping', fpath |
3903
7967d3766ecf
fix #544758 by calling custom sql scripts in add_cubes.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3872
diff
changeset
|
391 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
392 |
# schema synchronization internals ######################################## |
0 | 393 |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
394 |
def _synchronize_permissions(self, erschema, teid): |
0 | 395 |
"""permission synchronization for an entity or relation type""" |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
396 |
assert teid, erschema |
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
397 |
if 'update' in erschema.ACTIONS or erschema.final: |
0 | 398 |
# entity type |
399 |
exprtype = u'ERQLExpression' |
|
400 |
else: |
|
401 |
# relation type |
|
402 |
exprtype = u'RRQLExpression' |
|
403 |
gm = self.group_mapping() |
|
404 |
confirm = self.verbosity >= 2 |
|
405 |
# * remove possibly deprecated permission (eg in the persistent schema |
|
406 |
# but not in the new schema) |
|
407 |
# * synchronize existing expressions |
|
408 |
# * add new groups/expressions |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
409 |
for action in erschema.ACTIONS: |
0 | 410 |
perm = '%s_permission' % action |
411 |
# handle groups |
|
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
412 |
newgroups = list(erschema.get_groups(action)) |
0 | 413 |
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
|
414 |
'T eid %%(x)s' % perm, {'x': teid}, |
0 | 415 |
ask_confirm=False): |
416 |
if not gname in newgroups: |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
417 |
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
|
418 |
% (action, erschema, gname)): |
0 | 419 |
self.rqlexec('DELETE T %s G WHERE G eid %%(x)s, T eid %s' |
420 |
% (perm, teid), |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5132
diff
changeset
|
421 |
{'x': geid}, ask_confirm=False) |
0 | 422 |
else: |
423 |
newgroups.remove(gname) |
|
424 |
for gname in newgroups: |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
425 |
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
|
426 |
% (action, erschema, gname)): |
6075
6ebecb217efe
[migration] dont crash if a group doesn't exists (it may not exists yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6074
diff
changeset
|
427 |
try: |
6ebecb217efe
[migration] dont crash if a group doesn't exists (it may not exists yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6074
diff
changeset
|
428 |
self.rqlexec('SET T %s G WHERE G eid %%(x)s, T eid %s' |
6ebecb217efe
[migration] dont crash if a group doesn't exists (it may not exists yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6074
diff
changeset
|
429 |
% (perm, teid), |
6ebecb217efe
[migration] dont crash if a group doesn't exists (it may not exists yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6074
diff
changeset
|
430 |
{'x': gm[gname]}, ask_confirm=False) |
6ebecb217efe
[migration] dont crash if a group doesn't exists (it may not exists yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6074
diff
changeset
|
431 |
except KeyError: |
6ebecb217efe
[migration] dont crash if a group doesn't exists (it may not exists yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6074
diff
changeset
|
432 |
self.error('can grant %s perm to unexistant group %s', |
6ebecb217efe
[migration] dont crash if a group doesn't exists (it may not exists yet)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6074
diff
changeset
|
433 |
action, gname) |
0 | 434 |
# handle rql expressions |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
435 |
newexprs = dict((expr.expression, expr) for expr in erschema.get_rqlexprs(action)) |
0 | 436 |
for expreid, expression in self.rqlexec('Any E, EX WHERE T %s E, E expression EX, ' |
437 |
'T eid %s' % (perm, teid), |
|
438 |
ask_confirm=False): |
|
439 |
if not expression in newexprs: |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
440 |
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
|
441 |
% (expression, action, erschema)): |
0 | 442 |
# deleting the relation will delete the expression entity |
443 |
self.rqlexec('DELETE T %s E WHERE E eid %%(x)s, T eid %s' |
|
444 |
% (perm, teid), |
|
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5132
diff
changeset
|
445 |
{'x': expreid}, ask_confirm=False) |
0 | 446 |
else: |
447 |
newexprs.pop(expression) |
|
448 |
for expression in newexprs.values(): |
|
449 |
expr = expression.expression |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
450 |
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
|
451 |
% (expr, action, erschema)): |
0 | 452 |
self.rqlexec('INSERT RQLExpression X: X exprtype %%(exprtype)s, ' |
453 |
'X expression %%(expr)s, X mainvars %%(vars)s, T %s X ' |
|
454 |
'WHERE T eid %%(x)s' % perm, |
|
455 |
{'expr': expr, 'exprtype': exprtype, |
|
7161
e3f69df8dac7
fix failing tests before 3.12 release
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7116
diff
changeset
|
456 |
'vars': u','.join(sorted(expression.mainvars)), |
e3f69df8dac7
fix failing tests before 3.12 release
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
7116
diff
changeset
|
457 |
'x': teid}, |
0 | 458 |
ask_confirm=False) |
1477 | 459 |
|
5666
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
460 |
def _synchronize_rschema(self, rtype, syncrdefs=True, |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
461 |
syncperms=True, syncprops=True): |
0 | 462 |
"""synchronize properties of the persistent relation schema against its |
463 |
current definition: |
|
1477 | 464 |
|
0 | 465 |
* 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
|
466 |
* symmetric, meta |
0 | 467 |
* inlined |
468 |
* relation definitions if `syncrdefs` |
|
469 |
* permissions if `syncperms` |
|
1477 | 470 |
|
0 | 471 |
physical schema changes should be handled by repository's schema hooks |
472 |
""" |
|
473 |
rtype = str(rtype) |
|
474 |
if rtype in self._synchronized: |
|
475 |
return |
|
476 |
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
|
477 |
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
|
478 |
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
|
479 |
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
|
480 |
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
|
481 |
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
|
482 |
ask_confirm=self.verbosity>=2) |
0 | 483 |
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
|
484 |
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
|
485 |
if (subj, obj) not in reporschema.rdefs: |
0 | 486 |
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
|
487 |
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
|
488 |
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
|
489 |
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
|
490 |
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
|
491 |
syncperms=syncperms) |
1477 | 492 |
|
5666
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
493 |
def _synchronize_eschema(self, etype, syncrdefs=True, |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
494 |
syncperms=True, syncprops=True): |
0 | 495 |
"""synchronize properties of the persistent entity schema against |
496 |
its current definition: |
|
1477 | 497 |
|
0 | 498 |
* description |
499 |
* internationalizable, fulltextindexed, indexed, meta |
|
500 |
* relations from/to this entity |
|
6208
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
501 |
* __unique_together__ |
0 | 502 |
* permissions if `syncperms` |
503 |
""" |
|
504 |
etype = str(etype) |
|
505 |
if etype in self._synchronized: |
|
506 |
return |
|
507 |
self._synchronized.add(etype) |
|
508 |
repoeschema = self.repo.schema.eschema(etype) |
|
509 |
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
|
510 |
eschema = self.fs_schema.eschema(etype) |
0 | 511 |
except KeyError: |
5666
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
512 |
return # XXX somewhat unexpected, no?... |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
513 |
if syncprops: |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
514 |
repospschema = repoeschema.specializes() |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
515 |
espschema = eschema.specializes() |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
516 |
if repospschema and not espschema: |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
517 |
self.rqlexec('DELETE X specializes Y WHERE X is CWEType, X name %(x)s', |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
518 |
{'x': str(repoeschema)}, ask_confirm=False) |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
519 |
elif not repospschema and espschema: |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
520 |
self.rqlexec('SET X specializes Y WHERE X is CWEType, X name %(x)s, ' |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
521 |
'Y is CWEType, Y name %(y)s', |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
522 |
{'x': str(repoeschema), 'y': str(espschema)}, |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
523 |
ask_confirm=False) |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
524 |
self.rqlexecall(ss.updateeschema2rql(eschema, repoeschema.eid), |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
525 |
ask_confirm=self.verbosity >= 2) |
0 | 526 |
if syncperms: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
527 |
self._synchronize_permissions(eschema, repoeschema.eid) |
5666
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
528 |
if syncrdefs: |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
529 |
for rschema, targettypes, role in eschema.relation_definitions(True): |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
530 |
if rschema in VIRTUAL_RTYPES: |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
531 |
continue |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
532 |
if role == 'subject': |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
533 |
if not rschema in repoeschema.subject_relations(): |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
534 |
continue |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
535 |
subjtypes, objtypes = [etype], targettypes |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
536 |
else: # role == 'object' |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
537 |
if not rschema in repoeschema.object_relations(): |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
538 |
continue |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
539 |
subjtypes, objtypes = targettypes, [etype] |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
540 |
self._synchronize_rschema(rschema, syncrdefs=False, |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
541 |
syncprops=syncprops, syncperms=syncperms) |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
542 |
reporschema = self.repo.schema.rschema(rschema) |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
543 |
for subj in subjtypes: |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
544 |
for obj in objtypes: |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
545 |
if (subj, obj) not in reporschema.rdefs: |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
546 |
continue |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
547 |
self._synchronize_rdef_schema(subj, rschema, obj, |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
548 |
syncprops=syncprops, syncperms=syncperms) |
6208
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
549 |
if syncprops: # need to process __unique_together__ after rdefs were processed |
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
550 |
repo_unique_together = set([frozenset(ut) |
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
551 |
for ut in repoeschema._unique_together]) |
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
552 |
unique_together = set([frozenset(ut) |
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
553 |
for ut in eschema._unique_together]) |
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
554 |
for ut in repo_unique_together - unique_together: |
6815
a84190d4e78c
[schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6805
diff
changeset
|
555 |
restrictions = [] |
a84190d4e78c
[schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6805
diff
changeset
|
556 |
substs = {'x': repoeschema.eid} |
6208
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
557 |
for i, col in enumerate(ut): |
6815
a84190d4e78c
[schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6805
diff
changeset
|
558 |
restrictions.append('C relations T%(i)d, ' |
a84190d4e78c
[schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6805
diff
changeset
|
559 |
'T%(i)d name %%(T%(i)d)s' % {'i': i}) |
6208
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
560 |
substs['T%d'%i] = col |
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
561 |
self.rqlexec('DELETE CWUniqueTogetherConstraint C ' |
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
562 |
'WHERE C constraint_of E, ' |
6815
a84190d4e78c
[schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6805
diff
changeset
|
563 |
' E eid %%(x)s,' |
7008 | 564 |
' %s' % ', '.join(restrictions), |
6208
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
565 |
substs) |
07b176640a8c
unique_together integration in CW (alf+syt)
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6205
diff
changeset
|
566 |
for ut in unique_together - repo_unique_together: |
6815
a84190d4e78c
[schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6805
diff
changeset
|
567 |
rql, substs = ss.uniquetogether2rql(eschema, ut) |
a84190d4e78c
[schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6805
diff
changeset
|
568 |
substs['x'] = repoeschema.eid |
a84190d4e78c
[schema] fix pb with ambiguous relation used within UniqueTogetherConstraint by having CWUniqueTogetherConstraint.relation targeting CWRType instead of CWAttribute/CWRelation. This fixes 3.10.7 migration.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6805
diff
changeset
|
569 |
self.rqlexec(rql, substs) |
0 | 570 |
|
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
|
571 |
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
|
572 |
syncperms=True, syncprops=True): |
0 | 573 |
"""synchronize properties of the persistent relation definition schema |
574 |
against its current definition: |
|
575 |
* order and other properties |
|
576 |
* constraints |
|
577 |
""" |
|
578 |
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
|
579 |
rschema = self.fs_schema.rschema(rtype) |
0 | 580 |
reporschema = self.repo.schema.rschema(rschema) |
581 |
if (subjtype, rschema, objtype) in self._synchronized: |
|
582 |
return |
|
583 |
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
|
584 |
if rschema.symmetric: |
0 | 585 |
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
|
586 |
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
|
587 |
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
|
588 |
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
|
589 |
repordef = reporschema.rdef(subjtype, objtype) |
0 | 590 |
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
|
591 |
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
|
592 |
# 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
|
593 |
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
|
594 |
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
|
595 |
# 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
|
596 |
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
|
597 |
# 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
|
598 |
# 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
|
599 |
# 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
|
600 |
# 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
|
601 |
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
|
602 |
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
|
603 |
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
|
604 |
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
|
605 |
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
|
606 |
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
|
607 |
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
|
608 |
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
|
609 |
{'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
|
610 |
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
|
611 |
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
|
612 |
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
|
613 |
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
|
614 |
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
|
615 |
{'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
|
616 |
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
|
617 |
# 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
|
618 |
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
|
619 |
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
|
620 |
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
|
621 |
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
|
622 |
if syncperms and not rschema in VIRTUAL_RTYPES: |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
623 |
self._synchronize_permissions(rdef, repordef.eid) |
1477 | 624 |
|
0 | 625 |
# base actions ############################################################ |
626 |
||
4011
394f853bb653
[migration] write migration instructions for permissions handling on relation definition
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3998
diff
changeset
|
627 |
def checkpoint(self, ask_confirm=True): |
0 | 628 |
"""checkpoint action""" |
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
629 |
if not ask_confirm or self.confirm('Commit now ?', shell=False): |
0 | 630 |
self.commit() |
631 |
||
632 |
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
|
633 |
self.cmd_add_cubes( (cube,), update_database) |
1477 | 634 |
|
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
|
635 |
def cmd_add_cubes(self, cubes, update_database=True): |
0 | 636 |
"""update_database is telling if the database schema should be updated |
637 |
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
|
638 |
a cube has been extracted from an existing instance, so the |
0 | 639 |
cube schema is already in there) |
640 |
""" |
|
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
|
641 |
newcubes = super(ServerMigrationHelper, self).cmd_add_cubes(cubes) |
0 | 642 |
if not newcubes: |
643 |
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
|
644 |
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
|
645 |
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
|
646 |
self.config.cube_version(cube)) |
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
|
647 |
# 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
|
648 |
# 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
|
649 |
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
|
650 |
self.config._cubes += (cube,) |
0 | 651 |
if not update_database: |
652 |
self.commit() |
|
653 |
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
|
654 |
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
|
655 |
# 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
|
656 |
# etc. and fsschema of migration script contexts |
6217
e2aeb40d5983
[migration] fix so that context modification are reflected on the currently executed migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6208
diff
changeset
|
657 |
self.fs_schema = newcubes_schema |
e2aeb40d5983
[migration] fix so that context modification are reflected on the currently executed migration script
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6208
diff
changeset
|
658 |
self.update_context('fsschema', self.fs_schema) |
0 | 659 |
new = set() |
660 |
# 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
|
661 |
driver = self.repo.system_source.dbdriver |
0 | 662 |
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
|
663 |
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
|
664 |
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
|
665 |
self.exec_event_script('precreate', cubedir) |
0 | 666 |
# 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
|
667 |
for rschema in newcubes_schema.relations(): |
0 | 668 |
if not rschema in self.repo.schema: |
669 |
self.cmd_add_relation_type(rschema.type) |
|
670 |
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
|
671 |
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
|
672 |
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
|
673 |
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
|
674 |
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
|
675 |
new.add(eschema.type) |
0 | 676 |
# 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
|
677 |
for rschema in newcubes_schema.relations(): |
0 | 678 |
existingschema = self.repo.schema.rschema(rschema.type) |
4099
59ff385f7348
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4043
diff
changeset
|
679 |
for (fromtype, totype) in rschema.rdefs: |
59ff385f7348
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4043
diff
changeset
|
680 |
if (fromtype, totype) in existingschema.rdefs: |
0 | 681 |
continue |
682 |
# check we should actually add the relation definition |
|
683 |
if not (fromtype in new or totype in new or rschema in new): |
|
684 |
continue |
|
1477 | 685 |
self.cmd_add_relation_definition(str(fromtype), rschema.type, |
0 | 686 |
str(totype)) |
687 |
# execute post-create files |
|
688 |
for pack in reversed(newcubes): |
|
689 |
self.exec_event_script('postcreate', self.config.cube_dir(pack)) |
|
1477 | 690 |
self.commit() |
691 |
||
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
|
692 |
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
|
693 |
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
|
694 |
cube, removedeps) |
0 | 695 |
if not removedcubes: |
696 |
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
|
697 |
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
|
698 |
removedcubes_schema = self.config.load_schema(construction_mode='non-strict') |
0 | 699 |
reposchema = self.repo.schema |
700 |
# execute pre-remove files |
|
701 |
for pack in reversed(removedcubes): |
|
702 |
self.exec_event_script('preremove', self.config.cube_dir(pack)) |
|
703 |
# 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
|
704 |
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
|
705 |
if not rschema in removedcubes_schema and rschema in reposchema: |
0 | 706 |
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
|
707 |
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
|
708 |
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
|
709 |
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
|
710 |
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
|
711 |
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
|
712 |
for rschema in fsschema.relations(): |
1477 | 713 |
if rschema in removedcubes_schema and rschema in reposchema: |
714 |
# check if attributes/relations has been added to entities from |
|
0 | 715 |
# other cubes |
4099
59ff385f7348
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4043
diff
changeset
|
716 |
for fromtype, totype in rschema.rdefs: |
59ff385f7348
yams api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4043
diff
changeset
|
717 |
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
|
718 |
(fromtype, totype) in reposchema[rschema.type].rdefs: |
0 | 719 |
self.cmd_drop_relation_definition( |
720 |
str(fromtype), rschema.type, str(totype)) |
|
721 |
# 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
|
722 |
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
|
723 |
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
|
724 |
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
|
725 |
{'pk': u'system.version.'+cube}, ask_confirm=False) |
0 | 726 |
self.commit() |
1477 | 727 |
|
0 | 728 |
# schema migration actions ################################################ |
1477 | 729 |
|
0 | 730 |
def cmd_add_attribute(self, etype, attrname, attrtype=None, commit=True): |
731 |
"""add a new attribute on the given entity type""" |
|
732 |
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
|
733 |
rschema = self.fs_schema.rschema(attrname) |
0 | 734 |
attrtype = rschema.objects(etype)[0] |
735 |
self.cmd_add_relation_definition(etype, attrname, attrtype, commit=commit) |
|
1477 | 736 |
|
0 | 737 |
def cmd_drop_attribute(self, etype, attrname, commit=True): |
738 |
"""drop an existing attribute from the given entity type |
|
1477 | 739 |
|
0 | 740 |
`attrname` is a string giving the name of the attribute to drop |
741 |
""" |
|
7556
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
742 |
try: |
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
743 |
rschema = self.repo.schema.rschema(attrname) |
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
744 |
attrtype = rschema.objects(etype)[0] |
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
745 |
except KeyError: |
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
746 |
print 'warning: attribute %s %s is not known, skip deletion' % ( |
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
747 |
etype, attrname) |
7558
044135305359
[migration] litle enhancements and a fix to patch accidentally pushed two revisions ago
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7556
diff
changeset
|
748 |
else: |
044135305359
[migration] litle enhancements and a fix to patch accidentally pushed two revisions ago
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7556
diff
changeset
|
749 |
self.cmd_drop_relation_definition(etype, attrname, attrtype, |
044135305359
[migration] litle enhancements and a fix to patch accidentally pushed two revisions ago
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7556
diff
changeset
|
750 |
commit=commit) |
0 | 751 |
|
752 |
def cmd_rename_attribute(self, etype, oldname, newname, commit=True): |
|
753 |
"""rename an existing attribute of the given entity type |
|
1477 | 754 |
|
0 | 755 |
`oldname` is a string giving the name of the existing attribute |
756 |
`newname` is a string giving the name of the renamed attribute |
|
757 |
""" |
|
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
|
758 |
eschema = self.fs_schema.eschema(etype) |
0 | 759 |
attrtype = eschema.destination(newname) |
760 |
# have to commit this first step anyway to get the definition |
|
761 |
# actually in the schema |
|
762 |
self.cmd_add_attribute(etype, newname, attrtype, commit=True) |
|
763 |
# skipp NULL values if the attribute is required |
|
764 |
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
|
765 |
card = eschema.rdef(newname).cardinality[0] |
0 | 766 |
if card == '1': |
767 |
rql += ', NOT X %s NULL' % oldname |
|
768 |
self.rqlexec(rql, ask_confirm=self.verbosity>=2) |
|
3548
4cf5a360952e
add some notes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3213
diff
changeset
|
769 |
# 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
|
770 |
# 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
|
771 |
# 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
|
772 |
# fulltextindexed change...) |
0 | 773 |
self.cmd_drop_attribute(etype, oldname, commit=commit) |
1477 | 774 |
|
0 | 775 |
def cmd_add_entity_type(self, etype, auto=True, commit=True): |
776 |
"""register a new entity type |
|
1477 | 777 |
|
0 | 778 |
in auto mode, automatically register entity's relation where the |
779 |
targeted type is known |
|
780 |
""" |
|
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
|
781 |
instschema = self.repo.schema |
7166
dde161937d3e
[time zone] support for TZDatetime and TZTime data type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7161
diff
changeset
|
782 |
eschema = self.fs_schema.eschema(etype) |
7558
044135305359
[migration] litle enhancements and a fix to patch accidentally pushed two revisions ago
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7556
diff
changeset
|
783 |
if etype in instschema and not (eschema.final and eschema.eid is None): |
7556
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
784 |
print 'warning: %s already known, skip addition' % etype |
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
785 |
return |
0 | 786 |
confirm = self.verbosity >= 2 |
4043
39ae94e0c8b8
give group mapping where needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4042
diff
changeset
|
787 |
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
|
788 |
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
|
789 |
# 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
|
790 |
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
|
791 |
ss.execschemarql(execute, eschema, ss.eschema2rql(eschema, groupmap)) |
0 | 792 |
# 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
|
793 |
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
|
794 |
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
|
795 |
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
|
796 |
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
|
797 |
except KeyError: |
6323
a11c1e3c16c3
[c-c shell/upgrade] raise ExecutionError when traceback is not relevant
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6217
diff
changeset
|
798 |
raise ExecutionError('trying to add entity type but parent type is ' |
a11c1e3c16c3
[c-c shell/upgrade] raise ExecutionError when traceback is not relevant
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
6217
diff
changeset
|
799 |
'not yet in the database schema') |
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
|
800 |
self.rqlexecall(ss.eschemaspecialize2rql(eschema), ask_confirm=confirm) |
0 | 801 |
# register entity's attributes |
802 |
for rschema, attrschema in eschema.attribute_definitions(): |
|
803 |
# 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
|
804 |
if rschema.type in META_RTYPES: |
0 | 805 |
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
|
806 |
if not rschema.type in instschema: |
0 | 807 |
# need to add the relation type and to commit to get it |
808 |
# actually in the schema |
|
809 |
self.cmd_add_relation_type(rschema.type, False, commit=True) |
|
810 |
# 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
|
811 |
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
|
812 |
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
|
813 |
# 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
|
814 |
# 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
|
815 |
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
|
816 |
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
|
817 |
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
|
818 |
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
|
819 |
# 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
|
820 |
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
|
821 |
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
|
822 |
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
|
823 |
{'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
|
824 |
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
|
825 |
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
|
826 |
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
|
827 |
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
|
828 |
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
|
829 |
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
|
830 |
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
|
831 |
objschema = tschema |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3582
diff
changeset
|
832 |
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
|
833 |
# 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
|
834 |
# 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
|
835 |
# 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
|
836 |
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
|
837 |
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
|
838 |
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
|
839 |
objschema = spschema |
4633
a85b4361fb22
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4580
diff
changeset
|
840 |
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
|
841 |
or (instschema.has_relation(rschema) and |
4633
a85b4361fb22
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4580
diff
changeset
|
842 |
(subjschema, objschema) in instschema[rschema].rdefs)): |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
843 |
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
|
844 |
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
|
845 |
subjschema.type, rschema.type, objschema.type) |
0 | 846 |
if auto: |
847 |
# we have commit here to get relation types actually in the schema |
|
848 |
self.commit() |
|
849 |
added = [] |
|
850 |
for rschema in eschema.subject_relations(): |
|
851 |
# attribute relation have already been processed and |
|
852 |
# '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
|
853 |
if rschema.final or rschema.type in META_RTYPES: |
0 | 854 |
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
|
855 |
rtypeadded = rschema.type in instschema |
0 | 856 |
for targetschema in rschema.objects(etype): |
857 |
# 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
|
858 |
# current instance schema |
0 | 859 |
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
|
860 |
if not targettype in instschema and targettype != etype: |
0 | 861 |
continue |
862 |
if not rtypeadded: |
|
863 |
# need to add the relation type and to commit to get it |
|
864 |
# actually in the schema |
|
865 |
added.append(rschema.type) |
|
866 |
self.cmd_add_relation_type(rschema.type, False, commit=True) |
|
867 |
rtypeadded = True |
|
868 |
# 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
|
869 |
# remember this two avoid adding twice non symmetric relation |
0 | 870 |
# such as "Emailthread forked_from Emailthread" |
871 |
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
|
872 |
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
|
873 |
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
|
874 |
ss.rdef2rql(rdef, cstrtypemap, groupmap)) |
0 | 875 |
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
|
876 |
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
|
877 |
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
|
878 |
rtypeadded = rschema.type in instschema or rschema.type in added |
0 | 879 |
for targetschema in rschema.subjects(etype): |
880 |
# 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
|
881 |
# current instance schema |
0 | 882 |
targettype = targetschema.type |
883 |
# don't check targettype != etype since in this case the |
|
884 |
# 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
|
885 |
if not targettype in instschema: |
0 | 886 |
continue |
887 |
if not rtypeadded: |
|
888 |
# need to add the relation type and to commit to get it |
|
889 |
# actually in the schema |
|
890 |
self.cmd_add_relation_type(rschema.type, False, commit=True) |
|
891 |
rtypeadded = True |
|
892 |
elif (targettype, rschema.type, etype) in added: |
|
893 |
continue |
|
894 |
# 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
|
895 |
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
|
896 |
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
|
897 |
ss.rdef2rql(rdef, cstrtypemap, groupmap)) |
0 | 898 |
if commit: |
899 |
self.commit() |
|
1477 | 900 |
|
0 | 901 |
def cmd_drop_entity_type(self, etype, commit=True): |
902 |
"""unregister an existing entity type |
|
1477 | 903 |
|
0 | 904 |
This will trigger deletion of necessary relation types and definitions |
905 |
""" |
|
906 |
# 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
|
907 |
# 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
|
908 |
self.rqlexec('DELETE CWEType X WHERE X name %(etype)s', {'etype': etype}, |
0 | 909 |
ask_confirm=self.verbosity>=2) |
910 |
if commit: |
|
911 |
self.commit() |
|
912 |
||
6115
56ec278b843e
[migration] we can now specify attributes to backport to rename_entity_type using its attrs argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6075
diff
changeset
|
913 |
def cmd_rename_entity_type(self, oldname, newname, attrs=None, commit=True): |
0 | 914 |
"""rename an existing entity type in the persistent schema |
1477 | 915 |
|
0 | 916 |
`oldname` is a string giving the name of the existing entity type |
917 |
`newname` is a string giving the name of the renamed entity type |
|
918 |
""" |
|
5558
afd1face1faf
[schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
919 |
schema = self.repo.schema |
afd1face1faf
[schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
920 |
if newname in schema: |
afd1face1faf
[schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
921 |
assert oldname in ETYPE_NAME_MAP, \ |
6115
56ec278b843e
[migration] we can now specify attributes to backport to rename_entity_type using its attrs argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6075
diff
changeset
|
922 |
'%s should be mapped to %s in ETYPE_NAME_MAP' % (oldname, |
56ec278b843e
[migration] we can now specify attributes to backport to rename_entity_type using its attrs argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6075
diff
changeset
|
923 |
newname) |
56ec278b843e
[migration] we can now specify attributes to backport to rename_entity_type using its attrs argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6075
diff
changeset
|
924 |
if attrs is None: |
56ec278b843e
[migration] we can now specify attributes to backport to rename_entity_type using its attrs argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6075
diff
changeset
|
925 |
attrs = ','.join(SQL_PREFIX + rschema.type |
56ec278b843e
[migration] we can now specify attributes to backport to rename_entity_type using its attrs argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6075
diff
changeset
|
926 |
for rschema in schema[newname].subject_relations() |
56ec278b843e
[migration] we can now specify attributes to backport to rename_entity_type using its attrs argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6075
diff
changeset
|
927 |
if (rschema.final or rschema.inlined) |
56ec278b843e
[migration] we can now specify attributes to backport to rename_entity_type using its attrs argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6075
diff
changeset
|
928 |
and not rschema in PURE_VIRTUAL_RTYPES) |
56ec278b843e
[migration] we can now specify attributes to backport to rename_entity_type using its attrs argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6075
diff
changeset
|
929 |
else: |
56ec278b843e
[migration] we can now specify attributes to backport to rename_entity_type using its attrs argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6075
diff
changeset
|
930 |
attrs += ('eid', 'creation_date', 'modification_date', 'cwuri') |
56ec278b843e
[migration] we can now specify attributes to backport to rename_entity_type using its attrs argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6075
diff
changeset
|
931 |
attrs = ','.join(SQL_PREFIX + attr for attr in attrs) |
5558
afd1face1faf
[schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
932 |
self.sqlexec('INSERT INTO %s%s(%s) SELECT %s FROM %s%s' % ( |
5999
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
933 |
SQL_PREFIX, newname, attrs, attrs, SQL_PREFIX, oldname), |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
934 |
ask_confirm=False) |
5897
0c6f2f866202
[migration] fix rename_entity_type to an existant entity type: get back relation from the old entity type non existant on the target type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5863
diff
changeset
|
935 |
# old entity type has not been added to the schema, can't gather it |
0c6f2f866202
[migration] fix rename_entity_type to an existant entity type: get back relation from the old entity type non existant on the target type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5863
diff
changeset
|
936 |
new = schema.eschema(newname) |
6117 | 937 |
oldeid = self.rqlexec('CWEType ET WHERE ET name %(on)s', |
938 |
{'on': oldname}, ask_confirm=False)[0][0] |
|
5897
0c6f2f866202
[migration] fix rename_entity_type to an existant entity type: get back relation from the old entity type non existant on the target type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5863
diff
changeset
|
939 |
# backport old type relations to new type |
0c6f2f866202
[migration] fix rename_entity_type to an existant entity type: get back relation from the old entity type non existant on the target type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5863
diff
changeset
|
940 |
# XXX workflows, other relations? |
5999
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
941 |
for r1, rr1 in [('from_entity', 'to_entity'), |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
942 |
('to_entity', 'from_entity')]: |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
943 |
self.rqlexec('SET X %(r1)s NET WHERE X %(r1)s OET, ' |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
944 |
'NOT EXISTS(X2 %(r1)s NET, X relation_type XRT, ' |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
945 |
'X2 relation_type XRT, X %(rr1)s XTE, X2 %(rr1)s XTE), ' |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
946 |
'OET eid %%(o)s, NET eid %%(n)s' % locals(), |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
947 |
{'o': oldeid, 'n': new.eid}, ask_confirm=False) |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
948 |
# backport is / is_instance_of relation to new type |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
949 |
for rtype in ('is', 'is_instance_of'): |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
950 |
self.sqlexec('UPDATE %s_relation SET eid_to=%s WHERE eid_to=%s' |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
951 |
% (rtype, new.eid, oldeid), ask_confirm=False) |
eaf8219f8b7d
[migration] fix rename_entity_type to avoid to loose some relations on the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5928
diff
changeset
|
952 |
# delete relations using SQL to avoid relations content removal |
6190
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
953 |
# triggered by schema synchronization hooks. |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
954 |
session = self.session |
6116
2530e9c45296
[migration] fix rename_entity_type: avoid removal of attributes on the new entity type from the schema during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6115
diff
changeset
|
955 |
for rdeftype in ('CWRelation', 'CWAttribute'): |
7501
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
956 |
thispending = set( (eid for eid, in self.sqlexec( |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
957 |
'SELECT cw_eid FROM cw_%s WHERE cw_from_entity=%%(eid)s OR ' |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
958 |
' cw_to_entity=%%(eid)s' % rdeftype, |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
959 |
{'eid': oldeid}, ask_confirm=False)) ) |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
960 |
# we should add deleted eids into pending eids else we may |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
961 |
# get some validation error on commit since integrity hooks |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
962 |
# may think some required relation is missing... This also ensure |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
963 |
# repository caches are properly cleanup |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
964 |
hook.CleanupDeletedEidsCacheOp.get_instance(session).union(thispending) |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
965 |
# and don't forget to remove record from system tables |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
966 |
entities = [session.entity_from_eid(eid, rdeftype) for eid in thispending] |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
967 |
self.repo.system_source.delete_info_multi(session, entities, 'system') |
2983dd24494a
[repository] refactor/cleanup entity deletion methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7410
diff
changeset
|
968 |
self.sqlexec('DELETE FROM cw_%s WHERE cw_from_entity=%%(eid)s OR ' |
6116
2530e9c45296
[migration] fix rename_entity_type: avoid removal of attributes on the new entity type from the schema during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6115
diff
changeset
|
969 |
'cw_to_entity=%%(eid)s' % rdeftype, |
2530e9c45296
[migration] fix rename_entity_type: avoid removal of attributes on the new entity type from the schema during migration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6115
diff
changeset
|
970 |
{'eid': oldeid}, ask_confirm=False) |
6190
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
971 |
# now we have to manually cleanup relations pointing to deleted |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
972 |
# entities |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
973 |
thiseids = ','.join(str(eid) for eid in thispending) |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
974 |
for rschema, ttypes, role in schema[rdeftype].relation_definitions(): |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
975 |
if rschema.type in VIRTUAL_RTYPES: |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
976 |
continue |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
977 |
sqls = [] |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
978 |
if role == 'object': |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
979 |
if rschema.inlined: |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
980 |
for eschema in ttypes: |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
981 |
sqls.append('DELETE FROM cw_%s WHERE cw_%s IN(%%s)' |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
982 |
% (eschema, rschema)) |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
983 |
else: |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
984 |
sqls.append('DELETE FROM %s_relation WHERE eid_to IN(%%s)' |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
985 |
% rschema) |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
986 |
elif not rschema.inlined: |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
987 |
sqls.append('DELETE FROM %s_relation WHERE eid_from IN(%%s)' |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
988 |
% rschema) |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
989 |
for sql in sqls: |
564a6028067c
[migration] more rename_entity_type fix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6189
diff
changeset
|
990 |
self.sqlexec(sql % thiseids, ask_confirm=False) |
5897
0c6f2f866202
[migration] fix rename_entity_type to an existant entity type: get back relation from the old entity type non existant on the target type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5863
diff
changeset
|
991 |
# remove the old type: use rql to propagate deletion |
0c6f2f866202
[migration] fix rename_entity_type to an existant entity type: get back relation from the old entity type non existant on the target type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5863
diff
changeset
|
992 |
self.rqlexec('DELETE CWEType ET WHERE ET name %(on)s', {'on': oldname}, |
0c6f2f866202
[migration] fix rename_entity_type to an existant entity type: get back relation from the old entity type non existant on the target type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5863
diff
changeset
|
993 |
ask_confirm=False) |
5558
afd1face1faf
[schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
994 |
else: |
5897
0c6f2f866202
[migration] fix rename_entity_type to an existant entity type: get back relation from the old entity type non existant on the target type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5863
diff
changeset
|
995 |
self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(on)s', |
0c6f2f866202
[migration] fix rename_entity_type to an existant entity type: get back relation from the old entity type non existant on the target type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5863
diff
changeset
|
996 |
{'newname' : unicode(newname), 'on' : oldname}, |
5558
afd1face1faf
[schema migration] make some stuff to ease file 1.9 migration : we want to kill the Image entity so that existing image are turned into (existing entity type) File entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
997 |
ask_confirm=False) |
0 | 998 |
if commit: |
999 |
self.commit() |
|
1477 | 1000 |
|
0 | 1001 |
def cmd_add_relation_type(self, rtype, addrdef=True, commit=True): |
1002 |
"""register a new relation type named `rtype`, as described in the |
|
1003 |
schema description file. |
|
1004 |
||
1005 |
`addrdef` is a boolean value; when True, it will also add all relations |
|
1006 |
of the type just added found in the schema definition file. Note that it |
|
1007 |
implies an intermediate "commit" which commits the relation type |
|
1008 |
creation (but not the relation definitions themselves, for which |
|
1009 |
committing depends on the `commit` argument value). |
|
1477 | 1010 |
|
0 | 1011 |
""" |
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
|
1012 |
reposchema = self.repo.schema |
7558
044135305359
[migration] litle enhancements and a fix to patch accidentally pushed two revisions ago
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7556
diff
changeset
|
1013 |
if rtype in reposchema: |
7556
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
1014 |
print 'warning: relation type %s is already known, skip addition' % ( |
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
1015 |
rtype) |
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
1016 |
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
|
1017 |
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
|
1018 |
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
|
1019 |
# register the relation into CWRType and insert necessary relation |
0 | 1020 |
# 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
|
1021 |
ss.execschemarql(execute, rschema, ss.rschema2rql(rschema, addrdef=False)) |
0 | 1022 |
if addrdef: |
1023 |
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
|
1024 |
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
|
1025 |
cmap = self.cstrtype_mapping() |
6073
bf684f1022bf
[migration] fix crash when adding symmetric relation type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
1026 |
done = set() |
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
|
1027 |
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
|
1028 |
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
|
1029 |
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
|
1030 |
continue |
6073
bf684f1022bf
[migration] fix crash when adding symmetric relation type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
1031 |
# symmetric relations appears twice |
bf684f1022bf
[migration] fix crash when adding symmetric relation type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
1032 |
if (rdef.subject, rdef.object) in done: |
bf684f1022bf
[migration] fix crash when adding symmetric relation type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
1033 |
continue |
bf684f1022bf
[migration] fix crash when adding symmetric relation type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6042
diff
changeset
|
1034 |
done.add( (rdef.subject, rdef.object) ) |
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
|
1035 |
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
|
1036 |
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
|
1037 |
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
|
1038 |
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
|
1039 |
# 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
|
1040 |
# 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
|
1041 |
# 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
|
1042 |
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
|
1043 |
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
|
1044 |
# 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
|
1045 |
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
|
1046 |
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
|
1047 |
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
|
1048 |
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
|
1049 |
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
|
1050 |
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
|
1051 |
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
|
1052 |
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
|
1053 |
ss.rdef2rql(rdef, cmap, gmap)) |
0 | 1054 |
if commit: |
1055 |
self.commit() |
|
1477 | 1056 |
|
0 | 1057 |
def cmd_drop_relation_type(self, rtype, commit=True): |
1058 |
"""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
|
1059 |
# 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
|
1060 |
self.rqlexec('DELETE CWRType X WHERE X name %r' % rtype, |
0 | 1061 |
ask_confirm=self.verbosity>=2) |
1062 |
if commit: |
|
1063 |
self.commit() |
|
1477 | 1064 |
|
0 | 1065 |
def cmd_rename_relation(self, oldname, newname, commit=True): |
1066 |
"""rename an existing relation |
|
1477 | 1067 |
|
0 | 1068 |
`oldname` is a string giving the name of the existing relation |
1069 |
`newname` is a string giving the name of the renamed relation |
|
1070 |
""" |
|
1071 |
self.cmd_add_relation_type(newname, commit=True) |
|
1072 |
self.rqlexec('SET X %s Y WHERE X %s Y' % (newname, oldname), |
|
1073 |
ask_confirm=self.verbosity>=2) |
|
1074 |
self.cmd_drop_relation_type(oldname, commit=commit) |
|
1075 |
||
1076 |
def cmd_add_relation_definition(self, subjtype, rtype, objtype, commit=True): |
|
1077 |
"""register a new relation definition, from its definition found in the |
|
1078 |
schema definition file |
|
1079 |
""" |
|
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
|
1080 |
rschema = self.fs_schema.rschema(rtype) |
0 | 1081 |
if not rtype in self.repo.schema: |
1082 |
self.cmd_add_relation_type(rtype, addrdef=False, commit=True) |
|
7556
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
1083 |
if (subjtype, objtype) in self.repo.schema.rschema(rtype).rdefs: |
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
1084 |
print 'warning: relation %s %s %s is already known, skip addition' % ( |
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
1085 |
subjtype, rtype, objtype) |
867ec36530b8
[migration] closes #1777411: schema modification commands shouldn't crash if already exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7505
diff
changeset
|
1086 |
return |
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
|
1087 |
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
|
1088 |
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
|
1089 |
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
|
1090 |
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
|
1091 |
self.group_mapping())) |
0 | 1092 |
if commit: |
1093 |
self.commit() |
|
1477 | 1094 |
|
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
|
1095 |
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
|
1096 |
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
|
1097 |
|
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
|
1098 |
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
|
1099 |
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
|
1100 |
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
|
1101 |
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
|
1102 |
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
|
1103 |
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
|
1104 |
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
|
1105 |
|
0 | 1106 |
def cmd_drop_relation_definition(self, subjtype, rtype, objtype, commit=True): |
1107 |
"""unregister an existing relation definition""" |
|
1108 |
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
|
1109 |
# 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
|
1110 |
if rschema.final: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1111 |
etype = 'CWAttribute' |
0 | 1112 |
else: |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1113 |
etype = 'CWRelation' |
0 | 1114 |
rql = ('DELETE %s X WHERE X from_entity FE, FE name "%s",' |
1115 |
'X relation_type RT, RT name "%s", X to_entity TE, TE name "%s"') |
|
1116 |
self.rqlexec(rql % (etype, subjtype, rtype, objtype), |
|
1117 |
ask_confirm=self.verbosity>=2) |
|
1118 |
if commit: |
|
1119 |
self.commit() |
|
1477 | 1120 |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1121 |
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
|
1122 |
syncprops=True, syncrdefs=True, commit=True): |
0 | 1123 |
"""synchronize the persistent schema against the current definition |
1124 |
schema. |
|
1477 | 1125 |
|
6292
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1126 |
`ertype` can be : |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1127 |
- None, in that case everything will be synced ; |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1128 |
- a string, it should be an entity type or |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1129 |
a relation type. In that case, only the corresponding |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1130 |
entities / relations will be synced ; |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1131 |
- an rdef object to synchronize only this specific relation definition |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1132 |
|
0 | 1133 |
It will synch common stuff between the definition schema and the |
1134 |
actual persistent schema, it won't add/remove any entity or relation. |
|
1135 |
""" |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1136 |
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
|
1137 |
if ertype is not None: |
6292
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1138 |
if isinstance(ertype, RelationDefinitionSchema): |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1139 |
ertype = ertype.as_triple() |
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1140 |
if isinstance(ertype, (tuple, list)): |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1141 |
assert len(ertype) == 3, 'not a relation definition' |
3877
7ca53fc72a0a
reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3836
diff
changeset
|
1142 |
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
|
1143 |
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
|
1144 |
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
|
1145 |
else: |
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1146 |
erschema = self.repo.schema[ertype] |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1147 |
if isinstance(erschema, CubicWebRelationSchema): |
5666
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
1148 |
self._synchronize_rschema(erschema, syncrdefs=syncrdefs, |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
1149 |
syncperms=syncperms, |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
1150 |
syncprops=syncprops) |
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
|
1151 |
else: |
5666
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
1152 |
self._synchronize_eschema(erschema, syncrdefs=syncrdefs, |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
1153 |
syncperms=syncperms, |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
1154 |
syncprops=syncprops) |
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1155 |
else: |
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1156 |
for etype in self.repo.schema.entities(): |
7505
e04790a6eea8
[migration] this is needed to avoid pb with yams 0.33 / cw < 3.13
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
1157 |
if etype.eid is None: |
e04790a6eea8
[migration] this is needed to avoid pb with yams 0.33 / cw < 3.13
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
1158 |
# not yet added final etype (thing to BigInt defined in |
e04790a6eea8
[migration] this is needed to avoid pb with yams 0.33 / cw < 3.13
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
1159 |
# yams though 3.13 migration not done yet) |
e04790a6eea8
[migration] this is needed to avoid pb with yams 0.33 / cw < 3.13
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7501
diff
changeset
|
1160 |
continue |
5666
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
1161 |
self._synchronize_eschema(etype, syncrdefs=syncrdefs, |
58d6c2c96500
[migration] fix schema synchronization bug
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
1162 |
syncprops=syncprops, syncperms=syncperms) |
0 | 1163 |
if commit: |
1164 |
self.commit() |
|
1477 | 1165 |
|
0 | 1166 |
def cmd_change_relation_props(self, subjtype, rtype, objtype, |
1167 |
commit=True, **kwargs): |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1168 |
"""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
|
1169 |
|
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1170 |
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
|
1171 |
""" |
0 | 1172 |
assert kwargs |
1173 |
restriction = [] |
|
1174 |
if subjtype and subjtype != 'Any': |
|
1175 |
restriction.append('X from_entity FE, FE name "%s"' % subjtype) |
|
1176 |
if objtype and objtype != 'Any': |
|
1177 |
restriction.append('X to_entity TE, TE name "%s"' % objtype) |
|
1178 |
if rtype and rtype != 'Any': |
|
1179 |
restriction.append('X relation_type RT, RT name "%s"' % rtype) |
|
1180 |
assert restriction |
|
1181 |
values = [] |
|
1182 |
for k, v in kwargs.items(): |
|
1183 |
values.append('X %s %%(%s)s' % (k, k)) |
|
1184 |
if isinstance(v, str): |
|
1185 |
kwargs[k] = unicode(v) |
|
1186 |
rql = 'SET %s WHERE %s' % (','.join(values), ','.join(restriction)) |
|
1187 |
self.rqlexec(rql, kwargs, ask_confirm=self.verbosity>=2) |
|
1188 |
if commit: |
|
1189 |
self.commit() |
|
1190 |
||
1191 |
def cmd_set_size_constraint(self, etype, rtype, size, commit=True): |
|
1192 |
"""set change size constraint of a string attribute |
|
1193 |
||
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1194 |
if size is None any size constraint will be removed. |
1477 | 1195 |
|
1196 |
you usually want to use sync_schema_props_perms instead. |
|
0 | 1197 |
""" |
1198 |
oldvalue = None |
|
4695
4aaf87e7f79e
3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4650
diff
changeset
|
1199 |
for constr in self.repo.schema.eschema(etype).rdef(rtype).constraints: |
0 | 1200 |
if isinstance(constr, SizeConstraint): |
1201 |
oldvalue = constr.max |
|
1202 |
if oldvalue == size: |
|
1203 |
return |
|
1204 |
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
|
1205 |
ceid = self.rqlexec('INSERT CWConstraint C: C value %(v)s, C cstrtype CT ' |
0 | 1206 |
'WHERE CT name "SizeConstraint"', |
1207 |
{'v': SizeConstraint(size).serialize()}, |
|
1208 |
ask_confirm=self.verbosity>=2)[0][0] |
|
1209 |
self.rqlexec('SET X constrained_by C WHERE X from_entity S, X relation_type R, ' |
|
1210 |
'S name "%s", R name "%s", C eid %s' % (etype, rtype, ceid), |
|
1211 |
ask_confirm=self.verbosity>=2) |
|
1212 |
elif not oldvalue is None: |
|
1213 |
if not size is None: |
|
1214 |
self.rqlexec('SET C value %%(v)s WHERE X from_entity S, X relation_type R,' |
|
1215 |
'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
|
1216 |
'S name "%s", R name "%s"' % (etype, rtype), |
|
1217 |
{'v': unicode(SizeConstraint(size).serialize())}, |
|
1218 |
ask_confirm=self.verbosity>=2) |
|
1219 |
else: |
|
1220 |
self.rqlexec('DELETE X constrained_by C WHERE X from_entity S, X relation_type R,' |
|
1221 |
'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
|
1222 |
'S name "%s", R name "%s"' % (etype, rtype), |
|
1223 |
ask_confirm=self.verbosity>=2) |
|
1224 |
# cleanup unused constraints |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1225 |
self.rqlexec('DELETE CWConstraint C WHERE NOT X constrained_by C') |
0 | 1226 |
if commit: |
1227 |
self.commit() |
|
1399
3f408c7a164e
unify schema sync migration commands with (hopefuly) a clearer name
sylvain.thenault@logilab.fr
parents:
1398
diff
changeset
|
1228 |
|
3577
067610a0e12f
add version number to deprecation messages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3548
diff
changeset
|
1229 |
@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
|
1230 |
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
|
1231 |
self.cmd_sync_schema_props_perms(ertype, syncprops=False, commit=commit) |
1477 | 1232 |
|
0 | 1233 |
# Workflows handling ###################################################### |
1477 | 1234 |
|
6805
d0d7345581d4
[migration]Â add_workflow now ensure the entity type is actually workflowable, and a new make_workflowable migration function is available to make an entity type workflowable (closes #1382720)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6686
diff
changeset
|
1235 |
def cmd_make_workflowable(self, etype): |
d0d7345581d4
[migration]Â add_workflow now ensure the entity type is actually workflowable, and a new make_workflowable migration function is available to make an entity type workflowable (closes #1382720)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6686
diff
changeset
|
1236 |
"""add workflow relations to an entity type to make it workflowable""" |
d0d7345581d4
[migration]Â add_workflow now ensure the entity type is actually workflowable, and a new make_workflowable migration function is available to make an entity type workflowable (closes #1382720)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6686
diff
changeset
|
1237 |
self.cmd_add_relation_definition(etype, 'in_state', 'State') |
d0d7345581d4
[migration]Â add_workflow now ensure the entity type is actually workflowable, and a new make_workflowable migration function is available to make an entity type workflowable (closes #1382720)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6686
diff
changeset
|
1238 |
self.cmd_add_relation_definition(etype, 'custom_workflow', 'Workflow') |
d0d7345581d4
[migration]Â add_workflow now ensure the entity type is actually workflowable, and a new make_workflowable migration function is available to make an entity type workflowable (closes #1382720)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6686
diff
changeset
|
1239 |
self.cmd_add_relation_definition('TrInfo', 'wf_info_for', etype) |
d0d7345581d4
[migration]Â add_workflow now ensure the entity type is actually workflowable, and a new make_workflowable migration function is available to make an entity type workflowable (closes #1382720)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6686
diff
changeset
|
1240 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1241 |
def cmd_add_workflow(self, name, wfof, default=True, commit=False, |
6816
f61de39cd396
[wf migration] allow to deactivate assertion on wfable entity to quick-fix tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6815
diff
changeset
|
1242 |
ensure_workflowable=True, **kwargs): |
3582
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1243 |
""" |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1244 |
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
|
1245 |
:type name: unicode |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1246 |
:param name: name of the workflow |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1247 |
|
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1248 |
:type wfof: string or list/tuple of strings |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1249 |
:param wfof: entity type(s) having this workflow |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1250 |
|
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1251 |
:type default: bool |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1252 |
:param default: tells wether this is the default workflow |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1253 |
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
|
1254 |
the case of a subworkflow |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1255 |
|
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1256 |
:rtype: `Workflow` |
28547f21308e
two bits of docstring
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3577
diff
changeset
|
1257 |
""" |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1258 |
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
|
1259 |
**kwargs) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1260 |
if not isinstance(wfof, (list, tuple)): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1261 |
wfof = (wfof,) |
6805
d0d7345581d4
[migration]Â add_workflow now ensure the entity type is actually workflowable, and a new make_workflowable migration function is available to make an entity type workflowable (closes #1382720)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6686
diff
changeset
|
1262 |
def _missing_wf_rel(etype): |
d0d7345581d4
[migration]Â add_workflow now ensure the entity type is actually workflowable, and a new make_workflowable migration function is available to make an entity type workflowable (closes #1382720)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6686
diff
changeset
|
1263 |
return 'missing workflow relations, see make_workflowable(%s)' % etype |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1264 |
for etype in wfof: |
6805
d0d7345581d4
[migration]Â add_workflow now ensure the entity type is actually workflowable, and a new make_workflowable migration function is available to make an entity type workflowable (closes #1382720)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6686
diff
changeset
|
1265 |
eschema = self.repo.schema[etype] |
6816
f61de39cd396
[wf migration] allow to deactivate assertion on wfable entity to quick-fix tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6815
diff
changeset
|
1266 |
if ensure_workflowable: |
f61de39cd396
[wf migration] allow to deactivate assertion on wfable entity to quick-fix tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6815
diff
changeset
|
1267 |
assert 'in_state' in eschema.subjrels, _missing_wf_rel(etype) |
f61de39cd396
[wf migration] allow to deactivate assertion on wfable entity to quick-fix tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6815
diff
changeset
|
1268 |
assert 'custom_workflow' in eschema.subjrels, _missing_wf_rel(etype) |
f61de39cd396
[wf migration] allow to deactivate assertion on wfable entity to quick-fix tests
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6815
diff
changeset
|
1269 |
assert 'wf_info_for' in eschema.objrels, _missing_wf_rel(etype) |
2956
6a57c0be0e58
[migration] don't ask confirm here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2943
diff
changeset
|
1270 |
rset = self.rqlexec( |
6a57c0be0e58
[migration] don't ask confirm here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2943
diff
changeset
|
1271 |
'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
|
1272 |
{'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
|
1273 |
assert rset, 'unexistant entity type %s' % etype |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1274 |
if default: |
2956
6a57c0be0e58
[migration] don't ask confirm here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2943
diff
changeset
|
1275 |
self.rqlexec( |
6a57c0be0e58
[migration] don't ask confirm here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2943
diff
changeset
|
1276 |
'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
|
1277 |
{'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
|
1278 |
if commit: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1279 |
self.commit() |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1280 |
return wf |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1281 |
|
7295
b6fd14ee491e
[migration] new command to get default workflow for an entity type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7166
diff
changeset
|
1282 |
def cmd_get_workflow_for(self, etype): |
b6fd14ee491e
[migration] new command to get default workflow for an entity type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7166
diff
changeset
|
1283 |
"""return default workflow for the given entity type""" |
b6fd14ee491e
[migration] new command to get default workflow for an entity type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7166
diff
changeset
|
1284 |
rset = self.rqlexec('Workflow X WHERE ET default_workflow X, ET name %(et)s', |
b6fd14ee491e
[migration] new command to get default workflow for an entity type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7166
diff
changeset
|
1285 |
{'et': etype}) |
b6fd14ee491e
[migration] new command to get default workflow for an entity type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7166
diff
changeset
|
1286 |
return rset.get_entity(0, 0) |
b6fd14ee491e
[migration] new command to get default workflow for an entity type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7166
diff
changeset
|
1287 |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1288 |
# 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
|
1289 |
def _get_or_create_wf(self, etypes): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1290 |
if not isinstance(etypes, (list, tuple)): |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1291 |
etypes = (etypes,) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1292 |
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
|
1293 |
{'et': etypes[0]}) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1294 |
if rset: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1295 |
return rset.get_entity(0, 0) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1296 |
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
|
1297 |
|
4339
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4330
diff
changeset
|
1298 |
@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
|
1299 |
stacklevel=3) |
0 | 1300 |
def cmd_add_state(self, name, stateof, initial=False, commit=False, **kwargs): |
1301 |
"""method to ease workflow definition: add a state for one or more |
|
1302 |
entity type(s) |
|
1303 |
""" |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1304 |
wf = self._get_or_create_wf(stateof) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1305 |
state = wf.add_state(name, initial, **kwargs) |
0 | 1306 |
if commit: |
1307 |
self.commit() |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1308 |
return state.eid |
1477 | 1309 |
|
4339
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4330
diff
changeset
|
1310 |
@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
|
1311 |
stacklevel=3) |
0 | 1312 |
def cmd_add_transition(self, name, transitionof, fromstates, tostate, |
1313 |
requiredgroups=(), conditions=(), commit=False, **kwargs): |
|
1314 |
"""method to ease workflow definition: add a transition for one or more |
|
1315 |
entity type(s), from one or more state and to a single state |
|
1316 |
""" |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1317 |
wf = self._get_or_create_wf(transitionof) |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1318 |
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
|
1319 |
conditions, **kwargs) |
0 | 1320 |
if commit: |
1321 |
self.commit() |
|
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1322 |
return tr.eid |
0 | 1323 |
|
4339
fe93b670343d
some fixes so that deprecation warning are properly localized
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4330
diff
changeset
|
1324 |
@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
|
1325 |
stacklevel=3) |
0 | 1326 |
def cmd_set_transition_permissions(self, treid, |
1327 |
requiredgroups=(), conditions=(), |
|
1328 |
reset=True, commit=False): |
|
1329 |
"""set or add (if `reset` is False) groups and conditions for a |
|
1330 |
transition |
|
1331 |
""" |
|
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1332 |
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
|
1333 |
tr.set_transition_permissions(requiredgroups, conditions, reset) |
0 | 1334 |
if commit: |
1335 |
self.commit() |
|
1336 |
||
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
1337 |
@deprecated('[3.5] use iworkflowable.fire_transition("transition") or ' |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
1338 |
'iworkflowable.change_state("state")', stacklevel=3) |
972 | 1339 |
def cmd_set_state(self, eid, statename, commit=False): |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5552
diff
changeset
|
1340 |
self._cw.entity_from_eid(eid).cw_adapt_to('IWorkflowable').change_state(statename) |
972 | 1341 |
if commit: |
1342 |
self.commit() |
|
1477 | 1343 |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
1344 |
# CWProperty handling ###################################################### |
0 | 1345 |
|
1346 |
def cmd_property_value(self, pkey): |
|
7111
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1347 |
"""retreive the site-wide persistent property value for the given key. |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1348 |
|
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1349 |
To get a user specific property value, use appropriate method on CWUser |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1350 |
instance. |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1351 |
""" |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1352 |
rset = self.rqlexec( |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1353 |
'Any V WHERE X is CWProperty, X pkey %(k)s, X value V, NOT X for_user U', |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1354 |
{'k': pkey}, ask_confirm=False) |
0 | 1355 |
return rset[0][0] |
1356 |
||
1357 |
def cmd_set_property(self, pkey, value): |
|
7111
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1358 |
"""set the site-wide persistent property value for the given key to the |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1359 |
given value. |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1360 |
|
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1361 |
To set a user specific property value, use appropriate method on CWUser |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1362 |
instance. |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1363 |
""" |
0 | 1364 |
value = unicode(value) |
1365 |
try: |
|
7111
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1366 |
prop = self.rqlexec( |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1367 |
'CWProperty X WHERE X pkey %(k)s, NOT X for_user U', |
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1368 |
{'k': pkey}, ask_confirm=False).get_entity(0, 0) |
0 | 1369 |
except: |
2920
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1370 |
self.cmd_create_entity('CWProperty', pkey=unicode(pkey), value=value) |
0 | 1371 |
else: |
7111
6c8e8747268d
[cwprops, migration] fix [set_]property to ensure we don't retrieve a user specific value. Add some docstring along the way
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7008
diff
changeset
|
1372 |
prop.set_attributes(value=value) |
0 | 1373 |
|
1374 |
# other data migration commands ########################################### |
|
1477 | 1375 |
|
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1376 |
@property |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1377 |
def _cw(self): |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1378 |
session = self.session |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1379 |
if session is not None: |
7398
26695dd703d8
[repository api] definitly kill usage of word 'pool' to refer to connections set used by a session
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
1380 |
session.set_cnxset() |
3700
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1381 |
return session |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1382 |
return self.cnx.request() |
fd550e4dc515
#481017: cubicweb-ctl shell on remote instance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3689
diff
changeset
|
1383 |
|
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
|
1384 |
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
|
1385 |
"""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
|
1386 |
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
|
1387 |
""" |
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
|
1388 |
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
|
1389 |
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
|
1390 |
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
|
1391 |
source.unset_storage(etype, attribute) |
5552 | 1392 |
rset = self.rqlexec('Any X WHERE X is %s' % etype, ask_confirm=False) |
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
|
1393 |
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
|
1394 |
for entity in rset.entities(): |
5551
e41cd3bf5080
[migration] fix storage_change migraction to avoid exhausting the server's memory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
1395 |
# fill cache. Do not fetch that attribute using the global rql query |
e41cd3bf5080
[migration] fix storage_change migraction to avoid exhausting the server's memory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
1396 |
# since we may exhaust memory doing that.... |
e41cd3bf5080
[migration] fix storage_change migraction to avoid exhausting the server's memory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
1397 |
getattr(entity, attribute) |
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
|
1398 |
storage.migrate_entity(entity, attribute) |
5551
e41cd3bf5080
[migration] fix storage_change migraction to avoid exhausting the server's memory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
1399 |
# remove from entity cache to avoid memory exhaustion |
e41cd3bf5080
[migration] fix storage_change migraction to avoid exhausting the server's memory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5426
diff
changeset
|
1400 |
del entity[attribute] |
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
|
1401 |
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
|
1402 |
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
|
1403 |
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
|
1404 |
|
5132
260d73ad4f24
[cleaning] simpler implementation of cmd_create_entity
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5043
diff
changeset
|
1405 |
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
|
1406 |
"""add a new entity of the given type""" |
3708
95e8c3a9698a
update prototype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3707
diff
changeset
|
1407 |
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
|
1408 |
if commit: |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1409 |
self.commit() |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1410 |
return entity |
64322aa83a1d
start a new workflow engine
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2903
diff
changeset
|
1411 |
|
7116
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1412 |
def cmd_find_entities(self, etype, **kwargs): |
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1413 |
"""find entities of the given type and attribute values""" |
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1414 |
return self._cw.find_entities(etype, **kwargs) |
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1415 |
|
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1416 |
def cmd_find_one_entity(self, etype, **kwargs): |
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1417 |
"""find one entity of the given type and attribute values. |
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1418 |
|
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1419 |
raise :exc:`cubicweb.req.FindEntityError` if can not return one and only |
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1420 |
one entity. |
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1421 |
""" |
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1422 |
return self._cw.find_one_entity(etype, **kwargs) |
dfd4680a23f0
[session] add find_entities and find_one_entity to session/request API (closes #1550045)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7111
diff
changeset
|
1423 |
|
5928
d9936c39d478
[migration] new migration command to change fti weight for an entity t ype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5897
diff
changeset
|
1424 |
def cmd_update_etype_fti_weight(self, etype, weight): |
d9936c39d478
[migration] new migration command to change fti weight for an entity t ype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5897
diff
changeset
|
1425 |
if self.repo.system_source.dbdriver == 'postgres': |
d9936c39d478
[migration] new migration command to change fti weight for an entity t ype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5897
diff
changeset
|
1426 |
self.sqlexec('UPDATE appears SET weight=%(weight)s ' |
d9936c39d478
[migration] new migration command to change fti weight for an entity t ype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5897
diff
changeset
|
1427 |
'FROM entities as X ' |
d9936c39d478
[migration] new migration command to change fti weight for an entity t ype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5897
diff
changeset
|
1428 |
'WHERE X.eid=appears.uid AND X.type=%(type)s', |
d9936c39d478
[migration] new migration command to change fti weight for an entity t ype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5897
diff
changeset
|
1429 |
{'type': etype, 'weight': weight}, ask_confirm=False) |
d9936c39d478
[migration] new migration command to change fti weight for an entity t ype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5897
diff
changeset
|
1430 |
|
5850
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5666
diff
changeset
|
1431 |
def cmd_reindex_entities(self, etypes=None): |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5666
diff
changeset
|
1432 |
"""force reindexaction of entities of the given types or of all |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5666
diff
changeset
|
1433 |
indexable entity types |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5666
diff
changeset
|
1434 |
""" |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5666
diff
changeset
|
1435 |
from cubicweb.server.checkintegrity import reindex_entities |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5666
diff
changeset
|
1436 |
reindex_entities(self.repo.schema, self.session, etypes=etypes) |
fabff2813ee4
[migration] schema should be accessed through .repo
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5666
diff
changeset
|
1437 |
|
4367
fa02f7dccfe4
fix deprecation warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4344
diff
changeset
|
1438 |
@deprecated('[3.5] use create_entity', stacklevel=3) |
0 | 1439 |
def cmd_add_entity(self, etype, *args, **kwargs): |
1440 |
"""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
|
1441 |
return self.cmd_create_entity(etype, *args, **kwargs).eid |
1477 | 1442 |
|
6292
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1443 |
@contextmanager |
7409
47c8009dbbe2
[migractions] dropped_constraints should accept only droprequired without cstrtype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
1444 |
def cmd_dropped_constraints(self, etype, attrname, cstrtype=None, |
6292
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1445 |
droprequired=False): |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1446 |
"""context manager to drop constraints temporarily on fs_schema |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1447 |
|
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1448 |
`cstrtype` should be a constraint class (or a tuple of classes) |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1449 |
and will be passed to isinstance directly |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1450 |
|
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1451 |
For instance:: |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1452 |
|
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1453 |
>>> with dropped_constraints('MyType', 'myattr', |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1454 |
... UniqueConstraint, droprequired=True): |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1455 |
... add_attribute('MyType', 'myattr') |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1456 |
... # + instructions to fill MyType.myattr column |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1457 |
... |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1458 |
>>> |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1459 |
|
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1460 |
""" |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1461 |
rdef = self.fs_schema.eschema(etype).rdef(attrname) |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1462 |
original_constraints = rdef.constraints |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1463 |
# remove constraints |
7409
47c8009dbbe2
[migractions] dropped_constraints should accept only droprequired without cstrtype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
1464 |
if cstrtype: |
47c8009dbbe2
[migractions] dropped_constraints should accept only droprequired without cstrtype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
1465 |
rdef.constraints = [cstr for cstr in original_constraints |
47c8009dbbe2
[migractions] dropped_constraints should accept only droprequired without cstrtype
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7342
diff
changeset
|
1466 |
if not (cstrtype and isinstance(cstr, cstrtype))] |
6292
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1467 |
if droprequired: |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1468 |
original_cardinality = rdef.cardinality |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1469 |
rdef.cardinality = '?' + rdef.cardinality[1] |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1470 |
yield |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1471 |
# restore original constraints |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1472 |
rdef.constraints = original_constraints |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1473 |
if droprequired: |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1474 |
rdef.cardinality = original_cardinality |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1475 |
# update repository schema |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1476 |
self.cmd_sync_schema_props_perms(rdef, syncperms=False) |
054fa36060d5
[migractions] add a dropped_constraints() migration command
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
6217
diff
changeset
|
1477 |
|
0 | 1478 |
def sqlexec(self, sql, args=None, ask_confirm=True): |
1479 |
"""execute the given sql if confirmed |
|
1477 | 1480 |
|
0 | 1481 |
should only be used for low level stuff undoable with existing higher |
1482 |
level actions |
|
1483 |
""" |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
1484 |
if not ask_confirm or self.confirm('Execute sql: %s ?' % sql): |
0 | 1485 |
try: |
1486 |
cu = self.session.system_sql(sql, args) |
|
1487 |
except: |
|
1488 |
ex = sys.exc_info()[1] |
|
6187
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6186
diff
changeset
|
1489 |
if self.confirm('Error: %s\nabort?' % ex, pdb=True): |
0 | 1490 |
raise |
1491 |
return |
|
1492 |
try: |
|
1493 |
return cu.fetchall() |
|
1494 |
except: |
|
1495 |
# no result to fetch |
|
1496 |
return |
|
1477 | 1497 |
|
4945
356662a6f06c
[migration] new build_descr argument to rqlexec on the migration helper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4925
diff
changeset
|
1498 |
def rqlexec(self, rql, kwargs=None, cachekey=None, build_descr=True, |
6186
8ada7df95877
[migration] set ask_confirm=False by default on rqlexec
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6179
diff
changeset
|
1499 |
ask_confirm=False): |
0 | 1500 |
"""rql action""" |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5132
diff
changeset
|
1501 |
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
|
1502 |
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
|
1503 |
DeprecationWarning, stacklevel=2) |
0 | 1504 |
if not isinstance(rql, (tuple, list)): |
1505 |
rql = ( (rql, kwargs), ) |
|
1506 |
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
|
1507 |
execute = self._cw.execute |
0 | 1508 |
for rql, kwargs in rql: |
1509 |
if kwargs: |
|
1510 |
msg = '%s (%s)' % (rql, kwargs) |
|
1511 |
else: |
|
1512 |
msg = rql |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
1513 |
if not ask_confirm or self.confirm('Execute rql: %s ?' % msg): |
0 | 1514 |
try: |
5174
78438ad513ca
#759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5132
diff
changeset
|
1515 |
res = execute(rql, kwargs, build_descr=build_descr) |
0 | 1516 |
except Exception, ex: |
6187
348c7d93cda3
[migration] propose to open pdb on error during execution of rql/sql query
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6186
diff
changeset
|
1517 |
if self.confirm('Error: %s\nabort?' % ex, pdb=True): |
0 | 1518 |
raise |
1519 |
return res |
|
1520 |
||
1521 |
def rqliter(self, rql, kwargs=None, ask_confirm=True): |
|
6686
070e3b23160d
[migration] fix rqliter when some query arguments are given
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
1522 |
return ForRqlIterator(self, rql, kwargs, ask_confirm) |
0 | 1523 |
|
1524 |
# broken db commands ###################################################### |
|
1525 |
||
1526 |
def cmd_change_attribute_type(self, etype, attr, newtype, commit=True): |
|
1527 |
"""low level method to change the type of an entity attribute. This is |
|
1528 |
a quick hack which has some drawback: |
|
1529 |
* only works when the old type can be changed to the new type by the |
|
1530 |
underlying rdbms (eg using ALTER TABLE) |
|
1531 |
* the actual schema won't be updated until next startup |
|
1532 |
""" |
|
1533 |
rschema = self.repo.schema.rschema(attr) |
|
1534 |
oldtype = rschema.objects(etype)[0] |
|
1535 |
rdefeid = rschema.rproperty(etype, oldtype, 'eid') |
|
7410
1dbf9a83bb49
[migractions] fix change_attribute_type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7409
diff
changeset
|
1536 |
sql = ("UPDATE cw_CWAttribute " |
1dbf9a83bb49
[migractions] fix change_attribute_type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7409
diff
changeset
|
1537 |
"SET cw_to_entity=(SELECT cw_eid FROM cw_CWEType WHERE cw_name='%s')" |
1dbf9a83bb49
[migractions] fix change_attribute_type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7409
diff
changeset
|
1538 |
"WHERE cw_eid=%s") % (newtype, rdefeid) |
0 | 1539 |
self.sqlexec(sql, ask_confirm=False) |
1540 |
dbhelper = self.repo.system_source.dbhelper |
|
1541 |
sqltype = dbhelper.TYPE_MAPPING[newtype] |
|
7410
1dbf9a83bb49
[migractions] fix change_attribute_type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7409
diff
changeset
|
1542 |
sql = 'ALTER TABLE cw_%s ALTER COLUMN cw_%s TYPE %s' % (etype, attr, sqltype) |
0 | 1543 |
self.sqlexec(sql, ask_confirm=False) |
1544 |
if commit: |
|
1545 |
self.commit() |
|
1477 | 1546 |
|
0 | 1547 |
def cmd_add_entity_type_table(self, etype, commit=True): |
1548 |
"""low level method to create the sql table for an existing entity. |
|
1549 |
This may be useful on accidental desync between the repository schema |
|
1550 |
and a sql database |
|
1551 |
""" |
|
1552 |
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
|
1553 |
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
|
1554 |
prefix=SQL_PREFIX) |
0 | 1555 |
for sql in tablesql.split(';'): |
1556 |
if sql.strip(): |
|
1557 |
self.sqlexec(sql) |
|
1558 |
if commit: |
|
1559 |
self.commit() |
|
1477 | 1560 |
|
0 | 1561 |
def cmd_add_relation_type_table(self, rtype, commit=True): |
1562 |
"""low level method to create the sql table for an existing relation. |
|
1563 |
This may be useful on accidental desync between the repository schema |
|
1564 |
and a sql database |
|
1565 |
""" |
|
1566 |
dbhelper = self.repo.system_source.dbhelper |
|
1567 |
tablesql = rschema2sql(dbhelper, self.repo.schema.rschema(rtype)) |
|
1568 |
for sql in tablesql.split(';'): |
|
1569 |
if sql.strip(): |
|
1570 |
self.sqlexec(sql) |
|
1571 |
if commit: |
|
1572 |
self.commit() |
|
1477 | 1573 |
|
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1574 |
@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
|
1575 |
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
|
1576 |
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
|
1577 |
|
4843
5f7363416765
fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4835
diff
changeset
|
1578 |
@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
|
1579 |
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
|
1580 |
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
|
1581 |
|
0 | 1582 |
|
1583 |
class ForRqlIterator: |
|
1584 |
"""specific rql iterator to make the loop skipable""" |
|
1585 |
def __init__(self, helper, rql, kwargs, ask_confirm): |
|
1586 |
self._h = helper |
|
1587 |
self.rql = rql |
|
1588 |
self.kwargs = kwargs |
|
1589 |
self.ask_confirm = ask_confirm |
|
1590 |
self._rsetit = None |
|
1477 | 1591 |
|
0 | 1592 |
def __iter__(self): |
1593 |
return self |
|
1477 | 1594 |
|
6125
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1595 |
def _get_rset(self): |
0 | 1596 |
rql, kwargs = self.rql, self.kwargs |
1597 |
if kwargs: |
|
1598 |
msg = '%s (%s)' % (rql, kwargs) |
|
1599 |
else: |
|
1600 |
msg = rql |
|
1601 |
if self.ask_confirm: |
|
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
1602 |
if not self._h.confirm('Execute rql: %s ?' % msg): |
0 | 1603 |
raise StopIteration |
1604 |
try: |
|
6125
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1605 |
return self._h._cw.execute(rql, kwargs) |
0 | 1606 |
except Exception, ex: |
4650
965395d821bc
typo: capitalize migration confirmation messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
4640
diff
changeset
|
1607 |
if self._h.confirm('Error: %s\nabort?' % ex): |
0 | 1608 |
raise |
1609 |
else: |
|
1610 |
raise StopIteration |
|
6125
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1611 |
|
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1612 |
def next(self): |
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1613 |
if self._rsetit is not None: |
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1614 |
return self._rsetit.next() |
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1615 |
rset = self._get_rset() |
0 | 1616 |
self._rsetit = iter(rset) |
1617 |
return self._rsetit.next() |
|
6125
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1618 |
|
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1619 |
def entities(self): |
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1620 |
try: |
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1621 |
rset = self._get_rset() |
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1622 |
except StopIteration: |
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1623 |
return [] |
46b9cbbc1e37
[migration] enhance RqlForIterator so we can do 'for e in rqliter(...).entities()'
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6118
diff
changeset
|
1624 |
return rset.entities() |