1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow to filter migrations by passing a block

Example:
  ActiveRecord::Migrator.migrate(path) do |migration|
    migration.name =~ /User/
  end

The above example will migrate only migrations with User in
the name
This commit is contained in:
Piotr Sarnacki 2011-12-09 20:07:29 +01:00
parent ab30570b69
commit f0b782d060
2 changed files with 31 additions and 9 deletions

View file

@ -528,16 +528,16 @@ module ActiveRecord
attr_writer :migrations_paths
alias :migrations_path= :migrations_paths=
def migrate(migrations_paths, target_version = nil)
def migrate(migrations_paths, target_version = nil, &block)
case
when target_version.nil?
up(migrations_paths, target_version)
up(migrations_paths, target_version, &block)
when current_version == 0 && target_version == 0
[]
when current_version > target_version
down(migrations_paths, target_version)
down(migrations_paths, target_version, &block)
else
up(migrations_paths, target_version)
up(migrations_paths, target_version, &block)
end
end
@ -549,12 +549,12 @@ module ActiveRecord
move(:up, migrations_paths, steps)
end
def up(migrations_paths, target_version = nil)
self.new(:up, migrations_paths, target_version).migrate
def up(migrations_paths, target_version = nil, &block)
self.new(:up, migrations_paths, target_version).migrate(&block)
end
def down(migrations_paths, target_version = nil)
self.new(:down, migrations_paths, target_version).migrate
def down(migrations_paths, target_version = nil, &block)
self.new(:down, migrations_paths, target_version).migrate(&block)
end
def run(direction, migrations_paths, target_version)
@ -657,7 +657,7 @@ module ActiveRecord
end
end
def migrate
def migrate(&block)
current = migrations.detect { |m| m.version == current_version }
target = migrations.detect { |m| m.version == @target_version }
@ -674,6 +674,10 @@ module ActiveRecord
ran = []
runnable.each do |migration|
if block && !block.call(migration)
next
end
Base.logger.info "Migrating to #{migration.name} (#{migration.version})" if Base.logger
seen = migrated.include?(migration.version.to_i)

View file

@ -1235,6 +1235,24 @@ if ActiveRecord::Base.connection.supports_migrations?
assert_raise(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
end
def test_filtering_migrations
assert !Person.column_methods_hash.include?(:last_name)
assert !Reminder.table_exists?
name_filter = lambda { |migration| migration.name == "ValidPeopleHaveLastNames" }
ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid", &name_filter)
Person.reset_column_information
assert Person.column_methods_hash.include?(:last_name)
assert_raise(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/valid", &name_filter)
Person.reset_column_information
assert !Person.column_methods_hash.include?(:last_name)
assert_raise(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
end
class MockMigration < ActiveRecord::Migration
attr_reader :went_up, :went_down
def initialize