200 def init_repository(config, interactive=True, drop=False, vreg=None): |
200 def init_repository(config, interactive=True, drop=False, vreg=None): |
201 """initialise a repository database by creating tables add filling them |
201 """initialise a repository database by creating tables add filling them |
202 with the minimal set of entities (ie at least the schema, base groups and |
202 with the minimal set of entities (ie at least the schema, base groups and |
203 a initial user) |
203 a initial user) |
204 """ |
204 """ |
205 from cubicweb.dbapi import in_memory_repo_cnx |
205 from cubicweb.repoapi import get_repository, connect |
206 from cubicweb.server.repository import Repository |
206 from cubicweb.server.repository import Repository |
207 from cubicweb.server.utils import manager_userpasswd |
207 from cubicweb.server.utils import manager_userpasswd |
208 from cubicweb.server.sqlutils import sqlexec, sqlschema, sql_drop_all_user_tables |
208 from cubicweb.server.sqlutils import sqlexec, sqlschema, sql_drop_all_user_tables |
209 from cubicweb.server.sqlutils import _SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION as drop_filter |
209 from cubicweb.server.sqlutils import _SQL_DROP_ALL_USER_TABLES_FILTER_FUNCTION as drop_filter |
210 # configuration to avoid db schema loading and user'state checking |
210 # configuration to avoid db schema loading and user'state checking |
279 {'u': admin.eid}) |
279 {'u': admin.eid}) |
280 cnx.commit() |
280 cnx.commit() |
281 repo.shutdown() |
281 repo.shutdown() |
282 # reloging using the admin user |
282 # reloging using the admin user |
283 config._cubes = None # avoid assertion error |
283 config._cubes = None # avoid assertion error |
284 repo, cnx = in_memory_repo_cnx(config, login, password=pwd) |
284 repo = get_repository(config=config) |
285 repo.system_source.eid = ssource.eid # redo this manually |
285 with connect(repo, login, password=pwd) as cnx: |
286 handler = config.migration_handler(schema, interactive=False, |
286 repo.system_source.eid = ssource.eid # redo this manually |
287 cnx=cnx, repo=repo) |
287 handler = config.migration_handler(schema, interactive=False, |
288 # install additional driver specific sql files |
288 cnx=cnx, repo=repo) |
289 handler.cmd_install_custom_sql_scripts() |
289 # install additional driver specific sql files |
290 for cube in reversed(config.cubes()): |
290 handler.cmd_install_custom_sql_scripts() |
291 handler.cmd_install_custom_sql_scripts(cube) |
291 for cube in reversed(config.cubes()): |
292 # serialize the schema |
292 handler.cmd_install_custom_sql_scripts(cube) |
293 initialize_schema(config, schema, handler) |
293 # serialize the schema |
294 # yoo ! |
294 initialize_schema(config, schema, handler) |
295 cnx.commit() |
295 # yoo ! |
296 repo.system_source.init_creating() |
296 cnx.commit() |
297 cnx.commit() |
297 repo.system_source.init_creating() |
298 cnx.close() |
298 cnx.commit() |
299 repo.shutdown() |
299 repo.shutdown() |
300 # restore initial configuration |
300 # restore initial configuration |
301 config.creating = False |
301 config.creating = False |
302 config.consider_user_state = True |
302 config.consider_user_state = True |
303 # (drop instance attribute to get back to class attribute) |
303 # (drop instance attribute to get back to class attribute) |
306 print '-> database for instance %s initialized.' % config.appid |
306 print '-> database for instance %s initialized.' % config.appid |
307 |
307 |
308 |
308 |
309 def initialize_schema(config, schema, mhandler, event='create'): |
309 def initialize_schema(config, schema, mhandler, event='create'): |
310 from cubicweb.server.schemaserial import serialize_schema |
310 from cubicweb.server.schemaserial import serialize_schema |
311 session = mhandler.session |
311 cnx = mhandler.cnx |
312 cubes = config.cubes() |
312 cubes = config.cubes() |
313 # deactivate every hooks but those responsible to set metadata |
313 # deactivate every hooks but those responsible to set metadata |
314 # so, NO INTEGRITY CHECKS are done, to have quicker db creation. |
314 # so, NO INTEGRITY CHECKS are done, to have quicker db creation. |
315 # Active integrity is kept else we may pb such as two default |
315 # Active integrity is kept else we may pb such as two default |
316 # workflows for one entity type. |
316 # workflows for one entity type. |
317 with session.deny_all_hooks_but('metadata', 'activeintegrity'): |
317 with cnx._cnx.deny_all_hooks_but('metadata', 'activeintegrity'): |
318 # execute cubicweb's pre<event> script |
318 # execute cubicweb's pre<event> script |
319 mhandler.cmd_exec_event_script('pre%s' % event) |
319 mhandler.cmd_exec_event_script('pre%s' % event) |
320 # execute cubes pre<event> script if any |
320 # execute cubes pre<event> script if any |
321 for cube in reversed(cubes): |
321 for cube in reversed(cubes): |
322 mhandler.cmd_exec_event_script('pre%s' % event, cube) |
322 mhandler.cmd_exec_event_script('pre%s' % event, cube) |
323 # execute instance's pre<event> script (useful in tests) |
323 # execute instance's pre<event> script (useful in tests) |
324 mhandler.cmd_exec_event_script('pre%s' % event, apphome=True) |
324 mhandler.cmd_exec_event_script('pre%s' % event, apphome=True) |
325 # enter instance'schema into the database |
325 # enter instance'schema into the database |
326 session.set_cnxset() |
326 serialize_schema(cnx, schema) |
327 serialize_schema(session, schema) |
|
328 # execute cubicweb's post<event> script |
327 # execute cubicweb's post<event> script |
329 mhandler.cmd_exec_event_script('post%s' % event) |
328 mhandler.cmd_exec_event_script('post%s' % event) |
330 # execute cubes'post<event> script if any |
329 # execute cubes'post<event> script if any |
331 for cube in reversed(cubes): |
330 for cube in reversed(cubes): |
332 mhandler.cmd_exec_event_script('post%s' % event, cube) |
331 mhandler.cmd_exec_event_script('post%s' % event, cube) |