From 6ae5cceab4e71c946da62c9b5700398ebffc0661 Mon Sep 17 00:00:00 2001 From: Wandenberg Peixoto Date: Mon, 24 Jan 2011 00:36:22 -0200 Subject: [PATCH] closing some cursors --- simple_db_migrate/mysql.py | 8 ++++++-- tests/mysql_test.py | 12 ++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/simple_db_migrate/mysql.py b/simple_db_migrate/mysql.py index 0119257..859879f 100755 --- a/simple_db_migrate/mysql.py +++ b/simple_db_migrate/mysql.py @@ -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") @@ -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() @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/tests/mysql_test.py b/tests/mysql_test.py index ba59e3a..51a2638 100644 --- a/tests/mysql_test.py +++ b/tests/mysql_test.py @@ -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') @@ -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') @@ -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') @@ -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') @@ -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') @@ -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') @@ -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') @@ -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') @@ -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')