author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 04 Aug 2009 18:46:59 +0200 | |
changeset 2686 | c700ace6ebfd |
parent 2633 | bc9386c3b2c9 |
child 2730 | bb6fcb8c5d71 |
permissions | -rw-r--r-- |
0 | 1 |
"""Server subcube of cubicweb : defines objects used only on the server |
2 |
(repository) side |
|
3 |
||
4 |
This module contains functions to initialize a new repository. |
|
5 |
||
6 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 8 |
: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:
1802
diff
changeset
|
9 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 10 |
""" |
11 |
__docformat__ = "restructuredtext en" |
|
12 |
||
13 |
import sys |
|
14 |
from os.path import join, exists |
|
15 |
||
16 |
from logilab.common.modutils import LazyObject |
|
2633
bc9386c3b2c9
get_csv is being renamed to splitstrip
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2631
diff
changeset
|
17 |
from logilab.common.textutils import splitstrip |
0 | 18 |
|
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
19 |
# server-side debugging ######################################################### |
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
20 |
|
2628
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
21 |
# server debugging flags. They may be combined using binary operators. |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
22 |
DBG_NONE = 0 # no debug information |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
23 |
DBG_RQL = 1 # rql execution information |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
24 |
DBG_SQL = 2 # executed sql |
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
25 |
DBG_REPO = 4 # repository events |
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
26 |
DBG_MORE = 8 # repository events |
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
27 |
|
2628
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
28 |
# current debug mode |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
29 |
DEBUG = 0 |
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
30 |
|
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
31 |
def set_debug(debugmode): |
2628
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
32 |
"""change the repository debugging mode""" |
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
33 |
global DEBUG |
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
34 |
if not debugmode: |
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
35 |
DEBUG = 0 |
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
36 |
return |
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
37 |
if isinstance(debugmode, basestring): |
2633
bc9386c3b2c9
get_csv is being renamed to splitstrip
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2631
diff
changeset
|
38 |
for mode in splitstrip(debugmode, sep='|'): |
2628
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
39 |
DEBUG |= globals()[mode] |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
40 |
else: |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
41 |
DEBUG |= debugmode |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
42 |
|
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
43 |
|
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
44 |
class debugged(object): |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
45 |
"""repository debugging context manager / decorator |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
46 |
|
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
47 |
Can be used either as a context manager: |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
48 |
|
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
49 |
>>> with debugged(server.DBG_RQL | server.DBG_REPO): |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
50 |
... # some code in which you want to debug repository activity, |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
51 |
... # seing information about RQL being executed an repository events. |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
52 |
|
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
53 |
or as a function decorator: |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
54 |
|
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
55 |
>>> @debugged(server.DBG_RQL | server.DBG_REPO) |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
56 |
... def some_function(): |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
57 |
... # some code in which you want to debug repository activity, |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
58 |
... # seing information about RQL being executed an repository events |
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
59 |
|
2628
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
60 |
debug mode will be reseted at its original value when leaving the "with" |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
61 |
block or the decorated function |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
62 |
""" |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
63 |
def __init__(self, debugmode): |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
64 |
self.debugmode = debugmode |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
65 |
self._clevel = None |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
66 |
|
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
67 |
def __enter__(self): |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
68 |
"""enter with block""" |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
69 |
self._clevel = DEBUG |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
70 |
set_debug(self.debugmode) |
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
71 |
|
2628
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
72 |
def __exit__(self, exctype, exc, traceback): |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
73 |
"""leave with block""" |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
74 |
set_debug(self._clevel) |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
75 |
return traceback is None |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
76 |
|
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
77 |
def __call__(self, func): |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
78 |
"""decorate function""" |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
79 |
def wrapped(*args, **kwargs): |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
80 |
_clevel = DEBUG |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
81 |
set_debug(self.debugmode) |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
82 |
try: |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
83 |
return func(*args, **kwargs) |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
84 |
finally: |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
85 |
set_debug(self._clevel) |
a2cc32c1d982
document and replace debugged by a contextmanager/decorator class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2595
diff
changeset
|
86 |
return wrapped |
2593
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
87 |
|
16d9419a4a79
F: start to handle binary debug log level on the server side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2589
diff
changeset
|
88 |
# database initialization ###################################################### |
0 | 89 |
|
90 |
def init_repository(config, interactive=True, drop=False, vreg=None): |
|
91 |
"""initialise a repository database by creating tables add filling them |
|
92 |
with the minimal set of entities (ie at least the schema, base groups and |
|
93 |
a initial user) |
|
94 |
""" |
|
95 |
from glob import glob |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
96 |
from yams import BASE_GROUPS |
0 | 97 |
from cubicweb.dbapi import in_memory_cnx |
98 |
from cubicweb.server.repository import Repository |
|
99 |
from cubicweb.server.utils import manager_userpasswd |
|
100 |
from cubicweb.server.sqlutils import sqlexec, sqlschema, sqldropschema |
|
101 |
# configuration to avoid db schema loading and user'state checking |
|
102 |
# on connection |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
103 |
read_instance_schema = config.read_instance_schema |
0 | 104 |
bootstrap_schema = config.bootstrap_schema |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
105 |
config.read_instance_schema = False |
0 | 106 |
config.creating = True |
107 |
config.bootstrap_schema = True |
|
108 |
config.consider_user_state = False |
|
109 |
config.set_language = False |
|
110 |
# only enable the system source at initialization time + admin which is not |
|
111 |
# an actual source but contains initial manager account information |
|
112 |
config.enabled_sources = ('system', 'admin') |
|
113 |
repo = Repository(config, vreg=vreg) |
|
114 |
assert len(repo.sources) == 1, repo.sources |
|
115 |
schema = repo.schema |
|
116 |
sourcescfg = config.sources() |
|
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
117 |
_title = '-> creating tables ' |
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
118 |
print _title, |
0 | 119 |
source = sourcescfg['system'] |
120 |
driver = source['db-driver'] |
|
121 |
sqlcnx = repo.system_source.get_connection() |
|
122 |
sqlcursor = sqlcnx.cursor() |
|
2306
95da5d9f0870
give session to doexec so it's able to rollback the connection on unexpected error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2172
diff
changeset
|
123 |
execute = sqlcursor.execute |
0 | 124 |
if drop: |
125 |
dropsql = sqldropschema(schema, driver) |
|
126 |
try: |
|
127 |
sqlexec(dropsql, execute) |
|
128 |
except Exception, ex: |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2306
diff
changeset
|
129 |
print '-> drop failed, skipped (%s).' % ex |
0 | 130 |
sqlcnx.rollback() |
131 |
# schema entities and relations tables |
|
132 |
# can't skip entities table even if system source doesn't support them, |
|
133 |
# they are used sometimes by generated sql. Keeping them empty is much |
|
134 |
# simpler than fixing this... |
|
135 |
if sqlcnx.logged_user != source['db-user']: |
|
136 |
schemasql = sqlschema(schema, driver, user=source['db-user']) |
|
137 |
else: |
|
138 |
schemasql = sqlschema(schema, driver) |
|
139 |
#skip_entities=[str(e) for e in schema.entities() |
|
140 |
# if not repo.system_source.support_entity(str(e))]) |
|
2396
8bfb99d7bbcc
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2395
diff
changeset
|
141 |
sqlexec(schemasql, execute, pbtitle=_title) |
0 | 142 |
# install additional driver specific sql files |
143 |
for fpath in glob(join(config.schemas_lib_dir(), '*.sql.%s' % driver)): |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2306
diff
changeset
|
144 |
print '-> installing', fpath |
0 | 145 |
sqlexec(open(fpath).read(), execute, False, delimiter=';;') |
146 |
for directory in config.cubes_path(): |
|
147 |
for fpath in glob(join(directory, 'schema', '*.sql.%s' % driver)): |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2306
diff
changeset
|
148 |
print '-> installing', fpath |
0 | 149 |
sqlexec(open(fpath).read(), execute, False, delimiter=';;') |
150 |
sqlcursor.close() |
|
151 |
sqlcnx.commit() |
|
152 |
sqlcnx.close() |
|
153 |
session = repo.internal_session() |
|
154 |
try: |
|
155 |
login = unicode(sourcescfg['admin']['login']) |
|
156 |
pwd = sourcescfg['admin']['password'] |
|
157 |
except KeyError: |
|
158 |
if interactive: |
|
159 |
msg = 'enter login and password of the initial manager account' |
|
160 |
login, pwd = manager_userpasswd(msg=msg, confirm=True) |
|
161 |
else: |
|
162 |
login, pwd = unicode(source['db-user']), source['db-password'] |
|
2394
92bba46b853f
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2306
diff
changeset
|
163 |
print '-> inserting default user and default groups.' |
2595
e76bf303c6f2
[F server test] sort for test predictability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
164 |
# sort for eid predicatability as expected in some server tests |
e76bf303c6f2
[F server test] sort for test predictability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2593
diff
changeset
|
165 |
for group in sorted(BASE_GROUPS): |
2631 | 166 |
session.execute('INSERT CWGroup X: X name %(name)s', |
167 |
{'name': unicode(group)}) |
|
168 |
session.execute('INSERT CWUser X: X login %(login)s, X upassword %(pwd)s', |
|
169 |
{'login': login, 'pwd': pwd}) |
|
0 | 170 |
session.execute('SET U in_group G WHERE G name "managers"') |
171 |
session.commit() |
|
172 |
# reloging using the admin user |
|
173 |
config._cubes = None # avoid assertion error |
|
174 |
repo, cnx = in_memory_cnx(config, login, pwd) |
|
175 |
assert len(repo.sources) == 1, repo.sources |
|
176 |
handler = config.migration_handler(schema, interactive=False, |
|
177 |
cnx=cnx, repo=repo) |
|
178 |
initialize_schema(config, schema, handler) |
|
179 |
# insert versions |
|
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
480
diff
changeset
|
180 |
handler.cmd_add_entity('CWProperty', pkey=u'system.version.cubicweb', |
0 | 181 |
value=unicode(config.cubicweb_version())) |
182 |
for cube in config.cubes(): |
|
2172
cf8f9180e63e
delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
183 |
handler.cmd_add_entity('CWProperty', |
0 | 184 |
pkey=u'system.version.%s' % cube.lower(), |
185 |
value=unicode(config.cube_version(cube))) |
|
2588
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
186 |
# some entities have been added before schema entities, fix the 'is' and |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
187 |
# 'is_instance_of' relations |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
188 |
for rtype in ('is', 'is_instance_of'): |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
189 |
handler.sqlexec( |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
190 |
'INSERT INTO %s_relation ' |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
191 |
'SELECT X.eid, ET.cw_eid FROM entities as X, cw_CWEType as ET ' |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
192 |
'WHERE X.type=ET.cw_name AND NOT EXISTS(' |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
193 |
' SELECT 1 from is_relation ' |
3a590ff82e99
[F schema serial] #344876: missing some 'is'/'is_instance_of' relation for newly created instances
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2396
diff
changeset
|
194 |
' WHERE eid_from=X.eid AND eid_to=ET.cw_eid)' % rtype) |
0 | 195 |
# yoo ! |
196 |
cnx.commit() |
|
197 |
config.enabled_sources = None |
|
198 |
for uri, source_config in config.sources().items(): |
|
199 |
if uri in ('admin', 'system'): |
|
200 |
# not an actual source or init_creating already called |
|
201 |
continue |
|
202 |
source = repo.get_source(uri, source_config) |
|
203 |
source.init_creating() |
|
204 |
cnx.commit() |
|
205 |
cnx.close() |
|
206 |
session.close() |
|
207 |
# restore initial configuration |
|
208 |
config.creating = False |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
209 |
config.read_instance_schema = read_instance_schema |
0 | 210 |
config.bootstrap_schema = bootstrap_schema |
211 |
config.consider_user_state = True |
|
212 |
config.set_language = True |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
213 |
print '-> database for instance %s initialized.' % config.appid |
0 | 214 |
|
215 |
||
216 |
def initialize_schema(config, schema, mhandler, event='create'): |
|
217 |
from cubicweb.server.schemaserial import serialize_schema |
|
218 |
paths = [p for p in config.cubes_path() + [config.apphome] |
|
219 |
if exists(join(p, 'migration'))] |
|
220 |
# execute cubicweb's pre<event> script |
|
221 |
mhandler.exec_event_script('pre%s' % event) |
|
222 |
# execute cubes pre<event> script if any |
|
223 |
for path in reversed(paths): |
|
224 |
mhandler.exec_event_script('pre%s' % event, path) |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
225 |
# enter instance'schema into the database |
0 | 226 |
serialize_schema(mhandler.rqlcursor, schema) |
227 |
# execute cubicweb's post<event> script |
|
228 |
mhandler.exec_event_script('post%s' % event) |
|
229 |
# execute cubes'post<event> script if any |
|
230 |
for path in reversed(paths): |
|
231 |
mhandler.exec_event_script('post%s' % event, path) |
|
232 |
||
233 |
||
234 |
# sqlite'stored procedures have to be registered at connexion opening time |
|
235 |
SQL_CONNECT_HOOKS = {} |
|
236 |
||
237 |
# add to this set relations which should have their add security checking done |
|
238 |
# *BEFORE* adding the actual relation (done after by default) |
|
239 |
BEFORE_ADD_RELATIONS = set(('owned_by',)) |
|
240 |
||
241 |
# add to this set relations which should have their add security checking done |
|
242 |
# *at COMMIT TIME* (done after by default) |
|
243 |
ON_COMMIT_ADD_RELATIONS = set(()) |
|
244 |
||
245 |
# available sources registry |
|
246 |
SOURCE_TYPES = {'native': LazyObject('cubicweb.server.sources.native', 'NativeSQLSource'), |
|
247 |
# XXX private sources installed by an external cube |
|
248 |
'pyrorql': LazyObject('cubicweb.server.sources.pyrorql', 'PyroRQLSource'), |
|
249 |
'ldapuser': LazyObject('cubicweb.server.sources.ldapuser', 'LDAPUserSource'), |
|
250 |
} |