Skip to content

Commit 66c0e11

Browse files
author
Richard Jones
committed
fix mysql unit tests for recent MySQL releases
1 parent 733fe56 commit 66c0e11

File tree

2 files changed

+70
-48
lines changed

2 files changed

+70
-48
lines changed

roundup/backends/back_mysql.py

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,82 @@
88
# Mysql backend for roundup
99
#
1010

11+
from roundup import hyperdb
1112
from roundup.backends.rdbms_common import *
1213
from roundup.backends import rdbms_common
1314
import MySQLdb
1415
import os, shutil
1516
from MySQLdb.constants import ER
1617

17-
class Maintenance:
18-
""" Database maintenance functions """
19-
def db_nuke(self, config):
20-
"""Clear all database contents and drop database itself"""
21-
db = Database(config, 'admin')
18+
def db_nuke(config):
19+
"""Clear all database contents and drop database itself"""
20+
if db_exists(config):
21+
conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER,
22+
config.MYSQL_DBPASSWORD)
2223
try:
23-
db.sql_commit()
24-
db.sql("DROP DATABASE %s" % config.MYSQL_DBNAME)
25-
db.sql("CREATE DATABASE %s" % config.MYSQL_DBNAME)
26-
finally:
27-
db.close()
28-
if os.path.exists(config.DATABASE):
29-
shutil.rmtree(config.DATABASE)
30-
31-
def db_exists(self, config):
32-
"""Check if database already exists"""
33-
# Yes, this is a hack, but we must must open connection without
34-
# selecting a database to prevent creation of some tables
35-
config.MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER, config.MYSQL_DBPASSWORD)
36-
db = Database(config, 'admin')
24+
conn.select_db(config.MYSQL_DBNAME)
25+
except:
26+
# no, it doesn't exist
27+
pass
28+
else:
29+
cursor = conn.cursor()
30+
cursor.execute("SHOW TABLES")
31+
tables = cursor.fetchall()
32+
for table in tables:
33+
if __debug__:
34+
print >>hyperdb.DEBUG, 'DROP TABLE %s'%table[0]
35+
cursor.execute("DROP TABLE %s"%table[0])
36+
if __debug__:
37+
print >>hyperdb.DEBUG, "DROP DATABASE %s"%config.MYSQL_DBNAME
38+
cursor.execute("DROP DATABASE %s"%config.MYSQL_DBNAME)
39+
conn.commit()
40+
conn.close()
41+
42+
if os.path.exists(config.DATABASE):
43+
shutil.rmtree(config.DATABASE)
44+
45+
def db_create(config):
46+
"""Create the database."""
47+
conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER,
48+
config.MYSQL_DBPASSWORD)
49+
cursor = conn.cursor()
50+
if __debug__:
51+
print >>hyperdb.DEBUG, "CREATE DATABASE %s"%config.MYSQL_DBNAME
52+
cursor.execute("CREATE DATABASE %s"%config.MYSQL_DBNAME)
53+
conn.commit()
54+
conn.close()
55+
56+
def db_exists(config):
57+
"""Check if database already exists."""
58+
conn = MySQLdb.connect(config.MYSQL_DBHOST, config.MYSQL_DBUSER,
59+
config.MYSQL_DBPASSWORD)
60+
# tables = None
61+
try:
3762
try:
38-
db.conn.select_db(config.MYSQL_DBNAME)
39-
config.MYSQL_DATABASE = (config.MYSQL_DBHOST, config.MYSQL_DBUSER,
40-
config.MYSQL_DBPASSWORD, config.MYSQL_DBNAME)
41-
db.sql("SHOW TABLES")
42-
tables = db.sql_fetchall()
43-
finally:
44-
db.close()
45-
if tables or os.path.exists(config.DATABASE):
46-
return 1
47-
return 0
63+
conn.select_db(config.MYSQL_DBNAME)
64+
# cursor = conn.cursor()
65+
# cursor.execute("SHOW TABLES")
66+
# tables = cursor.fetchall()
67+
# if __debug__:
68+
# print >>hyperdb.DEBUG, "tables %s"%(tables,)
69+
except MySQLdb.OperationalError:
70+
if __debug__:
71+
print >>hyperdb.DEBUG, "no database '%s'"%config.MYSQL_DBNAME
72+
return 0
73+
finally:
74+
conn.close()
75+
if __debug__:
76+
print >>hyperdb.DEBUG, "database '%s' exists"%config.MYSQL_DBNAME
77+
return 1
4878

4979
class Database(Database):
5080
arg = '%s'
5181

5282
def open_connection(self):
83+
# make sure the database actually exists
84+
if not db_exists(self.config):
85+
db_create(self.config)
86+
5387
db = getattr(self.config, 'MYSQL_DATABASE')
5488
try:
5589
self.conn = MySQLdb.connect(*db)
@@ -142,9 +176,6 @@ def create_multilink_table(self, spec, ml):
142176
print >>hyperdb.DEBUG, 'create_class', (self, sql)
143177
self.cursor.execute(sql)
144178

145-
# Static methods
146-
nuke = Maintenance().db_nuke
147-
exists = Maintenance().db_exists
148179

149180
class MysqlClass:
150181
def find(self, **propspec):

test/test_db.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_db.py,v 1.90.2.2 2003-11-14 02:47:56 richard Exp $
18+
# $Id: test_db.py,v 1.90.2.3 2004-03-12 00:28:31 richard Exp $
1919

2020
import unittest, os, shutil, time
2121

@@ -814,18 +814,17 @@ def setUp(self):
814814
class mysqlDBTestCase(anydbmDBTestCase):
815815
def setUp(self):
816816
from roundup.backends import mysql
817-
# remove previous test, ignore errors
818-
if os.path.exists(config.DATABASE):
819-
shutil.rmtree(config.DATABASE)
820-
os.makedirs(config.DATABASE + '/files')
817+
mysql.db_nuke(config)
818+
821819
# open database for testing
820+
os.makedirs(config.DATABASE + '/files')
822821
self.db = mysql.Database(config, 'admin')
823822
setupSchema(self.db, 1, mysql)
824-
823+
825824
def tearDown(self):
826825
from roundup.backends import mysql
827826
self.db.close()
828-
mysql.Database.nuke(config)
827+
mysql.db_nuke(config)
829828

830829
class mysqlReadOnlyDBTestCase(anydbmReadOnlyDBTestCase):
831830
def setUp(self):
@@ -840,7 +839,7 @@ def setUp(self):
840839
def tearDown(self):
841840
from roundup.backends import mysql
842841
self.db.close()
843-
mysql.Database.nuke(config)
842+
mysql.db_nuke(config)
844843

845844
class sqliteDBTestCase(anydbmDBTestCase):
846845
def setUp(self):
@@ -953,14 +952,6 @@ def suite():
953952
# Check if we can run mysql tests
954953
import MySQLdb
955954
db = mysql.Database(nodbconfig, 'admin')
956-
db.conn.select_db(config.MYSQL_DBNAME)
957-
db.sql("SHOW TABLES");
958-
tables = db.sql_fetchall()
959-
if 0: #tables:
960-
# Database should be empty. We don't dare to delete any data
961-
raise DatabaseError, "(Database %s contains tables)" % config.MYSQL_DBNAME
962-
db.sql("DROP DATABASE %s" % config.MYSQL_DBNAME)
963-
db.sql("CREATE DATABASE %s" % config.MYSQL_DBNAME)
964955
db.close()
965956
except (MySQLdb.ProgrammingError, DatabaseError), msg:
966957
print "Warning! Mysql tests will not be performed", msg

0 commit comments

Comments
 (0)