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