equal
deleted
inserted
replaced
24 import os |
24 import os |
25 from contextlib import contextmanager |
25 from contextlib import contextmanager |
26 |
26 |
27 from logilab.common.configuration import Configuration, merge_options |
27 from logilab.common.configuration import Configuration, merge_options |
28 from logilab.common.shellutils import ASK, generate_password |
28 from logilab.common.shellutils import ASK, generate_password |
|
29 from logilab.common.deprecation import deprecated |
29 |
30 |
30 from logilab.database import get_db_helper, get_connection |
31 from logilab.database import get_db_helper, get_connection |
31 |
32 |
32 from cubicweb import AuthenticationError, ExecutionError, ConfigurationError, SourceException |
33 from cubicweb import AuthenticationError, ExecutionError, ConfigurationError, SourceException |
33 from cubicweb.toolsutils import Command, CommandHandler, underline_title |
34 from cubicweb.toolsutils import Command, CommandHandler, underline_title |
94 cnx = _SimpleConnectionWrapper(cnx) |
95 cnx = _SimpleConnectionWrapper(cnx) |
95 cnx.logged_user = user |
96 cnx.logged_user = user |
96 return cnx |
97 return cnx |
97 |
98 |
98 |
99 |
|
100 @deprecated('[3.28] instead of system_source_cnx(source, True, **args) use ' |
|
101 'source_cnx(source, get_db_helper(source[\'db-driver\']).system_database(), ' |
|
102 '**args)') |
99 def system_source_cnx(source, dbms_system_base=False, |
103 def system_source_cnx(source, dbms_system_base=False, |
100 special_privs='CREATE/DROP DATABASE', interactive=True): |
104 special_privs='CREATE/DROP DATABASE', interactive=True): |
101 """shortcut to get a connextion to the instance system database |
105 """shortcut to get a connextion to the instance system database |
102 defined in the given config. If <dbms_system_base> is True, |
106 defined in the given config. If <dbms_system_base> is True, |
103 connect to the dbms system database instead (for task such as |
107 connect to the dbms system database instead (for task such as |
116 database) |
120 database) |
117 """ |
121 """ |
118 import logilab.common as lgp |
122 import logilab.common as lgp |
119 lgp.USE_MX_DATETIME = False |
123 lgp.USE_MX_DATETIME = False |
120 # connect on the dbms system base to create our base |
124 # connect on the dbms system base to create our base |
121 cnx = system_source_cnx(source, True, special_privs=special_privs, |
125 system_db = get_db_helper(source['db-driver']).system_database() |
122 interactive=interactive) |
126 cnx = source_cnx(source, system_db, special_privs=special_privs, |
|
127 interactive=interactive) |
123 # disable autocommit (isolation_level(1)) because DROP and |
128 # disable autocommit (isolation_level(1)) because DROP and |
124 # CREATE DATABASE can't be executed in a transaction |
129 # CREATE DATABASE can't be executed in a transaction |
125 set_isolation_level = getattr(cnx, 'set_isolation_level', None) |
130 set_isolation_level = getattr(cnx, 'set_isolation_level', None) |
126 if set_isolation_level is not None: |
131 if set_isolation_level is not None: |
127 # set_isolation_level() is psycopg specific |
132 # set_isolation_level() is psycopg specific |
199 |
204 |
200 |
205 |
201 @contextmanager |
206 @contextmanager |
202 def db_transaction(source, privilege): |
207 def db_transaction(source, privilege): |
203 """Open a transaction to the instance database""" |
208 """Open a transaction to the instance database""" |
204 cnx = system_source_cnx(source, special_privs=privilege) |
209 cnx = source_cnx(source, special_privs=privilege) |
205 cursor = cnx.cursor() |
210 cursor = cnx.cursor() |
206 try: |
211 try: |
207 yield cursor |
212 yield cursor |
208 except: |
213 except: |
209 cnx.rollback() |
214 cnx.rollback() |
377 dbcnx.commit() |
382 dbcnx.commit() |
378 print('-> database %s created.' % dbname) |
383 print('-> database %s created.' % dbname) |
379 except BaseException: |
384 except BaseException: |
380 dbcnx.rollback() |
385 dbcnx.rollback() |
381 raise |
386 raise |
382 cnx = system_source_cnx(source, special_privs='CREATE LANGUAGE/SCHEMA', |
387 cnx = source_cnx(source, special_privs='CREATE LANGUAGE/SCHEMA', |
383 interactive=not automatic) |
388 interactive=not automatic) |
384 cursor = cnx.cursor() |
389 cursor = cnx.cursor() |
385 helper.init_fti_extensions(cursor) |
390 helper.init_fti_extensions(cursor) |
386 namespace = source.get('db-namespace') |
391 namespace = source.get('db-namespace') |
387 if namespace and ASK.confirm('Create schema %s in database %s ?' |
392 if namespace and ASK.confirm('Create schema %s in database %s ?' |
388 % (namespace, dbname)): |
393 % (namespace, dbname)): |
570 from cubicweb.server.sqlutils import sqlexec, sqlgrants |
575 from cubicweb.server.sqlutils import sqlexec, sqlgrants |
571 appid, user = args |
576 appid, user = args |
572 config = ServerConfiguration.config_for(appid) |
577 config = ServerConfiguration.config_for(appid) |
573 source = config.system_source_config |
578 source = config.system_source_config |
574 set_owner = self.config.set_owner |
579 set_owner = self.config.set_owner |
575 cnx = system_source_cnx(source, special_privs='GRANT') |
580 cnx = source_cnx(source, special_privs='GRANT') |
576 cursor = cnx.cursor() |
581 cursor = cnx.cursor() |
577 schema = config.load_schema() |
582 schema = config.load_schema() |
578 try: |
583 try: |
579 sqlexec(sqlgrants(schema, source['db-driver'], user, |
584 sqlexec(sqlgrants(schema, source['db-driver'], user, |
580 set_owner=set_owner), cursor) |
585 set_owner=set_owner), cursor) |