<s> from . cli import CLI <EOL> from . log import LOG <EOL> from . core import Migration , SimpleDBMigrate <EOL> from . helpers import Lists <EOL> from . config import Config <EOL> """<STR_LIT>""" <EOL> class Main ( object ) : <EOL> def __init__ ( self , config , sgdb = None ) : <EOL> Main . _check_configuration ( config ) <EOL> self . cli = CLI ( ) <EOL> self . config = config <EOL> self . log = LOG ( self . config . get ( "<STR_LIT>" , None ) ) <EOL> self . sgdb = sgdb <EOL> if self . sgdb is None and not self . config . get ( "<STR_LIT>" , None ) : <EOL> if self . config . get ( "<STR_LIT>" ) == '<STR_LIT>' : <EOL> from mysql import MySQL <EOL> self . sgdb = MySQL ( config ) <EOL> elif self . config . get ( "<STR_LIT>" ) == '<STR_LIT>' : <EOL> from oracle import Oracle <EOL> self . sgdb = Oracle ( config ) <EOL> elif self . config . get ( "<STR_LIT>" ) == '<STR_LIT>' : <EOL> from mssql import MSSQL <EOL> self . sgdb = MSSQL ( config ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % self . config . get ( "<STR_LIT>" ) ) <EOL> self . db_migrate = SimpleDBMigrate ( self . config ) <EOL> def execute ( self ) : <EOL> self . _execution_log ( '<STR_LIT>' % ( self . config . get ( '<STR_LIT>' ) , self . config . get ( '<STR_LIT>' ) , self . config . get ( '<STR_LIT>' ) ) , "<STR_LIT>" , log_level_limit = <NUM_LIT:1> ) <EOL> if self . config . get ( "<STR_LIT>" , None ) : <EOL> self . _create_migration ( ) <EOL> else : <EOL> self . _migrate ( ) <EOL> self . _execution_log ( "<STR_LIT>" , "<STR_LIT>" , log_level_limit = <NUM_LIT:1> ) <EOL> def last_label ( self ) : <EOL> labels = self . labels ( ) <EOL> return labels and labels [ - <NUM_LIT:1> ] or None <EOL> def labels ( self ) : <EOL> labels = [ ] <EOL> migrations = self . sgdb . get_all_schema_migrations ( ) <EOL> for migration in migrations : <EOL> if migration . label and ( migration . label not in labels ) : <EOL> labels . append ( migration . label ) <EOL> return labels <EOL> @ staticmethod <EOL> def _check_configuration ( config ) : <EOL> if not isinstance ( config , Config ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> required_configs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if config . get ( "<STR_LIT>" , None ) : <EOL> required_configs = [ '<STR_LIT>' ] <EOL> for key in required_configs : <EOL> config . get ( key ) <EOL> def _create_migration ( self ) : <EOL> migrations_dir = self . config . get ( "<STR_LIT>" ) <EOL> new_file = Migration . create ( self . config . get ( "<STR_LIT>" , None ) , migrations_dir [ <NUM_LIT:0> ] , self . config . get ( "<STR_LIT>" , "<STR_LIT:utf-8>" ) , self . config . get ( "<STR_LIT>" , False ) ) <EOL> self . _execution_log ( "<STR_LIT>" % ( new_file ) , log_level_limit = <NUM_LIT:1> ) <EOL> def _migrate ( self ) : <EOL> destination_version = self . _get_destination_version ( ) <EOL> current_version = self . sgdb . get_current_schema_version ( ) <EOL> self . _execute_migrations ( current_version , destination_version ) <EOL> def _get_destination_version ( self ) : <EOL> label_version = self . config . get ( "<STR_LIT>" , None ) <EOL> schema_version = self . config . get ( "<STR_LIT>" , None ) <EOL> destination_version = None <EOL> destination_version_by_label = None <EOL> destination_version_by_schema = None <EOL> if label_version is not None : <EOL> destination_version_by_label = self . sgdb . get_version_number_from_label ( label_version ) <EOL> """<STR_LIT>""" <EOL> if destination_version_by_label is not None and schema_version is None : <EOL> schema_version = destination_version_by_label <EOL> self . config . update ( "<STR_LIT>" , destination_version_by_label ) <EOL> if schema_version is not None and self . sgdb . get_version_id_from_version_number ( schema_version ) : <EOL> destination_version_by_schema = schema_version <EOL> if label_version is None : <EOL> if schema_version is None : <EOL> destination_version = self . db_migrate . latest_version_available ( ) <EOL> elif destination_version_by_schema is None : <EOL> destination_version = schema_version <EOL> else : <EOL> destination_version = destination_version_by_schema <EOL> else : <EOL> if schema_version is None : <EOL> destination_version = self . db_migrate . latest_version_available ( ) <EOL> elif ( destination_version_by_label is None ) or ( destination_version_by_schema == destination_version_by_label ) : <EOL> destination_version = schema_version <EOL> if ( destination_version_by_schema is not None ) and ( destination_version_by_label is not None ) and ( destination_version_by_schema != destination_version_by_label ) : <EOL> raise Exception ( "<STR_LIT>" % ( label_version , schema_version ) ) <EOL> if ( schema_version is not None and label_version is not None ) and ( ( destination_version_by_schema is not None and destination_version_by_label is None ) or ( destination_version_by_schema is None and destination_version_by_label is not None ) ) : <EOL> raise Exception ( "<STR_LIT>" % ( label_version , schema_version ) ) <EOL> if destination_version is not '<STR_LIT:0>' and not ( self . db_migrate . check_if_version_exists ( destination_version ) or self . sgdb . get_version_id_from_version_number ( destination_version ) ) : <EOL> raise Exception ( "<STR_LIT>" % destination_version ) <EOL> return destination_version <EOL> def _get_migration_files_to_be_executed ( self , current_version , destination_version , is_migration_up ) : <EOL> if current_version == destination_version and not self . config . get ( "<STR_LIT>" , False ) : <EOL> return [ ] <EOL> schema_migrations = self . sgdb . get_all_schema_migrations ( ) <EOL> if is_migration_up : <EOL> available_migrations = self . db_migrate . get_all_migrations ( ) <EOL> remaining_migrations = Lists . subtract ( available_migrations , schema_migrations ) <EOL> remaining_migrations_to_execute = [ migration for migration in remaining_migrations if migration . version <= destination_version ] <EOL> return remaining_migrations_to_execute <EOL> destination_version_id = self . sgdb . get_version_id_from_version_number ( destination_version ) <EOL> try : <EOL> migration_versions = self . db_migrate . get_all_migration_versions ( ) <EOL> except : <EOL> migration_versions = [ ] <EOL> down_migrations_to_execute = [ migration for migration in schema_migrations if migration . id > destination_version_id ] <EOL> force_files = self . config . get ( "<STR_LIT>" , False ) <EOL> for migration in down_migrations_to_execute : <EOL> if not migration . sql_down or force_files : <EOL> if migration . version not in migration_versions : <EOL> raise Exception ( "<STR_LIT>" % migration . version ) <EOL> migration_tmp = self . db_migrate . get_migration_from_version_number ( migration . version ) <EOL> migration . sql_up = migration_tmp . sql_up <EOL> migration . sql_down = migration_tmp . sql_down <EOL> migration . file_name = migration_tmp . file_name <EOL> down_migrations_to_execute . reverse ( ) <EOL> return down_migrations_to_execute <EOL> def _execute_migrations ( self , current_version , destination_version ) : <EOL> """<STR_LIT>""" <EOL> is_migration_up = True <EOL> if self . config . get ( "<STR_LIT>" ) : <EOL> destination_version_id = self . sgdb . get_version_id_from_version_number ( destination_version ) <EOL> if destination_version_id : <EOL> current_version_id = self . sgdb . get_version_id_from_version_number ( current_version ) <EOL> if current_version_id > destination_version_id : <EOL> is_migration_up = False <EOL> elif current_version > destination_version : <EOL> raise Exception ( "<STR_LIT>" % destination_version ) <EOL> migrations_to_be_executed = self . _get_migration_files_to_be_executed ( current_version , destination_version , is_migration_up ) <EOL> self . _execution_log ( "<STR_LIT>" % current_version , "<STR_LIT>" , log_level_limit = <NUM_LIT:1> ) <EOL> if migrations_to_be_executed is None or len ( migrations_to_be_executed ) == <NUM_LIT:0> : <EOL> self . _execution_log ( "<STR_LIT>" % current_version , "<STR_LIT>" , log_level_limit = <NUM_LIT:1> ) <EOL> self . _execution_log ( "<STR_LIT>" , "<STR_LIT>" , log_level_limit = <NUM_LIT:1> ) <EOL> return <EOL> self . _execution_log ( "<STR_LIT>" % ( is_migration_up and migrations_to_be_executed [ - <NUM_LIT:1> ] . version or destination_version ) , "<STR_LIT>" , log_level_limit = <NUM_LIT:1> ) <EOL> up_down_label = is_migration_up and "<STR_LIT>" or "<STR_LIT>" <EOL> if self . config . get ( "<STR_LIT>" , False ) : <EOL> self . _execution_log ( "<STR_LIT>" , "<STR_LIT>" , log_level_limit = <NUM_LIT:1> ) <EOL> else : <EOL> self . _execution_log ( "<STR_LIT>" % up_down_label , log_level_limit = <NUM_LIT:1> ) <EOL> self . _execution_log ( "<STR_LIT>" % ( [ migration . version for migration in migrations_to_be_executed ] ) , "<STR_LIT>" , log_level_limit = <NUM_LIT:1> ) <EOL> sql_statements_executed = [ ] <EOL> for migration in migrations_to_be_executed : <EOL> sql = is_migration_up and migration . sql_up or migration . sql_down <EOL> if not self . config . get ( "<STR_LIT>" , False ) : <EOL> self . _execution_log ( "<STR_LIT>" % ( migration . file_name , up_down_label ) , log_level_limit = <NUM_LIT:1> ) <EOL> label = None <EOL> if is_migration_up : <EOL> label = self . config . get ( "<STR_LIT>" , None ) <EOL> try : <EOL> self . sgdb . change ( sql , migration . version , migration . file_name , migration . sql_up , migration . sql_down , is_migration_up , self . _execution_log , label ) <EOL> except Exception as e : <EOL> self . _execution_log ( "<STR_LIT>" % ( migration . abspath , up_down_label ) , log_level_limit = <NUM_LIT:1> ) <EOL> raise e <EOL> if self . config . get ( "<STR_LIT>" , False ) : <EOL> raw_input ( "<STR_LIT>" ) <EOL> sql_statements_executed . append ( sql ) <EOL> if self . config . get ( "<STR_LIT>" , False ) or self . config . get ( "<STR_LIT>" , False ) : <EOL> self . _execution_log ( "<STR_LIT>" , "<STR_LIT>" , log_level_limit = <NUM_LIT:1> ) <EOL> for sql in sql_statements_executed : <EOL> self . _execution_log ( sql , "<STR_LIT>" , log_level_limit = <NUM_LIT:1> ) <EOL> self . _execution_log ( "<STR_LIT>" , "<STR_LIT>" , log_level_limit = <NUM_LIT:1> ) <EOL> def _execution_log ( self , msg , color = "<STR_LIT>" , log_level_limit = <NUM_LIT:2> ) : <EOL> if self . config . get ( "<STR_LIT>" , <NUM_LIT:1> ) >= log_level_limit : <EOL> CLI . msg ( msg , color ) <EOL> self . log . debug ( msg ) </s>
