class ActiveRecord::MigrationContext

handle this for you.
a SchemaMigration object per database. From the Rake tasks, Rails will
class can be provided. Multiple database applications will instantiate
in the migrations_paths parameter. Optionally a schema_migration
A migration context requires the path to the migrations is set
MigrationContext sets the context in which a migration is run.
= Migration Context

def connection

def connection
  ActiveRecord::Tasks::DatabaseTasks.migration_connection
end

def connection_pool

def connection_pool
  ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
end

def current_environment # :nodoc:

:nodoc:
def current_environment # :nodoc:
  ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
end

def current_version # :nodoc:

:nodoc:
def current_version # :nodoc:
  get_all_versions.max || 0
rescue ActiveRecord::NoDatabaseError
end

def down(target_version = nil, &block) # :nodoc:

:nodoc:
def down(target_version = nil, &block) # :nodoc:
  selected_migrations = if block_given?
    migrations.select(&block)
  else
    migrations
  end
  Migrator.new(:down, selected_migrations, schema_migration, internal_metadata, target_version).migrate
end

def forward(steps = 1) # :nodoc:

:nodoc:
def forward(steps = 1) # :nodoc:
  move(:up, steps)
end

def get_all_versions # :nodoc:

:nodoc:
def get_all_versions # :nodoc:
  if schema_migration.table_exists?
    schema_migration.integer_versions
  else
    []
  end
end

def initialize(migrations_paths, schema_migration = nil, internal_metadata = nil)

def initialize(migrations_paths, schema_migration = nil, internal_metadata = nil)
  @migrations_paths = migrations_paths
  @schema_migration = schema_migration || SchemaMigration.new(connection_pool)
  @internal_metadata = internal_metadata || InternalMetadata.new(connection_pool)
end

def last_stored_environment # :nodoc:

:nodoc:
def last_stored_environment # :nodoc:
  internal_metadata = connection_pool.internal_metadata
  return nil unless internal_metadata.enabled?
  return nil if current_version == 0
  raise NoEnvironmentInSchemaError unless internal_metadata.table_exists?
  environment = internal_metadata[:environment]
  raise NoEnvironmentInSchemaError unless environment
  environment
end

def migrate(target_version = nil, &block)

the +target_version+.
If none of the conditions are met, +up+ will be run with

the +target_version+, then +down+ will be run.
If the +current_version+ in the schema is greater than

will be run.
0 then an empty array will be returned and no migrations
If the +current_version+ and +target_version+ are both

If +target_version+ is +nil+, +migrate+ will run +up+.

Runs the migrations in the +migrations_path+.
def migrate(target_version = nil, &block)
  case
  when target_version.nil?
    up(target_version, &block)
  when current_version == 0 && target_version == 0
    []
  when current_version > target_version
    down(target_version, &block)
  else
    up(target_version, &block)
  end
end

def migration_files

def migration_files
  paths = Array(migrations_paths)
  Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }]
end

def migrations # :nodoc:

:nodoc:
def migrations # :nodoc:
  migrations = migration_files.map do |file|
    version, name, scope = parse_migration_filename(file)
    raise IllegalMigrationNameError.new(file) unless version
    if validate_timestamp? && !valid_migration_timestamp?(version)
      raise InvalidMigrationTimestampError.new(version, name)
    end
    version = version.to_i
    name = name.camelize
    MigrationProxy.new(name, version, file, scope)
  end
  migrations.sort_by(&:version)
end

def migrations_status # :nodoc:

:nodoc:
def migrations_status # :nodoc:
  db_list = schema_migration.normalized_versions
  file_list = migration_files.filter_map do |file|
    version, name, scope = parse_migration_filename(file)
    raise IllegalMigrationNameError.new(file) unless version
    if validate_timestamp? && !valid_migration_timestamp?(version)
      raise InvalidMigrationTimestampError.new(version, name)
    end
    version = schema_migration.normalize_migration_number(version)
    status = db_list.delete(version) ? "up" : "down"
    [status, version, (name + scope).humanize]
  end
  db_list.map! do |version|
    ["up", version, "********** NO FILE **********"]
  end
  (db_list + file_list).sort_by { |_, version, _| version.to_i }
end

def move(direction, steps)

def move(direction, steps)
  migrator = Migrator.new(direction, migrations, schema_migration, internal_metadata)
  if current_version != 0 && !migrator.current_migration
    raise UnknownMigrationVersionError.new(current_version)
  end
  start_index =
    if current_version == 0
      0
    else
      migrator.migrations.index(migrator.current_migration)
    end
  finish = migrator.migrations[start_index + steps]
  version = finish ? finish.version : 0
  public_send(direction, version)
end

def needs_migration? # :nodoc:

:nodoc:
def needs_migration? # :nodoc:
  pending_migration_versions.size > 0
end

def open # :nodoc:

:nodoc:
def open # :nodoc:
  Migrator.new(:up, migrations, schema_migration, internal_metadata)
end

def parse_migration_filename(filename)

def parse_migration_filename(filename)
  File.basename(filename).scan(Migration::MigrationFilenameRegexp).first
end

def pending_migration_versions # :nodoc:

:nodoc:
def pending_migration_versions # :nodoc:
  migrations.collect(&:version) - get_all_versions
end

def protected_environment? # :nodoc:

:nodoc:
def protected_environment? # :nodoc:
  ActiveRecord::Base.protected_environments.include?(last_stored_environment) if last_stored_environment
end

def rollback(steps = 1) # :nodoc:

:nodoc:
def rollback(steps = 1) # :nodoc:
  move(:down, steps)
end

def run(direction, target_version) # :nodoc:

:nodoc:
def run(direction, target_version) # :nodoc:
  Migrator.new(direction, migrations, schema_migration, internal_metadata, target_version).run
end

def up(target_version = nil, &block) # :nodoc:

:nodoc:
def up(target_version = nil, &block) # :nodoc:
  selected_migrations = if block_given?
    migrations.select(&block)
  else
    migrations
  end
  Migrator.new(:up, selected_migrations, schema_migration, internal_metadata, target_version).migrate
end

def valid_migration_timestamp?(version)

def valid_migration_timestamp?(version)
  version.to_i < (Time.now.utc + 1.day).strftime("%Y%m%d%H%M%S").to_i
end

def validate_timestamp?

def validate_timestamp?
  ActiveRecord.timestamped_migrations && ActiveRecord.validate_migration_timestamps
end