comparison roundup/backends/back_postgresql.py @ 2633:a9e1fff1e793

I thought I committed this last night. Ho hum. - This implements most of the rest of the new tracker config layout: - dbinit.py split between schema.py and initial_data.py - interfaces.py gone - tracker and detectors __init__.py gone - Added some missing functionality to backends: db_exists test and db_nuke. - Implemented configuration file options in postgresql backend. - Cleaned up tracker initialisation a lot.
author Richard Jones <richard@users.sourceforge.net>
date Tue, 27 Jul 2004 00:57:19 +0000
parents 091711fb2f8c
children f47ca4541770
comparison
equal deleted inserted replaced
2632:9c55f2bc5961 2633:a9e1fff1e793
12 import psycopg 12 import psycopg
13 13
14 from roundup import hyperdb, date 14 from roundup import hyperdb, date
15 from roundup.backends import rdbms_common 15 from roundup.backends import rdbms_common
16 16
17 from roundup import configuration
18
19 configuration.SETTINGS += (
20 ("postgresql", (
21 (configuration.Option, 'database', 'roundup'),
22 (configuration.NullableOption, 'host', 'localhost'),
23 (configuration.NullableOption, 'port', '5432'),
24 (configuration.NullableOption, 'user', 'roundup'),
25 (configuration.NullableOption, 'password', 'roundup'),
26 )),
27 )
28
29 def connection_dict(config):
30 d = {
31 'database': config.POSTGRESQL_DATABASE,
32 }
33 for name in 'host', 'port', 'password', 'user':
34 cvar = 'POSTGRESQL_'+name.upper()
35 if config[cvar] is not None:
36 d[name] = config[cvar]
37 return d
38
17 def db_create(config): 39 def db_create(config):
18 """Clear all database contents and drop database itself""" 40 """Clear all database contents and drop database itself"""
19 command = 'CREATE DATABASE %s'%config.POSTGRESQL_DATABASE['database'] 41 command = 'CREATE DATABASE %s'%config.POSTGRESQL_DATABASE
20 config.logging.getLogger('hyperdb').info(command) 42 config.logging.getLogger('hyperdb').info(command)
21 db_command(config, command) 43 db_command(config, command)
22 44
23 def db_nuke(config, fail_ok=0): 45 def db_nuke(config, fail_ok=0):
24 """Clear all database contents and drop database itself""" 46 """Clear all database contents and drop database itself"""
25 command = 'DROP DATABASE %s'% config.POSTGRESQL_DATABASE['database'] 47 command = 'DROP DATABASE %s'% config.POSTGRESQL_DATABASE
26 config.logging.getLogger('hyperdb').info(command) 48 config.logging.getLogger('hyperdb').info(command)
27 db_command(config, command) 49 db_command(config, command)
28 50
29 if os.path.exists(config.DATABASE): 51 if os.path.exists(config.DATABASE):
30 shutil.rmtree(config.DATABASE) 52 shutil.rmtree(config.DATABASE)
31 53
32 def db_command(config, command): 54 def db_command(config, command):
33 '''Perform some sort of database-level command. Retry 10 times if we 55 '''Perform some sort of database-level command. Retry 10 times if we
34 fail by conflicting with another user. 56 fail by conflicting with another user.
35 ''' 57 '''
36 template1 = config.POSTGRESQL_DATABASE.copy() 58 template1 = connection_dict(config)
37 template1['database'] = 'template1' 59 template1['database'] = 'template1'
38 60
39 try: 61 try:
40 conn = psycopg.connect(**template1) 62 conn = psycopg.connect(**template1)
41 except psycopg.OperationalError, message: 63 except psycopg.OperationalError, message:
68 return 0 90 return 0
69 return 1 91 return 1
70 92
71 def db_exists(config): 93 def db_exists(config):
72 """Check if database already exists""" 94 """Check if database already exists"""
73 db = getattr(config, 'POSTGRESQL_DATABASE') 95 db = connection_dict(config)
74 try: 96 try:
75 conn = psycopg.connect(**db) 97 conn = psycopg.connect(**db)
76 conn.close() 98 conn.close()
77 return 1 99 return 1
78 except: 100 except:
80 102
81 class Database(rdbms_common.Database): 103 class Database(rdbms_common.Database):
82 arg = '%s' 104 arg = '%s'
83 105
84 def sql_open_connection(self): 106 def sql_open_connection(self):
85 db = getattr(self.config, 'POSTGRESQL_DATABASE') 107 db = connection_dict(config)
86 self.config.logging.getLogger('hyperdb').info('open database %r'%db) 108 self.config.logging.getLogger('hyperdb').info('open database %r'%db)
87 try: 109 try:
88 conn = psycopg.connect(**db) 110 conn = psycopg.connect(**db)
89 except psycopg.OperationalError, message: 111 except psycopg.OperationalError, message:
90 raise hyperdb.DatabaseError, message 112 raise hyperdb.DatabaseError, message

Roundup Issue Tracker: http://roundup-tracker.org/