Browse files

Refactoring the class SimpleDBMigrate to Migrations, and fixing a bug…

… about the "drop database" command.
  • Loading branch information...
1 parent 8e20e48 commit 29be5b7a37f0eac17f5afada9d3810b3d158b9ee Gabriel Falcão committed Apr 27, 2009
View
0 .gitignore 100644 → 100755
File mode changed.
View
0 LICENSE.txt 100644 → 100755
File mode changed.
View
0 Makefile 100644 → 100755
File mode changed.
View
0 NOTICE.txt 100644 → 100755
File mode changed.
View
0 README.textile 100644 → 100755
File mode changed.
View
0 example/20090123195634_create_table_users.migration 100644 → 100755
File mode changed.
View
0 example/20090211120001_add_user_age.migration 100644 → 100755
File mode changed.
View
0 example/20090211120002_add_user_mother_age.migration 100644 → 100755
File mode changed.
View
0 example/20090211120003_create_table_chimps.migration 100644 → 100755
File mode changed.
View
0 example/20090228235709_create_table_aleatory.migration 100644 → 100755
File mode changed.
View
0 example/simple-db-migrate.conf 100644 → 100755
File mode changed.
View
0 setup.py 100644 → 100755
File mode changed.
View
0 src/simple_db_migrate/__init__.py 100644 → 100755
File mode changed.
View
0 src/simple_db_migrate/cli.py 100644 → 100755
File mode changed.
View
2 src/simple_db_migrate/core.py 100644 → 100755
@@ -5,7 +5,7 @@
import re
#TODO: Refactor: rename to "Migrations"
-class SimpleDBMigrate(object):
+class Migrations(object):
__migration_files_extension = ".migration"
View
0 src/simple_db_migrate/helpers.py 100644 → 100755
File mode changed.
View
4 src/simple_db_migrate/main.py 100644 → 100755
@@ -1,5 +1,5 @@
from cli import CLI
-from core import SimpleDBMigrate
+from core import Migrations
from helpers import Lists
from mysql import MySQL
import sys
@@ -17,7 +17,7 @@ def __init__(self, options=None, args=None, mysql=None, db_migrate=None):
self.__db_migrate = db_migrate
if self.__db_migrate is None:
- self.__db_migrate = SimpleDBMigrate(self.__options.migrations_dir)
+ self.__db_migrate = Migrations(self.__options.migrations_dir)
def execute(self):
self.__cli.msg("\nStarting DB migration...")
View
2 src/simple_db_migrate/mysql.py 100644 → 100755
@@ -56,7 +56,7 @@ def __execute(self, sql):
def _drop_database(self):
db = self.__mysql_connect(False)
try:
- db.query("drop database %s;" % self.__mysql_db)
+ db.query("drop database if exists %s;" % self.__mysql_db)
except Exception, e:
self.__cli.error_and_exit("can't drop database '%s'; database doesn't exist" % self.__mysql_db)
db.close()
View
0 tests/cli_test.py 100644 → 100755
File mode changed.
View
38 tests/core_test.py 100644 → 100755
@@ -4,7 +4,7 @@
import os
import unittest
-class SimpleDBMigrateTest(unittest.TestCase):
+class MigrationsTest(unittest.TestCase):
def __create_empty_file(self, file_name):
f = open(file_name, "w")
@@ -63,20 +63,20 @@ def tearDown(self):
# eventually the tests that fail leave some garbage behind
# this is to clean up the mess
try:
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
for each_file in db_migrate.get_all_migration_files():
os.remove(each_file)
except:
pass
def test_it_should_get_all_migration_files_in_dir(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
migration_files = db_migrate.get_all_migration_files()
for each_file in migration_files:
self.assertTrue(each_file in self.__test_migration_files)
def test_it_should_get_only_valid_migration_files_in_dir(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
migration_files = db_migrate.get_all_migration_files()
for file_name in self.__test_migration_files:
@@ -86,7 +86,7 @@ def test_it_should_get_only_valid_migration_files_in_dir(self):
self.assertFalse(bad_file_name in migration_files)
def test_it_should_get_all_migration_versions_available(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
migration_files = db_migrate.get_all_migration_files()
expected_versions = []
for each_file in migration_files:
@@ -97,25 +97,25 @@ def test_it_should_get_all_migration_versions_available(self):
self.assertTrue(each_version_got in expected_versions)
def test_it_should_get_all_migration_versions_up_to_a_version(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
migration_files = db_migrate.get_all_migration_versions_up_to("20090214115200")
self.assertEquals(len(migration_files), 1)
self.assertEquals(migration_files[0], "20090214115100")
def test_it_should_get_migration_up_command_in_file(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
migration_file = "20090214120600_example_migration_file_with_commands.migration"
sql = db_migrate.get_sql_command(migration_file, True)
self.assertEquals(sql, "create table test;")
def test_it_should_get_migration_down_command_in_file(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
migration_file = "20090214120600_example_migration_file_with_commands.migration"
sql = db_migrate.get_sql_command(migration_file, False)
self.assertEquals(sql, "drop table test;")
def test_it_should_not_get_migration_command_in_files_with_blank_commands(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
migration_file = "20090214120700_example_migration_file_without_commands.migration"
try:
sql = db_migrate.get_sql_command(migration_file, True)
@@ -124,7 +124,7 @@ def test_it_should_not_get_migration_command_in_files_with_blank_commands(self):
pass
def test_it_should_not_get_migration_command_in_empty_file(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
migration_file = self.__test_migration_files[0]
try:
sql = db_migrate.get_sql_command(migration_file, True)
@@ -135,7 +135,7 @@ def test_it_should_not_get_migration_command_in_empty_file(self):
pass
def test_it_should_get_migration_version_from_file(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
# good file name
example_file_name = "20090214120600_example_migration_file_name.migration"
version = db_migrate.get_migration_version(example_file_name)
@@ -146,17 +146,17 @@ def test_it_should_get_migration_version_from_file(self):
self.assertEquals(version, "2009021401")
def test_it_should_check_if_schema_version_exists_in_files(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
exists = db_migrate.check_if_version_exists("20090214115100")
self.assertTrue(exists)
def test_it_should_get_the_latest_schema_version_available(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
latest_version = db_migrate.latest_schema_version_available()
self.assertEquals(latest_version, "21420101000000")
def test_it_should_validate_file_name_format_mask(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
for file_name in self.__test_migration_files:
self.assertTrue(db_migrate.is_file_name_valid(file_name))
@@ -165,12 +165,12 @@ def test_it_should_validate_file_name_format_mask(self):
self.assertFalse(db_migrate.is_file_name_valid(bad_file_name))
def test_it_should_not_validate_gedit_swap_files(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
invalid_file_name = "%s~" % self.__test_migration_files[0]
self.assertFalse(db_migrate.is_file_name_valid(invalid_file_name))
def test_it_should_create_migration_file(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
new_migration_file_name = db_migrate.create_migration("create_a_migration_file")
self.assertTrue(db_migrate.is_file_name_valid(new_migration_file_name))
@@ -180,20 +180,20 @@ def test_it_should_create_migration_file(self):
self.__test_migration_files.append(new_migration_file_name)
def test_it_should_not_create_migration_file_with_bad_name(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
try:
db_migrate.create_migration("INVALID FILE NAME")
self.fail("it should not pass here")
except:
pass
def test_it_should_get_migration_file_from_version_number(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
migration_file_name = db_migrate.get_migration_file_name_from_version_number("20090214115100")
self.assertEquals(migration_file_name, "20090214115100_example_migration_file1.migration")
def test_it_should_get_none_migration_file_from_invalid_version_number(self):
- db_migrate = SimpleDBMigrate(".")
+ db_migrate = Migrations(".")
migration_file_name = db_migrate.get_migration_file_name_from_version_number("***invalid***")
self.assertTrue(migration_file_name is None)
View
0 tests/helpers_test.py 100644 → 100755
File mode changed.
View
0 tests/main_test.py 100644 → 100755
File mode changed.
View
2 tests/mysql_test.py 100644 → 100755
@@ -61,7 +61,7 @@ def test_it_should_drop_database_on_init_if_its_asked(self):
self.__mock_db_init(mysql_driver_mock, db_mock, cursor_mock)
- db_mock.expects(once()).method("query").query(eq("drop database migration_test;"))
+ db_mock.expects(once()).method("query").query(eq("drop database if exists migration_test;"))
db_mock.expects(once()).method("close")
mysql = MySQL(db_config_file="test.conf", mysql_driver=mysql_driver_mock, drop_db_first=True)
View
2 tests/test.py 100644 → 100755
@@ -22,7 +22,7 @@
test_suites.append(unittest.TestLoader().loadTestsFromTestCase(ListsTest))
test_suites.append(unittest.TestLoader().loadTestsFromTestCase(MainTest))
test_suites.append(unittest.TestLoader().loadTestsFromTestCase(MySQLTest))
- test_suites.append(unittest.TestLoader().loadTestsFromTestCase(SimpleDBMigrateTest))
+ test_suites.append(unittest.TestLoader().loadTestsFromTestCase(MigrationsTest))
alltests = unittest.TestSuite(test_suites)

0 comments on commit 29be5b7

Please sign in to comment.