123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::MigrationContext

Relationships & Source Files
Inherits: Object
Defined in: activerecord/lib/active_record/migration.rb

Overview

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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(migrations_paths, schema_migration = nil, internal_metadata = nil) ⇒ MigrationContext

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration.rb', line 1205

def initialize(migrations_paths, schema_migration = nil,  = nil)
  if schema_migration == SchemaMigration
    ActiveRecord.deprecator.warn(<<-MSG.squish)
      SchemaMigration no longer inherits from ActiveRecord::Base. If you want
      to use the default connection, remove this argument. If you want to use a
      specific connection, instantiate MigrationContext with the connection's schema
      migration, for example `MigrationContext.new(path, Dog.connection.schema_migration)`.
    MSG

    schema_migration = nil
  end

  if  == InternalMetadata
    ActiveRecord.deprecator.warn(<<-MSG.squish)
      SchemaMigration no longer inherits from ActiveRecord::Base. If you want
      to use the default connection, remove this argument. If you want to use a
      specific connection, instantiate MigrationContext with the connection's internal
      metadata, for example `MigrationContext.new(path, nil, Dog.connection.internal_metadata)`.
    MSG

     = nil
  end

  @migrations_paths = migrations_paths
  @schema_migration = schema_migration || SchemaMigration.new(connection)
  @internal_metadata =  || InternalMetadata.new(connection)
end

Instance Attribute Details

#internal_metadata (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration.rb', line 1203

attr_reader :migrations_paths, :schema_migration, :

#migrations_paths (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration.rb', line 1203

attr_reader :migrations_paths, :schema_migration, :

#schema_migration (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration.rb', line 1203

attr_reader :migrations_paths, :schema_migration, :

Instance Method Details

#migrate(target_version = nil, &block)

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.

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration.rb', line 1246

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