класс ActiveRecord::MigrationContext
Контекст миграции
MigrationContext задаёт контекст, в котором выполняется миграция.
Для контекста миграции требуется указать путь к миграциям в параметре migrations_paths. Дополнительно можно указать класс schema_migration. Несколько баз данных будут инициализировать объект SchemaMigration для каждой базы данных. Из задач Rake Rails позаботятся об этом за вас.
Атрибуты
Открытые методы класса
# File activerecord/lib/active_record/migration.rb, line 1204
def initialize(migrations_paths, schema_migration = nil, internal_metadata = 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 internal_metadata == 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
internal_metadata = nil
end
@migrations_paths = migrations_paths
@schema_migration = schema_migration || SchemaMigration.new(connection)
@internal_metadata = internal_metadata || InternalMetadata.new(connection)
end Открытые методы экземпляра
# File activerecord/lib/active_record/migration.rb, line 1245
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 Выполняет миграции в migrations_path.
Если target_version равно nil, migrate выполнит up.
Если current_version и target_version оба равны 0, будет возвращён пустой массив, и миграции не будут выполнены.
Если current_version в схеме больше, чем target_version, то будет выполнено down.
Если ни одно из условий не выполняется, будет выполнено up с target_version.
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.