Skip to content

Commit

Permalink
#15: Refactoring Main.
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermechapiewski committed Mar 11, 2009
1 parent 50a7f57 commit a87137b
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/simple_db_migrate/main.py
Expand Up @@ -20,45 +20,53 @@ def execute(self):
def __create_migration(self):
new_file = self.__db_migrate.create_migration(self.__options.create_migration)
print "- Created file '%s'" % (new_file)

#TODO: too big -- needs refactor

def __migrate(self):
destination_version = self.__options.schema_version

if destination_version == None:
destination_version = self.__db_migrate.latest_schema_version_available()

if not self.__db_migrate.check_if_version_exists(destination_version):
Log().error_and_exit("version not found (%s)" % destination_version)

destination_version = self.__get_destination_version()
current_version = self.__mysql.get_current_schema_version()

if str(current_version) == str(destination_version):
Log().error_and_exit("current and destination versions are the same (%s)" % current_version)

print "- Current version is: %s" % current_version
print "- Destination version is: %s" % destination_version

migration_up = True
migration = "up"
# if current and destination versions are the same,
# will consider a migration up to execute remaining files
is_migration_up = True
if int(current_version) > int(destination_version):
migration_up = False
migration = "down"
is_migration_up = False

print "\nStarting migration %s!\n" % "up" if is_migration_up else "down"

# do it!
self.__execute_migrations(current_version, destination_version, is_migration_up)

def __get_destination_version(self):
destination_version = self.__options.schema_version

print "\nStarting migration %s!\n" % migration
if destination_version == None:
destination_version = self.__db_migrate.latest_schema_version_available()

if not self.__db_migrate.check_if_version_exists(destination_version):
Log().error_and_exit("version not found (%s)" % destination_version)

return destination_version

def __execute_migrations(self, current_version, destination_version, is_migration_up):
# getting only the migration sql files to be executed
migration_files_to_be_executed = self.__db_migrate.get_migration_files_between_versions(current_version, destination_version)

sql_statements_executed = ""
for sql_file in migration_files_to_be_executed:

file_version = self.__db_migrate.get_migration_version(sql_file)
if not migration_up:
if not is_migration_up:
file_version = destination_version

print "===== executing %s (%s) =====" % (sql_file, migration)
sql = self.__db_migrate.get_sql_command(sql_file, migration_up)
self.__mysql.change(sql, file_version, migration_up)
print "===== executing %s (%s) =====" % (sql_file, "up" if is_migration_up else "down")
sql = self.__db_migrate.get_sql_command(sql_file, is_migration_up)
self.__mysql.change(sql, file_version, is_migration_up)

#recording the last statement executed
sql_statements_executed += sql
Expand Down

0 comments on commit a87137b

Please sign in to comment.