Skip to content

Commit

Permalink
closing some cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandenberg Peixoto committed Jan 25, 2011
1 parent e3bb65b commit 6ae5cce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 6 additions & 2 deletions simple_db_migrate/mysql.py
Expand Up @@ -7,7 +7,6 @@ class MySQL(object):
def __init__(self, config=None, mysql_driver=None):
self.__mysql_script_encoding = config.get("db_script_encoding", "utf8")
self.__mysql_encoding = config.get("db_encoding", "utf8")
self.__mysql_driver = mysql_driver
self.__mysql_host = config.get("db_host")
self.__mysql_user = config.get("db_user")
self.__mysql_passwd = config.get("db_password")
Expand Down Expand Up @@ -47,7 +46,7 @@ def __execute(self, sql, execution_log=None):
curr_statement = statement
affected_rows = cursor.execute(statement.encode(self.__mysql_script_encoding))
if execution_log:
execution_log("%s\n-- %d row(s) affected\n" % (statement, affected_rows))
execution_log("%s\n-- %d row(s) affected\n" % (statement, affected_rows and int(affected_rows) or 0))
cursor.close()
db.commit()
db.close()
Expand Down Expand Up @@ -130,6 +129,7 @@ def _create_version_table_if_not_exists(self):
cursor = db.cursor()
cursor.execute("select count(*) from %s;" % self.__version_table)
count = cursor.fetchone()[0]
cursor.close()
db.close()

# if there is not a version register, insert one
Expand Down Expand Up @@ -168,6 +168,7 @@ def get_current_schema_version(self):
cursor = db.cursor()
cursor.execute("select version from %s order by id desc limit 0,1;" % self.__version_table)
version = cursor.fetchone()[0]
cursor.close()
db.close()
return version

Expand All @@ -179,6 +180,7 @@ def get_all_schema_versions(self):
all_versions = cursor.fetchall()
for version in all_versions:
versions.append(version[0])
cursor.close()
db.close()
versions.sort()
return versions
Expand All @@ -189,6 +191,7 @@ def get_version_id_from_version_number(self, version):
cursor.execute("select id from %s where version = '%s';" % (self.__version_table, version))
result = cursor.fetchone()
id = result and int(result[0]) or None
cursor.close()
db.close()
return id

Expand All @@ -198,6 +201,7 @@ def get_version_number_from_label(self, label):
cursor.execute("select version from %s where label = '%s';" % (self.__version_table, label))
result = cursor.fetchone()
version = result and result[0] or None
cursor.close()
db.close()
return version

Expand Down
12 changes: 10 additions & 2 deletions tests/mysql_test.py
Expand Up @@ -56,6 +56,7 @@ def test_it_should_create_database_and_version_table_on_init_if_not_exists(self)

cursor_mock.execute('select count(*) from __db_version__;')
cursor_mock.fetchone().AndReturn([1])
cursor_mock.close()

db_mock.set_character_set('utf8')
db_mock.select_db('migration_test')
Expand Down Expand Up @@ -110,6 +111,7 @@ def test_it_should_drop_database_on_init_if_its_asked(self):

cursor_mock.execute('select count(*) from __db_version__;')
cursor_mock.fetchone().AndReturn([1])
cursor_mock.close()

db_mock.set_character_set('utf8')
db_mock.select_db('migration_test')
Expand Down Expand Up @@ -158,7 +160,7 @@ def test_it_should_execute_migration_up_and_update_schema_version(self):
self.mox.VerifyAll()

def test_it_should_execute_migration_down_and_update_schema_version(self):
self.cursor_mock.execute('create table spam()')
self.cursor_mock.execute('drop table spam')
self.cursor_mock.close()

self.db_mock.set_character_set('utf8')
Expand All @@ -179,13 +181,14 @@ def test_it_should_execute_migration_down_and_update_schema_version(self):
self.mox.ReplayAll()

mysql = MySQL(self.config_mock, self.mysql_driver_mock)
mysql.change("create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", False)
mysql.change("drop table spam;", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", False)

self.mox.VerifyAll()

def test_it_should_get_current_schema_version(self):
self.cursor_mock.execute("select version from __db_version__ order by id desc limit 0,1;")
self.cursor_mock.fetchone().AndReturn("0")
self.cursor_mock.close()

self.db_mock.set_character_set('utf8')
self.db_mock.select_db('migration_test')
Expand All @@ -209,6 +212,7 @@ def test_it_should_get_all_schema_versions(self):

self.cursor_mock.execute('select version from __db_version__ order by id;')
self.cursor_mock.fetchall().AndReturn(tuple(zip(expected_versions)))
self.cursor_mock.close()

self.db_mock.set_character_set('utf8')
self.db_mock.select_db('migration_test')
Expand Down Expand Up @@ -309,6 +313,7 @@ def test_it_should_get_none_for_a_non_existent_version_in_database(self):

self.cursor_mock.execute('select id from __db_version__ where version = \'xxx\';')
self.cursor_mock.fetchone().AndReturn(None)
self.cursor_mock.close()

self.db_mock.set_character_set('utf8')
self.db_mock.select_db('migration_test')
Expand Down Expand Up @@ -368,6 +373,7 @@ def test_it_should_update_version_table_on_init_if_dont_have_id_field(self):

cursor_mock.execute('select count(*) from __db_version__;')
cursor_mock.fetchone().AndReturn([1])
cursor_mock.close()

db_mock.set_character_set('utf8')
db_mock.select_db('migration_test')
Expand Down Expand Up @@ -431,6 +437,7 @@ def test_it_should_update_version_table_on_init_if_dont_have_label_field(self):

cursor_mock.execute('select count(*) from __db_version__;')
cursor_mock.fetchone().AndReturn([1])
cursor_mock.close()

db_mock.set_character_set('utf8')
db_mock.select_db('migration_test')
Expand Down Expand Up @@ -484,6 +491,7 @@ def create_database_and_version_table_mocks(self):

cursor_mock.execute('select count(*) from __db_version__;')
cursor_mock.fetchone().AndReturn([1])
cursor_mock.close()

db_mock.set_character_set('utf8')
db_mock.select_db('migration_test')
Expand Down

0 comments on commit 6ae5cce

Please sign in to comment.