Permalink
Browse files

refactoring Main tests

  • Loading branch information...
1 parent cef44b7 commit 02388b09e19ecbb0fa4cad037e8e2060bfc894fc @wandenberg wandenberg committed Mar 6, 2012
Showing with 712 additions and 1,071 deletions.
  1. +42 −40 simple_db_migrate/main.py
  2. +670 −1,031 tests/main_test.py
@@ -2,12 +2,16 @@
from log import LOG
from core import Migration, SimpleDBMigrate
from helpers import Lists
+from config import Config
class Main(object):
- def __init__(self, config=None, sgdb=None, db_migrate=None, execution_log=None):
+ def __init__(self, config, sgdb=None):
+ if not isinstance(config, Config):
+ raise Exception("config must be an instance of simple_db_migrate.config.Config")
+
self.cli = CLI()
- self.config = config or {}
+ self.config = config
self.log = LOG(self.config.get("log_dir", None))
self.sgdb = sgdb
@@ -18,37 +22,32 @@ def __init__(self, config=None, sgdb=None, db_migrate=None, execution_log=None):
elif self.config.get("db_engine") is 'oracle':
from oracle import Oracle
self.sgdb = Oracle(config)
+ else:
+ raise Exception("engine not supported '%s'" % self.config.get("db_engine"))
- self.db_migrate = db_migrate or SimpleDBMigrate(config)
- if execution_log:
- self.execution_log = execution_log
-
- def execution_log(self, msg, color="CYAN", log_level_limit=2):
- if self.config.get("log_level", 1) >= log_level_limit:
- self.cli.msg(msg)
- self.log.debug(msg)
+ self.db_migrate = SimpleDBMigrate(self.config)
def execute(self):
- self.execution_log("\nStarting DB migration...", "PINK", log_level_limit=1)
+ self._execution_log("\nStarting DB migration...", "PINK", log_level_limit=1)
if self.config.get("new_migration", None):
- self.create_migration()
+ self._create_migration()
else:
- self.migrate()
- self.execution_log("\nDone.\n", "PINK", log_level_limit=1)
+ self._migrate()
+ self._execution_log("\nDone.\n", "PINK", log_level_limit=1)
- def create_migration(self):
+ def _create_migration(self):
migrations_dir = self.config.get("migrations_dir")
new_file = Migration.create(self.config.get("new_migration", None), migrations_dir[0], self.config.get("db_script_encoding", "utf-8"), self.config.get("utc_timestamp", False))
- self.execution_log("- Created file '%s'" % (new_file), log_level_limit=1)
+ self._execution_log("- Created file '%s'" % (new_file), log_level_limit=1)
- def migrate(self):
- destination_version = self.get_destination_version()
+ def _migrate(self):
+ destination_version = self._get_destination_version()
current_version = self.sgdb.get_current_schema_version()
# do it!
- self.execute_migrations(current_version, destination_version)
+ self._execute_migrations(current_version, destination_version)
- def get_destination_version(self):
+ def _get_destination_version(self):
label_version = self.config.get("label_version", None)
schema_version = self.config.get("schema_version", None)
@@ -64,9 +63,7 @@ def get_destination_version(self):
"""
if destination_version_by_label is not None and schema_version is None:
schema_version = destination_version_by_label
- self.config.remove("schema_version")
- self.config.put("schema_version", destination_version_by_label)
-
+ self.config.update("schema_version", destination_version_by_label)
if schema_version is not None and self.sgdb.get_version_id_from_version_number(schema_version):
destination_version_by_schema = schema_version
@@ -95,7 +92,7 @@ def get_destination_version(self):
return destination_version
- def get_migration_files_to_be_executed(self, current_version, destination_version, is_migration_up):
+ def _get_migration_files_to_be_executed(self, current_version, destination_version, is_migration_up):
if current_version == destination_version and not self.config.get("force_execute_old_migrations_versions", False):
return []
schema_versions = self.sgdb.get_all_schema_versions()
@@ -124,10 +121,10 @@ def get_migration_files_to_be_executed(self, current_version, destination_versio
down_migrations_to_execute.reverse()
return down_migrations_to_execute
- def execute_migrations(self, current_version, destination_version):
+ def _execute_migrations(self, current_version, destination_version):
"""
passed a version:
- this version don't exists in the database and is younger than the last version -> do migrations up until the this version
+ this version don't exists in the database and is younger than the last version -> do migrations up until this version
this version don't exists in the database and is older than the last version -> do nothing, is a unpredictable behavior
this version exists in the database and is older than the last version -> do migrations down until this version
@@ -150,40 +147,40 @@ def execute_migrations(self, current_version, destination_version):
raise Exception("Trying to migrate to a lower version wich is not found on database (%s)" % destination_version)
# getting only the migration sql files to be executed
- migrations_to_be_executed = self.get_migration_files_to_be_executed(current_version, destination_version, is_migration_up)
+ migrations_to_be_executed = self._get_migration_files_to_be_executed(current_version, destination_version, is_migration_up)
- self.execution_log("- Current version is: %s" % current_version, "GREEN", log_level_limit=1)
+ self._execution_log("- Current version is: %s" % current_version, "GREEN", log_level_limit=1)
if migrations_to_be_executed is None or len(migrations_to_be_executed) == 0:
- self.execution_log("- Destination version is: %s" % current_version, "GREEN", log_level_limit=1)
- self.execution_log("\nNothing to do.\n", "PINK", log_level_limit=1)
+ self._execution_log("- Destination version is: %s" % current_version, "GREEN", log_level_limit=1)
+ self._execution_log("\nNothing to do.\n", "PINK", log_level_limit=1)
return
- self.execution_log("- Destination version is: %s" % (is_migration_up and migrations_to_be_executed[-1].version or destination_version), "GREEN", log_level_limit=1)
+ self._execution_log("- Destination version is: %s" % (is_migration_up and migrations_to_be_executed[-1].version or destination_version), "GREEN", log_level_limit=1)
up_down_label = is_migration_up and "up" or "down"
if self.config.get("show_sql_only", False):
- self.execution_log("\nWARNING: database migrations are not being executed ('--showsqlonly' activated)", "YELLOW", log_level_limit=1)
+ self._execution_log("\nWARNING: database migrations are not being executed ('--showsqlonly' activated)", "YELLOW", log_level_limit=1)
else:
- self.execution_log("\nStarting migration %s!" % up_down_label, log_level_limit=1)
+ self._execution_log("\nStarting migration %s!" % up_down_label, log_level_limit=1)
- self.execution_log("*** versions: %s\n" % ([ migration.version for migration in migrations_to_be_executed]), "CYAN", log_level_limit=1)
+ self._execution_log("*** versions: %s\n" % ([ migration.version for migration in migrations_to_be_executed]), "CYAN", log_level_limit=1)
sql_statements_executed = []
for migration in migrations_to_be_executed:
sql = is_migration_up and migration.sql_up or migration.sql_down
if not self.config.get("show_sql_only", False):
- self.execution_log("===== executing %s (%s) =====" % (migration.file_name, up_down_label), log_level_limit=1)
+ self._execution_log("===== executing %s (%s) =====" % (migration.file_name, up_down_label), log_level_limit=1)
label = None
if is_migration_up:
label = self.config.get("label_version", None)
try:
- self.sgdb.change(sql, migration.version, migration.file_name, migration.sql_up, migration.sql_down, is_migration_up, self.execution_log, label)
+ self.sgdb.change(sql, migration.version, migration.file_name, migration.sql_up, migration.sql_down, is_migration_up, self._execution_log, label)
except Exception, e:
- self.execution_log("===== ERROR executing %s (%s) =====" % (migration.abspath, up_down_label), log_level_limit=1)
+ self._execution_log("===== ERROR executing %s (%s) =====" % (migration.abspath, up_down_label), log_level_limit=1)
raise e
# paused mode
@@ -194,7 +191,12 @@ def execute_migrations(self, current_version, destination_version):
sql_statements_executed.append(sql)
if self.config.get("show_sql", False) or self.config.get("show_sql_only", False):
- self.execution_log("__________ SQL statements executed __________", "YELLOW", log_level_limit=1)
+ self._execution_log("__________ SQL statements executed __________", "YELLOW", log_level_limit=1)
for sql in sql_statements_executed:
- self.execution_log(sql, "YELLOW", log_level_limit=1)
- self.execution_log("_____________________________________________", "YELLOW", log_level_limit=1)
+ self._execution_log(sql, "YELLOW", log_level_limit=1)
+ self._execution_log("_____________________________________________", "YELLOW", log_level_limit=1)
+
+ def _execution_log(self, msg, color="CYAN", log_level_limit=2):
+ if self.config.get("log_level", 1) >= log_level_limit:
+ self.cli.msg(msg, color)
+ self.log.debug(msg)
Oops, something went wrong.

0 comments on commit 02388b0

Please sign in to comment.