Skip to content

Commit

Permalink
#35: Checking if a file is empty or does not define SQL_UP or SQL_DOWN.
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermechapiewski committed Mar 19, 2009
1 parent 4d1564c commit 770f84b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/simple_db_migrate/core.py
Expand Up @@ -29,14 +29,26 @@ def get_all_migration_files(self):
return files

def get_sql_command(self, sql_file, migration_up=True):
f = open(self.__migrations_dir + "/" + sql_file, "r")
exec(f.read())
f.close()

if migration_up:
return SQL_UP
try:
f = open(self.__migrations_dir + "/" + sql_file, "r")
exec(f.read())
except IOError:
self.__cli.error_and_exit("%s: file not found" % self.__migrations_dir + "/" + sql_file)
else:
return SQL_DOWN
f.close()

try:
(SQL_UP, SQL_DOWN)
except NameError:
self.__cli.error_and_exit("migration file is incorrect; it does not define 'SQL_UP' or 'SQL_DOWN' ('%s')" % sql_file)

sql = ""
sql = SQL_UP if migration_up else SQL_DOWN

if sql is None or sql == "":
self.__cli.error_and_exit("migration command is empty ('%s')" % sql_file)

return sql

def get_all_migration_versions(self):
versions = []
Expand Down
28 changes: 28 additions & 0 deletions tests/core_test.py
Expand Up @@ -30,6 +30,14 @@ def setUp(self):
f.close()
self.__test_migration_files.append(file_with_commands)

# migration file without commands
file_without_commands = "20090214120700_example_migration_file_without_commands.migration"
f = open(file_without_commands, "w")
f.write("SQL_UP = ''\n")
f.write("SQL_DOWN = ''\n")
f.close()
self.__test_migration_files.append(file_without_commands)

# very very last schema version available
file_in_the_future = "21420101000000_example_migration_file.migration"
self.__create_empty_file(file_in_the_future)
Expand Down Expand Up @@ -106,6 +114,26 @@ def test_it_should_get_migration_down_command_in_file(self):
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(".")
migration_file = "20090214120700_example_migration_file_without_commands.migration"
try:
sql = db_migrate.get_sql_command(migration_file, True)
self.fail("it should not pass here")
except:
pass

def test_it_should_not_get_migration_command_in_empty_file(self):
db_migrate = SimpleDBMigrate(".")
migration_file = self.__test_migration_files[0]
try:
sql = db_migrate.get_sql_command(migration_file, True)
self.fail("it should not pass here")
except NameError:
self.fail("it should not pass here")
except:
pass

def test_it_should_get_migration_version_from_file(self):
db_migrate = SimpleDBMigrate(".")
# good file name
Expand Down

0 comments on commit 770f84b

Please sign in to comment.