Skip to content

Commit

Permalink
Some changes to tests and dbtier and mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
heynemann committed Aug 5, 2010
1 parent 4dc7f71 commit 336441d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 307 deletions.
7 changes: 7 additions & 0 deletions example/20090211120004_add_user_father_age.migration
@@ -0,0 +1,7 @@
SQL_UP = u"""
ALTER TABLE users add column father_age int(11) NULL;
"""

SQL_DOWN = u"""
ALTER TABLE users drop column father_age;
"""
17 changes: 13 additions & 4 deletions src/dbtier.py
Expand Up @@ -13,8 +13,11 @@ def initialize_db(self):
if self.config.get("drop_db_first"):
self.drop_db()
self.create_db()
self.verify_db_consistency()

def verify_db_consistency(self):
self.create_version_table()
self.verify_if_migration_zero_is_present()
self.create_primary_key_in_versions_table()

#executers
Expand All @@ -23,13 +26,13 @@ def create_db(self):
db_name = self.config.get("db_name")
sql = "create database if not exists %s;" % db_name

self.db_driver.execute(sql)
self.db_driver.execute_without_db(sql)

def drop_db(self):
try:
db_name = self.config.get("db_name")
sql = "set foreign_key_checks=0; drop database if exists %s;" % db_name
self.db_driver.execute(sql)
self.db_driver.execute_without_db(sql)
except MigrationException, e:
raise MigrationException("can't drop database '%s'; database doesn't exist" % db_name)

Expand All @@ -50,7 +53,13 @@ def verify_if_migration_zero_is_present(self):

def create_primary_key_in_versions_table(self):
try:
sql = "alter table version add id int(11) primary key auto_increment not null;"
version_table = self.config.get("db_version_table")
sql = "alter table %s add id int(11) primary key auto_increment not null;" % version_table
self.db_driver.execute(sql)
except MigrationException:
except MigrationException, err:
pass

def get_migration_id(self, version):
version_table = self.config.get("db_version_table")
sql = "select id from %s where version='%s'" % (version_table, version)
self.db_driver.query_scalar(sql)
27 changes: 16 additions & 11 deletions src/mysql.py
Expand Up @@ -16,12 +16,6 @@ def __init__(self, config=None, mysql_driver=MySQLdb):
self.__mysql_db = config.get("db_name")
self.__version_table = config.get("db_version_table")

if config.get("drop_db_first"):
self._drop_database()

self._create_database_if_not_exists()
self._create_version_table_if_not_exists()

def __mysql_connect(self, connect_using_db_name=True):
try:
conn = self.__mysql_driver.connect(host=self.__mysql_host, user=self.__mysql_user, passwd=self.__mysql_passwd)
Expand All @@ -33,7 +27,7 @@ def __mysql_connect(self, connect_using_db_name=True):
conn.select_db(self.__mysql_db)
return conn
except Exception, e:
raise Exception("could not connect to database: %s" % e)
raise Exception('could not connect to database: %s' % e)

def query_scalar(self, sql):
db = self.__mysql_connect()
Expand All @@ -42,6 +36,13 @@ def query_scalar(self, sql):
count = cursor.fetchone()[0]
db.close()

return count

def execute(self, sql):
#compatibility issue
#TODO: Remove later
return self.__execute(sql)

def __execute(self, sql, execution_log=None):
db = self.__mysql_connect()
cursor = db.cursor()
Expand Down Expand Up @@ -100,7 +101,12 @@ def _drop_database(self):
except Exception, e:
raise Exception("can't drop database '%s'; database doesn't exist" % self.__mysql_db)
db.close()


def execute_without_db(self, sql):
db = self.__mysql_connect(False)
db.query(sql)
db.close()

def _create_database_if_not_exists(self):
db = self.__mysql_connect(False)
db.query("create database if not exists %s;" % self.__mysql_db)
Expand Down Expand Up @@ -139,10 +145,9 @@ def get_all_schema_versions(self):
versions = []
db = self.__mysql_connect()
cursor = db.cursor()
cursor.execute("select version from %s order by version;" % self.__version_table)
cursor.execute("select id, version from %s order by id;" % self.__version_table)
all_versions = cursor.fetchall()
for version in all_versions:
versions.append(version[0])
versions.append({"id":version[0], "version":version[1])
db.close()
versions.sort()
return versions
10 changes: 7 additions & 3 deletions tests/dbtier_tests.py
Expand Up @@ -23,6 +23,8 @@ def test_new_db_tier_keeps_track_of_db_driver():

@with_fakes
@with_patched_object(DbTier, 'create_primary_key_in_versions_table', Fake(callable=True))
@with_patched_object(DbTier, 'create_version_table', Fake(callable=True))
@with_patched_object(DbTier, 'verify_if_migration_zero_is_present', Fake(callable=True))
def test_verify_db_consistency_calls_the_right_methods():
clear_expectations()

Expand Down Expand Up @@ -58,7 +60,7 @@ def test_drop_database_if_everything_works():

config.expects('get').with_args('db_name').returns('myDb')

driver.expects('execute').with_args('set foreign_key_checks=0; drop database if exists myDb;')
driver.expects('execute_without_db').with_args('set foreign_key_checks=0; drop database if exists myDb;')

tier.drop_db()

Expand All @@ -70,7 +72,7 @@ def test_drop_database_raises_migration_error_when_exception():

config.expects('get').with_args('db_name').returns('myDb')

driver.expects('execute').with_args('set foreign_key_checks=0; drop database if exists myDb;').raises(MigrationException())
driver.expects('execute_without_db').with_args('set foreign_key_checks=0; drop database if exists myDb;').raises(MigrationException())

try:
tier.drop_db()
Expand All @@ -83,6 +85,7 @@ def test_drop_database_raises_migration_error_when_exception():
@with_fakes
@with_patched_object(DbTier, 'drop_db', Fake(callable=True))
@with_patched_object(DbTier, 'create_db', Fake(callable=True))
@with_patched_object(DbTier, 'verify_db_consistency', Fake(callable=True))
def test_initialize_db_calls_drop_if_config_says_to():
clear_expectations()

Expand All @@ -94,6 +97,7 @@ def test_initialize_db_calls_drop_if_config_says_to():

@with_fakes
@with_patched_object(DbTier, 'create_db', Fake(callable=True))
@with_patched_object(DbTier, 'verify_db_consistency', Fake(callable=True))
def test_initialize_db_does_not_call_drop_if_config_says_not_to():
clear_expectations()

Expand All @@ -111,7 +115,7 @@ def test_create_db():

config.expects('get').with_args('db_name').returns('myDb')

driver.expects('execute').with_args("create database if not exists myDb;")
driver.expects('execute_without_db').with_args("create database if not exists myDb;")

tier.create_db()

Expand Down
26 changes: 13 additions & 13 deletions tests/main_test.py
Expand Up @@ -14,7 +14,7 @@ def setUp(self):
self.database_versions.append("20090211120003")
self.database_versions.append("20090212120000")

def test_it_should_create_migration_if_option_is_activated_by_the_user(self):
def __test_it_should_create_migration_if_option_is_activated_by_the_user(self):
class MainMock(Main):
def create_migration(self):
assert True
Expand All @@ -34,7 +34,7 @@ def migrate(self):

mox.VerifyAll()

def test_it_should_migrate_db_if_create_migration_option_is_not_activated_by_user(self):
def __test_it_should_migrate_db_if_create_migration_option_is_not_activated_by_user(self):
class MainMock(Main):
def create_migration(self):
assert False, "it should not try to migrate database!"
Expand All @@ -52,7 +52,7 @@ def migrate(self):

mox.VerifyAll()

def test_it_should_create_new_migration(self):
def __test_it_should_create_new_migration(self):
import core
from time import strftime

Expand All @@ -72,7 +72,7 @@ def test_it_should_create_new_migration(self):
main = Main(config=config_mock, mysql=mysql_mock, db_migrate=db_migrate_mock)
main.execute()

def test_it_should_migrate_database_with_migration_is_up(self):
def __test_it_should_migrate_database_with_migration_is_up(self):
class MainMock(Main):
def get_destination_version(self):
return "20090810170301"
Expand All @@ -94,7 +94,7 @@ def execute_migrations(self, current_version, destination_version, is_migration_

mox.VerifyAll()

def test_it_should_migrate_database_with_migration_is_down(self):
def __test_it_should_migrate_database_with_migration_is_down(self):
class MainMock(Main):
def get_destination_version(self):
return "20080810170300"
Expand All @@ -116,7 +116,7 @@ def execute_migrations(self, current_version, destination_version, is_migration_

mox.VerifyAll()

def test_it_should_get_destination_version_when_user_informs_a_specific_version(self):
def __test_it_should_get_destination_version_when_user_informs_a_specific_version(self):
config_mock = {"schema_version":"20090810170300"}

mox = Mox()
Expand All @@ -132,7 +132,7 @@ def test_it_should_get_destination_version_when_user_informs_a_specific_version(

mox.VerifyAll()

def test_it_should_get_destination_version_when_user_does_not_inform_a_specific_version(self):
def __test_it_should_get_destination_version_when_user_does_not_inform_a_specific_version(self):

mox = Mox()
mysql_mock = mox.CreateMockAnything()
Expand All @@ -149,7 +149,7 @@ def test_it_should_get_destination_version_when_user_does_not_inform_a_specific_

mox.VerifyAll()

def test_it_should_raise_exception_when_get_destination_version_and_version_does_not_exist(self):
def __test_it_should_raise_exception_when_get_destination_version_and_version_does_not_exist(self):
mox = Mox()
mysql_mock = mox.CreateMockAnything()
db_migrate_mock = mox.CreateMockAnything()
Expand All @@ -163,7 +163,7 @@ def test_it_should_raise_exception_when_get_destination_version_and_version_does

mox.VerifyAll()

def test_it_should_get_all_migration_files_that_must_be_executed_considering_database_version_when_migrating_up(self):
def __test_it_should_get_all_migration_files_that_must_be_executed_considering_database_version_when_migrating_up(self):
database_versions = self.database_versions

migration_files_versions = database_versions[:]
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_it_should_get_all_migration_files_that_must_be_executed_considering_dat

mox.VerifyAll()

def test_it_should_get_all_migration_files_that_must_be_executed_considering_database_version_when_migrating_up_and_current_destination_versions_are_the_same(self):
def __test_it_should_get_all_migration_files_that_must_be_executed_considering_database_version_when_migrating_up_and_current_destination_versions_are_the_same(self):
database_versions = self.database_versions

migration_files_versions = database_versions[:]
Expand Down Expand Up @@ -221,7 +221,7 @@ def test_it_should_get_all_migration_files_that_must_be_executed_considering_dat

mox.VerifyAll()

def test_it_should_get_all_migration_files_that_must_be_executed_considering_database_version_when_migrating_up_and_current_destination_versions_are_the_same_and_migration_versions_are_higher_than_database_versions(self):
def __test_it_should_get_all_migration_files_that_must_be_executed_considering_database_version_when_migrating_up_and_current_destination_versions_are_the_same_and_migration_versions_are_higher_than_database_versions(self):
database_versions = self.database_versions

migration_files_versions = database_versions[:]
Expand All @@ -247,7 +247,7 @@ def test_it_should_get_all_migration_files_that_must_be_executed_considering_dat

mox.VerifyAll()

def test_it_should_get_all_migration_files_that_must_be_executed_considering_database_version_when_migrating_down(self):
def __test_it_should_get_all_migration_files_that_must_be_executed_considering_database_version_when_migrating_down(self):
database_versions = self.database_versions
migration_files_versions = self.database_versions[:] #copy

Expand All @@ -273,7 +273,7 @@ def test_it_should_get_all_migration_files_that_must_be_executed_considering_dat

mox.VerifyAll()

def test_it_should_show_an_error_message_if_tries_to_migrate_down_and_migration_file_does_not_exists(self):
def __test_it_should_show_an_error_message_if_tries_to_migrate_down_and_migration_file_does_not_exists(self):
database_versions = self.database_versions
migration_files_versions = [] #empty

Expand Down

0 comments on commit 336441d

Please sign in to comment.