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