Commit 633f9993 authored by Julien Muchembled's avatar Julien Muchembled

tests: automatically create MySQL DB if necessary

git-svn-id: https://svn.erp5.org/repos/neo/trunk@2807 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent caf58da5
...@@ -250,8 +250,7 @@ class NEOCluster(object): ...@@ -250,8 +250,7 @@ class NEOCluster(object):
self.db_list = db_list self.db_list = db_list
self.address_type = address_type self.address_type = address_type
self.local_ip = local_ip = IP_VERSION_FORMAT_DICT[self.address_type] self.local_ip = local_ip = IP_VERSION_FORMAT_DICT[self.address_type]
if clear_databases: self.setupDB(clear_databases)
self.setupDB()
self.process_dict = {} self.process_dict = {}
if temp_dir is None: if temp_dir is None:
temp_dir = tempfile.mkdtemp(prefix='neo_') temp_dir = tempfile.mkdtemp(prefix='neo_')
...@@ -324,17 +323,22 @@ class NEOCluster(object): ...@@ -324,17 +323,22 @@ class NEOCluster(object):
connect_arg_dict['passwd'] = password connect_arg_dict['passwd'] = password
return MySQLdb.Connect(**connect_arg_dict) return MySQLdb.Connect(**connect_arg_dict)
def setupDB(self): def setupDB(self, clear_databases=True):
if self.adapter == 'MySQL': if self.adapter == 'MySQL':
from MySQLdb.constants.ER import DB_CREATE_EXISTS
sql_connection = self.__getSuperSQLConnection() sql_connection = self.__getSuperSQLConnection()
cursor = sql_connection.cursor() cursor = sql_connection.cursor()
for database in self.db_list: for database in self.db_list:
create = 'CREATE DATABASE `%s`' % database
try: try:
cursor.execute('DROP DATABASE `%s`' % database) cursor.execute(create)
except MySQLdb.OperationalError, (code, _): except MySQLdb.ProgrammingError, (code, _):
if code != MySQLdb.constants.ER.DB_DROP_EXISTS: if code != DB_CREATE_EXISTS:
raise raise
cursor.execute('CREATE DATABASE `%s`' % database) if clear_databases:
cursor.execute('DROP DATABASE `%s`' % database)
cursor.execute(create)
continue
cursor.execute('GRANT ALL ON `%s`.* TO "%s"@"localhost" ' cursor.execute('GRANT ALL ON `%s`.* TO "%s"@"localhost" '
'IDENTIFIED BY "%s"' % (database, self.db_user, 'IDENTIFIED BY "%s"' % (database, self.db_user,
self.db_password)) self.db_password))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment