54 if dbhost: |
54 if dbhost: |
55 print '%s@%s' % (dbname, dbhost), |
55 print '%s@%s' % (dbname, dbhost), |
56 else: |
56 else: |
57 print dbname, |
57 print dbname, |
58 if dbhelper.users_support: |
58 if dbhelper.users_support: |
59 if not verbose or (not special_privs and source.get('db-user')): |
59 if not special_privs and source.get('db-user'): |
60 user = source['db-user'] |
60 user = source['db-user'] |
61 if verbose: |
61 if verbose: |
62 print 'as', user |
62 print 'as', user |
63 if source.get('db-password'): |
63 password = source.get('db-password') |
64 password = source['db-password'] |
|
65 else: |
|
66 password = getpass('password: ') |
|
67 else: |
64 else: |
68 print |
65 if verbose: |
|
66 print |
69 if special_privs: |
67 if special_privs: |
70 print 'WARNING' |
68 print 'WARNING' |
71 print ('the user will need the following special access rights ' |
69 print ('the user will need the following special access rights ' |
72 'on the database:') |
70 'on the database:') |
73 print special_privs |
71 print special_privs |
74 print |
72 print |
75 default_user = source.get('db-user', os.environ.get('USER', '')) |
73 default_user = source.get('db-user', os.environ.get('USER', '')) |
76 user = raw_input('Connect as user ? [%r]: ' % default_user) |
74 user = raw_input('Connect as user ? [%r]: ' % default_user) |
77 user = user or default_user |
75 user = user.strip() or default_user |
78 if user == source.get('db-user') and source.get('db-password'): |
76 if user == source.get('db-user'): |
79 password = source['db-password'] |
77 password = source['db-password'] |
80 else: |
78 else: |
81 password = getpass('password: ') |
79 password = getpass('password: ') |
82 else: |
80 else: |
83 user = password = None |
81 user = password = None |
106 from logilab.database import get_db_helper |
104 from logilab.database import get_db_helper |
107 system_db = get_db_helper(source['db-driver']).system_database() |
105 system_db = get_db_helper(source['db-driver']).system_database() |
108 return source_cnx(source, system_db, special_privs=special_privs, verbose=verbose) |
106 return source_cnx(source, system_db, special_privs=special_privs, verbose=verbose) |
109 return source_cnx(source, special_privs=special_privs, verbose=verbose) |
107 return source_cnx(source, special_privs=special_privs, verbose=verbose) |
110 |
108 |
111 def _db_sys_cnx(source, what, db=None, user=None, verbose=True): |
109 def _db_sys_cnx(source, special_privs, verbose=True): |
112 """return a connection on the RDMS system table (to create/drop a user |
110 """return a connection on the RDMS system table (to create/drop a user or a |
113 or a database |
111 database) |
114 """ |
112 """ |
115 import logilab.common as lgp |
113 import logilab.common as lgp |
116 from logilab.database import get_db_helper |
114 from logilab.database import get_db_helper |
117 lgp.USE_MX_DATETIME = False |
115 lgp.USE_MX_DATETIME = False |
118 special_privs = '' |
|
119 driver = source['db-driver'] |
116 driver = source['db-driver'] |
120 helper = get_db_helper(driver) |
117 helper = get_db_helper(driver) |
121 if user is not None and helper.users_support: |
|
122 special_privs += '%s USER' % what |
|
123 if db is not None: |
|
124 special_privs += ' %s DATABASE' % what |
|
125 # connect on the dbms system base to create our base |
118 # connect on the dbms system base to create our base |
126 cnx = system_source_cnx(source, True, special_privs=special_privs, verbose=verbose) |
119 cnx = system_source_cnx(source, True, special_privs=special_privs, |
|
120 verbose=verbose) |
127 # disable autocommit (isolation_level(1)) because DROP and |
121 # disable autocommit (isolation_level(1)) because DROP and |
128 # CREATE DATABASE can't be executed in a transaction |
122 # CREATE DATABASE can't be executed in a transaction |
129 try: |
123 try: |
130 cnx.set_isolation_level(0) |
124 cnx.set_isolation_level(0) |
131 except AttributeError: |
125 except AttributeError: |
311 ASK.confirm('Database %s already exists. Drop it?' % dbname)): |
305 ASK.confirm('Database %s already exists. Drop it?' % dbname)): |
312 os.unlink(dbname) |
306 os.unlink(dbname) |
313 elif self.config.create_db: |
307 elif self.config.create_db: |
314 print '\n'+underline_title('Creating the system database') |
308 print '\n'+underline_title('Creating the system database') |
315 # connect on the dbms system base to create our base |
309 # connect on the dbms system base to create our base |
316 dbcnx = _db_sys_cnx(source, 'CREATE DATABASE and / or USER', verbose=verbose) |
310 dbcnx = _db_sys_cnx(source, 'CREATE/DROP DATABASE and / or USER', verbose=verbose) |
317 cursor = dbcnx.cursor() |
311 cursor = dbcnx.cursor() |
318 try: |
312 try: |
319 if helper.users_support: |
313 if helper.users_support: |
320 user = source['db-user'] |
314 user = source['db-user'] |
321 if not helper.user_exists(cursor, user) and (automatic or \ |
315 if not helper.user_exists(cursor, user) and (automatic or \ |
331 dbcnx.commit() |
325 dbcnx.commit() |
332 print '-> database %s created.' % dbname |
326 print '-> database %s created.' % dbname |
333 except: |
327 except: |
334 dbcnx.rollback() |
328 dbcnx.rollback() |
335 raise |
329 raise |
336 cnx = system_source_cnx(source, special_privs='LANGUAGE C', verbose=verbose) |
330 cnx = system_source_cnx(source, special_privs='CREATE LANGUAGE', |
|
331 verbose=verbose) |
337 cursor = cnx.cursor() |
332 cursor = cnx.cursor() |
338 helper.init_fti_extensions(cursor) |
333 helper.init_fti_extensions(cursor) |
339 # postgres specific stuff |
334 # postgres specific stuff |
340 if driver == 'postgres': |
335 if driver == 'postgres': |
341 # install plpythonu/plpgsql language if not installed by the cube |
336 # install plpythonu/plpgsql language if not installed by the cube |