Skip to content

Commit b3c69a3

Browse files
author
Richard Jones
committed
Both RDBMS backends now use the same config.ini section, [rdbms].
Comments on each option clearly state that the options only apply to postgresql or mysql.
1 parent 245876b commit b3c69a3

File tree

4 files changed

+48
-50
lines changed

4 files changed

+48
-50
lines changed

roundup/backends/back_mysql.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,27 @@
3939
import os, shutil
4040
from MySQLdb.constants import ER
4141

42-
4342
def db_nuke(config):
4443
"""Clear all database contents and drop database itself"""
4544
if db_exists(config):
46-
conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER,
47-
config.MYSQL_DBPASSWORD)
45+
kwargs = rdbms_common.connection_dict(config)
46+
conn = MySQLdb.connect(**kwargs)
4847
try:
49-
conn.select_db(config.MYSQL_DBNAME)
48+
conn.select_db(config.RDBMS_NAME)
5049
except:
5150
# no, it doesn't exist
5251
pass
5352
else:
5453
cursor = conn.cursor()
5554
cursor.execute("SHOW TABLES")
5655
tables = cursor.fetchall()
56+
# stupid MySQL bug requires us to drop all the tables first
5757
for table in tables:
5858
command = 'DROP TABLE %s'%table[0]
5959
if __debug__:
6060
config.logging.getLogger('hyperdb').debug(command)
6161
cursor.execute(command)
62-
command = "DROP DATABASE %s"%config.MYSQL_DBNAME
62+
command = "DROP DATABASE %s"%config.RDBMS_NAME
6363
config.logging.getLogger('hyperdb').info(command)
6464
cursor.execute(command)
6565
conn.commit()
@@ -70,28 +70,22 @@ def db_nuke(config):
7070

7171
def db_create(config):
7272
"""Create the database."""
73-
conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER,
74-
config.MYSQL_DBPASSWORD)
73+
kwargs = rdbms_common.connection_dict(config)
74+
conn = MySQLdb.connect(**kwargs)
7575
cursor = conn.cursor()
76-
command = "CREATE DATABASE %s"%config.MYSQL_DBNAME
76+
command = "CREATE DATABASE %s"%config.RDBMS_NAME
7777
config.logging.getLogger('hyperdb').info(command)
7878
cursor.execute(command)
7979
conn.commit()
8080
conn.close()
8181

8282
def db_exists(config):
8383
"""Check if database already exists."""
84-
conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER,
85-
config.MYSQL_DBPASSWORD)
86-
# tables = None
84+
kwargs = rdbms_common.connection_dict(config)
85+
conn = MySQLdb.connect(**kwargs)
8786
try:
8887
try:
89-
conn.select_db(config.MYSQL_DBNAME)
90-
# cursor = conn.cursor()
91-
# cursor.execute("SHOW TABLES")
92-
# tables = cursor.fetchall()
93-
# if __debug__:
94-
# print >>hyperdb.DEBUG, "tables %s"%(tables,)
88+
conn.select_db(config.RDBMS_NAME)
9589
except MySQLdb.OperationalError:
9690
return 0
9791
finally:
@@ -131,10 +125,11 @@ class Database(Database):
131125
}
132126

133127
def sql_open_connection(self):
134-
db = getattr(self.config, 'MYSQL_DATABASE')
135-
self.config.logging.getLogger('hyperdb').info('open database %r'%(db,))
128+
kwargs = rdbms_common.connection_dict(config, 'db')
129+
self.config.logging.getLogger('hyperdb').info('open database %r'%(
130+
kwargs['db'],))
136131
try:
137-
conn = MySQLdb.connect(*db)
132+
conn = MySQLdb.connect(**kwargs)
138133
except MySQLdb.OperationalError, message:
139134
raise DatabaseError, message
140135
cursor = conn.cursor()

roundup/backends/back_postgresql.py

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,15 @@
1414
from roundup import hyperdb, date
1515
from roundup.backends import rdbms_common
1616

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-
3917
def db_create(config):
4018
"""Clear all database contents and drop database itself"""
41-
command = 'CREATE DATABASE %s'%config.POSTGRESQL_DATABASE
19+
command = 'CREATE DATABASE %s'%config.RDBMS_NAME
4220
config.logging.getLogger('hyperdb').info(command)
4321
db_command(config, command)
4422

4523
def db_nuke(config, fail_ok=0):
4624
"""Clear all database contents and drop database itself"""
47-
command = 'DROP DATABASE %s'% config.POSTGRESQL_DATABASE
25+
command = 'DROP DATABASE %s'% config.RDBMS_NAME
4826
config.logging.getLogger('hyperdb').info(command)
4927
db_command(config, command)
5028

@@ -55,7 +33,7 @@ def db_command(config, command):
5533
'''Perform some sort of database-level command. Retry 10 times if we
5634
fail by conflicting with another user.
5735
'''
58-
template1 = connection_dict(config)
36+
template1 = rdbms_common.connection_dict(config)
5937
template1['database'] = 'template1'
6038

6139
try:
@@ -92,7 +70,7 @@ def pg_command(cursor, command):
9270

9371
def db_exists(config):
9472
"""Check if database already exists"""
95-
db = connection_dict(config)
73+
db = rdbms_common.connection_dict(config, 'database')
9674
try:
9775
conn = psycopg.connect(**db)
9876
conn.close()
@@ -104,8 +82,9 @@ class Database(rdbms_common.Database):
10482
arg = '%s'
10583

10684
def sql_open_connection(self):
107-
db = connection_dict(config)
108-
self.config.logging.getLogger('hyperdb').info('open database %r'%db)
85+
db = rdbms_common.connection_dict(config, 'database')
86+
self.config.logging.getLogger('hyperdb').info('open database %r'%(
87+
db['database'],))
10988
try:
11089
conn = psycopg.connect(**db)
11190
except psycopg.OperationalError, message:

roundup/backends/rdbms_common.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $Id: rdbms_common.py,v 1.124 2004-07-20 05:58:07 richard Exp $
1+
# $Id: rdbms_common.py,v 1.125 2004-07-27 01:18:25 richard Exp $
22
''' Relational database (SQL) backend common code.
33
44
Basics:
@@ -59,6 +59,18 @@ def _bool_cvt(value):
5959
# assume it's a number returned from the db API
6060
return int(value)
6161

62+
def connection_dict(config, dbnamestr=None):
63+
''' Used by Postgresql and MySQL to detemine the keyword args for
64+
opening the database connection.'''
65+
d = { }
66+
if dbnamestr:
67+
d[dbnamestr] = config.RDBMS_NAME
68+
for name in 'host', 'port', 'password', 'user':
69+
cvar = 'RDBMS_'+name.upper()
70+
if config[cvar] is not None:
71+
d[name] = config[cvar]
72+
return d
73+
6274
class Database(FileStorage, hyperdb.Database, roundupdb.Database):
6375
''' Wrapper around an SQL database that presents a hyperdb interface.
6476

roundup/configuration.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Roundup Issue Tracker configuration support
22
#
3-
# $Id: configuration.py,v 1.10 2004-07-26 09:13:17 a1s Exp $
3+
# $Id: configuration.py,v 1.11 2004-07-27 01:18:25 richard Exp $
44
#
55
__docformat__ = "restructuredtext"
66

@@ -444,6 +444,18 @@ class NullableFilePathOption(NullableOption, FilePathOption):
444444
(MailAddressOption, "email", "issue_tracker",
445445
"Email address that mail to roundup should go to."),
446446
)),
447+
("rdbms", (
448+
(Option, 'name', 'roundup',
449+
"Name of the Postgresql or MySQL database to use."),
450+
(NullableOption, 'host', 'localhost'
451+
"Hostname that the Postgresql or MySQL database resides on."),
452+
(NullableOption, 'port', '5432'
453+
"Port number that the Postgresql or MySQL database resides on."),
454+
(NullableOption, 'user', 'roundup'
455+
"Postgresql or MySQL database user that Roundup should use."),
456+
(NullableOption, 'password', 'roundup',
457+
"Password for the Postgresql or MySQL database user."),
458+
)),
447459
("logging", (
448460
(FilePathOption, "config", "",
449461
"Path to configuration file for standard Python logging module.\n"

0 commit comments

Comments
 (0)