class ActiveRecord::MigrationContext

Migration Context

MigrationContext sets the context in which a migration is run.

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

Attributes

internal_metadata[R]
migrations_paths[R]
schema_migration[R]

Public Class Methods

new(migrations_paths, schema_migration = nil, internal_metadata = nil) click to toggle source
# File lib/active_record/migration.rb, line 1214
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

Public Instance Methods

migrate(target_version = nil, &block) click to toggle source

Runs the migrations in the migrations_path.

If target_version is nil, migrate will run up.

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

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

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

# File lib/active_record/migration.rb, line 1233
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

Private Instance Methods

connection() click to toggle source
# File lib/active_record/migration.rb, line 1360
def connection
  ActiveRecord::Tasks::DatabaseTasks.migration_connection
end
connection_pool() click to toggle source
# File lib/active_record/migration.rb, line 1364
def connection_pool
  ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool
end
migration_files() click to toggle source
# File lib/active_record/migration.rb, line 1368
def migration_files
  paths = Array(migrations_paths)
  Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }]
end
move(direction, steps) click to toggle source
# File lib/active_record/migration.rb, line 1385
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
parse_migration_filename(filename) click to toggle source
# File lib/active_record/migration.rb, line 1373
def parse_migration_filename(filename)
  File.basename(filename).scan(Migration::MigrationFilenameRegexp).first
end
valid_migration_timestamp?(version) click to toggle source
# File lib/active_record/migration.rb, line 1381
def valid_migration_timestamp?(version)
  version.to_i < (Time.now.utc + 1.day).strftime("%Y%m%d%H%M%S").to_i
end
validate_timestamp?() click to toggle source
# File lib/active_record/migration.rb, line 1377
def validate_timestamp?
  ActiveRecord.timestamped_migrations && ActiveRecord.validate_migration_timestamps
end