61 password = source['db-password'] |
61 password = source['db-password'] |
62 else: |
62 else: |
63 password = getpass('password: ') |
63 password = getpass('password: ') |
64 extra_args = source.get('db-extra-arguments') |
64 extra_args = source.get('db-extra-arguments') |
65 extra = extra_args and {'extra_args': extra_args} or {} |
65 extra = extra_args and {'extra_args': extra_args} or {} |
66 return get_connection(driver, dbhost, dbname, user, password=password, |
66 cnx = get_connection(driver, dbhost, dbname, user, password=password, |
67 port=source.get('db-port'), |
67 port=source.get('db-port'), |
68 **extra) |
68 **extra) |
|
69 cnx.logged_user = logged_user |
|
70 return cnx |
69 |
71 |
70 def system_source_cnx(source, dbms_system_base=False, |
72 def system_source_cnx(source, dbms_system_base=False, |
71 special_privs='CREATE/DROP DATABASE', verbose=True): |
73 special_privs='CREATE/DROP DATABASE', verbose=True): |
72 """shortcut to get a connextion to the instance system database |
74 """shortcut to get a connextion to the instance system database |
73 defined in the given config. If <dbms_system_base> is True, |
75 defined in the given config. If <dbms_system_base> is True, |
74 connect to the dbms system database instead (for task such as |
76 connect to the dbms system database instead (for task such as |
75 create/drop the instance database) |
77 create/drop the instance database) |
76 """ |
78 """ |
77 if dbms_system_base: |
79 if dbms_system_base: |
78 from logilab.common.adbh import get_adv_func_helper |
80 from logilab.db import get_db_helper |
79 system_db = get_adv_func_helper(source['db-driver']).system_database() |
81 system_db = get_db_helper(source['db-driver']).system_database() |
80 return source_cnx(source, system_db, special_privs=special_privs, verbose=verbose) |
82 return source_cnx(source, system_db, special_privs=special_privs, verbose=verbose) |
81 return source_cnx(source, special_privs=special_privs, verbose=verbose) |
83 return source_cnx(source, special_privs=special_privs, verbose=verbose) |
82 |
84 |
83 def _db_sys_cnx(source, what, db=None, user=None, verbose=True): |
85 def _db_sys_cnx(source, what, db=None, user=None, verbose=True): |
84 """return a connection on the RDMS system table (to create/drop a user |
86 """return a connection on the RDMS system table (to create/drop a user |
85 or a database |
87 or a database |
86 """ |
88 """ |
87 import logilab.common as lgp |
89 import logilab.common as lgp |
88 from logilab.common.adbh import get_adv_func_helper |
90 from logilab.db import get_db_helper |
89 lgp.USE_MX_DATETIME = False |
91 lgp.USE_MX_DATETIME = False |
90 special_privs = '' |
92 special_privs = '' |
91 driver = source['db-driver'] |
93 driver = source['db-driver'] |
92 helper = get_adv_func_helper(driver) |
94 helper = get_db_helper(driver) |
93 if user is not None and helper.users_support: |
95 if user is not None and helper.users_support: |
94 special_privs += '%s USER' % what |
96 special_privs += '%s USER' % what |
95 if db is not None: |
97 if db is not None: |
96 special_privs += ' %s DATABASE' % what |
98 special_privs += ' %s DATABASE' % what |
97 # connect on the dbms system base to create our base |
99 # connect on the dbms system base to create our base |
200 cmdname = 'delete' |
202 cmdname = 'delete' |
201 cfgname = 'repository' |
203 cfgname = 'repository' |
202 |
204 |
203 def cleanup(self): |
205 def cleanup(self): |
204 """remove instance's configuration and database""" |
206 """remove instance's configuration and database""" |
205 from logilab.common.adbh import get_adv_func_helper |
207 from logilab.db import get_db_helper |
206 source = self.config.sources()['system'] |
208 source = self.config.sources()['system'] |
207 dbname = source['db-name'] |
209 dbname = source['db-name'] |
208 helper = get_adv_func_helper(source['db-driver']) |
210 helper = get_db_helper(source['db-driver']) |
209 if ASK.confirm('Delete database %s ?' % dbname): |
211 if ASK.confirm('Delete database %s ?' % dbname): |
210 user = source['db-user'] or None |
212 user = source['db-user'] or None |
211 cnx = _db_sys_cnx(source, 'DROP DATABASE', user=user) |
213 cnx = _db_sys_cnx(source, 'DROP DATABASE', user=user) |
212 cursor = cnx.cursor() |
214 cursor = cnx.cursor() |
213 try: |
215 try: |
283 } |
285 } |
284 ), |
286 ), |
285 ) |
287 ) |
286 def run(self, args): |
288 def run(self, args): |
287 """run the command with its specific arguments""" |
289 """run the command with its specific arguments""" |
288 from logilab.common.adbh import get_adv_func_helper |
290 from logilab.db import get_db_helper |
289 from indexer import get_indexer |
|
290 verbose = self.get('verbose') |
291 verbose = self.get('verbose') |
291 automatic = self.get('automatic') |
292 automatic = self.get('automatic') |
292 appid = pop_arg(args, msg='No instance specified !') |
293 appid = pop_arg(args, msg='No instance specified !') |
293 config = ServerConfiguration.config_for(appid) |
294 config = ServerConfiguration.config_for(appid) |
294 source = config.sources()['system'] |
295 source = config.sources()['system'] |
295 dbname = source['db-name'] |
296 dbname = source['db-name'] |
296 driver = source['db-driver'] |
297 driver = source['db-driver'] |
297 create_db = self.config.create_db |
298 create_db = self.config.create_db |
298 helper = get_adv_func_helper(driver) |
299 helper = get_db_helper(driver) |
299 if driver == 'sqlite': |
300 if driver == 'sqlite': |
300 if os.path.exists(dbname) and automatic or \ |
301 if os.path.exists(dbname) and automatic or \ |
301 ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname): |
302 ASK.confirm('Database %s already exists -- do you want to drop it ?' % dbname): |
302 os.unlink(dbname) |
303 os.unlink(dbname) |
303 elif create_db: |
304 elif create_db: |
328 except: |
329 except: |
329 dbcnx.rollback() |
330 dbcnx.rollback() |
330 raise |
331 raise |
331 cnx = system_source_cnx(source, special_privs='LANGUAGE C', verbose=verbose) |
332 cnx = system_source_cnx(source, special_privs='LANGUAGE C', verbose=verbose) |
332 cursor = cnx.cursor() |
333 cursor = cnx.cursor() |
333 indexer = get_indexer(driver) |
334 dbhelper.init_fti_extensions(cursor) |
334 indexer.init_extensions(cursor) |
|
335 # postgres specific stuff |
335 # postgres specific stuff |
336 if driver == 'postgres': |
336 if driver == 'postgres': |
337 # install plpythonu/plpgsql language if not installed by the cube |
337 # install plpythonu/plpgsql language if not installed by the cube |
338 langs = sys.platform == 'win32' and ('plpgsql',) or ('plpythonu', 'plpgsql') |
338 langs = sys.platform == 'win32' and ('plpgsql',) or ('plpythonu', 'plpgsql') |
339 for extlang in langs: |
339 for extlang in langs: |