Skip to content

Commit

Permalink
Refactoring to remove logging class and put thing on CLI.
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermechapiewski committed Mar 12, 2009
1 parent 7fa2ad1 commit 7f9439b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
12 changes: 12 additions & 0 deletions src/simple_db_migrate/cli.py
@@ -1,4 +1,5 @@
from optparse import OptionParser
import sys

class CLI(object):

Expand Down Expand Up @@ -39,3 +40,14 @@ def get_parser(self):

def parse(self):
return self.__parser.parse_args()

def error_and_exit(self, msg):
print "[ERROR] %s\n" % (msg)
sys.exit(1)

def info_and_exit(self, msg):
print "%s\n" % (msg)
sys.exit(0)

def msg(self, msg):
print msg
9 changes: 5 additions & 4 deletions src/simple_db_migrate/core.py
@@ -1,4 +1,4 @@
from logging import *
from cli import CLI
from time import strftime
import os
import shutil
Expand All @@ -11,6 +11,7 @@ class SimpleDBMigrate(object):

def __init__(self, migrations_dir):
self.__migrations_dir = migrations_dir
self.__cli = CLI()

def get_all_migration_files(self):
dir_list = os.listdir(self.__migrations_dir)
Expand All @@ -21,7 +22,7 @@ def get_all_migration_files(self):
files.append(dir_file)

if len(files) == 0:
Log().error_and_exit("no migration files found")
self.__cli.error_and_exit("no migration files found")

files.sort()

Expand Down Expand Up @@ -76,7 +77,7 @@ def create_migration(self, migration_name):
file_name = "%s_%s%s" % (timestamp, migration_name, self.__migration_files_extension)

if not self.is_file_name_valid(file_name):
Log().error_and_exit("invalid migration name; it should contain only letters, numbers and/or underscores ('%s')" % migration_name)
self.__cli.error_and_exit("invalid migration name; it should contain only letters, numbers and/or underscores ('%s')" % migration_name)

new_file = "%s/%s" % (self.__migrations_dir, file_name)

Expand All @@ -85,7 +86,7 @@ def create_migration(self, migration_name):
f.write(MigrationFile.template)
f.close()
except IOError:
Log().error_and_exit("could not create file ('%s')" % new_file)
self.__cli.error_and_exit("could not create file ('%s')" % new_file)

return file_name

Expand Down
10 changes: 0 additions & 10 deletions src/simple_db_migrate/logging.py

This file was deleted.

31 changes: 16 additions & 15 deletions src/simple_db_migrate/main.py
@@ -1,11 +1,12 @@
from cli import CLI
from core import SimpleDBMigrate
from helpers import Lists
from mysql import MySQL
from logging import Log
import sys

class Main(object):
def __init__(self, options=None, args=None, mysql=None, db_migrate=None):
self.__cli = CLI()
self.__options = options
self.__args = args

Expand All @@ -18,23 +19,23 @@ def __init__(self, options=None, args=None, mysql=None, db_migrate=None):
self.__db_migrate = SimpleDBMigrate(self.__options.migrations_dir)

def execute(self):
print "\nStarting DB migration..."
self.__cli.msg("\nStarting DB migration...")
if self.__options.create_migration:
self._create_migration()
else:
self._migrate()
print "\nDone.\n"
self.__cli.msg("\nDone.\n")

def _create_migration(self):
new_file = self.__db_migrate.create_migration(self.__options.create_migration)
print "- Created file '%s'" % (new_file)
self.__cli.msg("- Created file '%s'" % (new_file))

def _migrate(self):
destination_version = self._get_destination_version()
current_version = self.__mysql.get_current_schema_version()

print "- Current version is: %s" % current_version
print "- Destination version is: %s" % destination_version
self.__cli.msg("- Current version is: %s" % current_version)
self.__cli.msg("- Destination version is: %s" % destination_version)

# if current and destination versions are the same,
# will consider a migration up to execute remaining files
Expand All @@ -52,7 +53,7 @@ def _get_destination_version(self):
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)
self.__cli.error_and_exit("version not found (%s)" % destination_version)

return destination_version

Expand All @@ -68,7 +69,7 @@ def _get_migration_files_to_be_executed(self, current_version, destination_versi
down_versions = [version for version in mysql_versions if version <= current_version and version > destination_version]
for version in down_versions:
if version not in migration_versions:
Log().error_and_exit("impossible to migrate down: one of the versions was not found (%s)" % version)
self.__cli.error_and_exit("impossible to migrate down: one of the versions was not found (%s)" % version)
down_versions.reverse()
return down_versions

Expand All @@ -77,25 +78,25 @@ def _execute_migrations(self, current_version, destination_version, is_migration
versions_to_be_executed = self._get_migration_files_to_be_executed(current_version, destination_version)

if versions_to_be_executed is None or len(versions_to_be_executed) == 0:
print "\nNothing to do.\n"
self.__cli.msg("\nNothing to do.\n")
sys.exit(0)

up_down_label = "up" if is_migration_up else "down"
print "\nStarting migration %s!" % up_down_label
print "*** will run %s\n" % versions_to_be_executed
self.__cli.msg("\nStarting migration %s!" % up_down_label)
self.__cli.msg("*** will run %s\n" % versions_to_be_executed)

sql_statements_executed = ""
for migration_version in versions_to_be_executed:
sql_file = self.__db_migrate.get_migration_file_name_from_version_number(migration_version)

print "===== executing %s (%s) =====" % (sql_file, up_down_label)
self.__cli.msg("===== executing %s (%s) =====" % (sql_file, up_down_label))
sql = self.__db_migrate.get_sql_command(sql_file, is_migration_up)
self.__mysql.change(sql, migration_version, is_migration_up)

#recording the last statement executed
sql_statements_executed += sql

if self.__options.show_sql:
print "__________ SQL statements executed __________"
print sql_statements_executed
print "_____________________________________________\n"
self.__cli.msg("__________ SQL statements executed __________")
self.__cli.msg(sql_statements_executed)
self.__cli.msg("_____________________________________________\n")
8 changes: 5 additions & 3 deletions src/simple_db_migrate/mysql.py
@@ -1,16 +1,18 @@
from logging import *
from cli import CLI
import MySQLdb
import sys

class MySQL(object):

def __init__(self, db_config_file="simple-db-migrate.conf", mysql_driver=MySQLdb):
self.__cli = CLI()

# read configurations
try:
f = open(db_config_file, "r")
exec(f.read())
except IOError:
Log().error_and_exit("%s: file not found" % db_config_file)
self.__cli.error_and_exit("%s: file not found" % db_config_file)
else:
f.close()

Expand All @@ -30,7 +32,7 @@ def __mysql_connect(self, connect_using_db_name=True):

return self.__mysql_driver.connect(host=self.__mysql_host__, user=self.__mysql_user__, passwd=self.__mysql_passwd__)
except Exception:
Log().error_and_exit("could not connect to database")
self.__cli.error_and_exit("could not connect to database")

def __execute(self, sql):
db = self.__mysql_connect()
Expand Down
16 changes: 16 additions & 0 deletions tests/cli_test.py
Expand Up @@ -23,6 +23,22 @@ def test_it_should_configure_all_options(self):
self.assertTrue(parser.has_option("--showsql"))

self.assertTrue(parser.has_option("--create"))

def test_it_should_show_error_message_and_exit(self):
cli = CLI()
try:
cli.error_and_exit("test message")
self.fail("it should not get here")
except:
pass

def test_it_should_show_info_message_and_exit(self):
cli = CLI()
try:
cli.info_and_exit("test message")
self.fail("it should not get here")
except:
pass

if __name__ == "__main__":
unittest.main()

0 comments on commit 7f9439b

Please sign in to comment.