Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
construct a migrator with a list of migrations rather than a list of …
…paths
  • Loading branch information
tenderlove committed Jan 13, 2012
1 parent 29fdd8c commit bc276fb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
47 changes: 29 additions & 18 deletions activerecord/lib/active_record/migration.rb
Expand Up @@ -546,14 +546,14 @@ class << self


def migrate(migrations_paths, target_version = nil, &block) def migrate(migrations_paths, target_version = nil, &block)
case case
when target_version.nil? when target_version.nil?
up(migrations_paths, target_version, &block) up(migrations_paths, target_version, &block)
when current_version == 0 && target_version == 0 when current_version == 0 && target_version == 0
[] []
when current_version > target_version when current_version > target_version
down(migrations_paths, target_version, &block) down(migrations_paths, target_version, &block)
else else
up(migrations_paths, target_version, &block) up(migrations_paths, target_version, &block)
end end
end end


Expand All @@ -566,15 +566,19 @@ def forward(migrations_paths, steps=1)
end end


def up(migrations_paths, target_version = nil, &block) def up(migrations_paths, target_version = nil, &block)
self.new(:up, migrations_paths, target_version).migrate(&block) self.new(:up, migrations(migrations_paths), target_version).migrate(&block)
end end


def down(migrations_paths, target_version = nil, &block) def down(migrations_paths, target_version = nil, &block)
self.new(:down, migrations_paths, target_version).migrate(&block) self.new(:down, migrations(migrations_paths), target_version).migrate(&block)
end end


def run(direction, migrations_paths, target_version) def run(direction, migrations_paths, target_version)
self.new(direction, migrations_paths, target_version).run self.new(direction, migrations(migrations_paths), target_version).run
end

def open(migrations_paths)
self.new(:up, migrations(migrations_paths), nil)
end end


def schema_migrations_table_name def schema_migrations_table_name
Expand Down Expand Up @@ -638,7 +642,7 @@ def migrations(paths)
private private


def move(direction, migrations_paths, steps) def move(direction, migrations_paths, steps)
migrator = self.new(direction, migrations_paths) migrator = self.new(direction, migrations(migrations_paths))
start_index = migrator.migrations.index(migrator.current_migration) start_index = migrator.migrations.index(migrator.current_migration)


if start_index if start_index
Expand All @@ -649,10 +653,20 @@ def move(direction, migrations_paths, steps)
end end
end end


def initialize(direction, migrations_paths, target_version = nil) def initialize(direction, migrations, target_version = nil)
raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations? raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations?
Base.connection.initialize_schema_migrations_table Base.connection.initialize_schema_migrations_table
@direction, @migrations_paths, @target_version = direction, migrations_paths, target_version
@direction = direction

if Array(migrations).grep(String).empty?
@migrations = migrations
else
ActiveSupport::Deprecation.warn "instantiate this class with a list of migrations"

This comment has been minimized.

Copy link
@rubys

rubys Dec 12, 2012

Contributor

This comment has been minimized.

Copy link
@jeremy

jeremy Dec 12, 2012

Member

Fixed fe70cde

@migrations = self.class.migrations(migrations)
end

@target_version = target_version
end end


def current_version def current_version
Expand Down Expand Up @@ -721,10 +735,7 @@ def migrate(&block)
end end


def migrations def migrations
@migrations ||= begin down? ? @migrations.reverse : @migrations
migrations = self.class.migrations(@migrations_paths)
down? ? migrations.reverse : migrations
end
end end


def pending_migrations def pending_migrations
Expand Down
18 changes: 14 additions & 4 deletions activerecord/test/cases/migration_test.rb
Expand Up @@ -890,7 +890,8 @@ def test_migrator_one_up_with_exception_and_rollback
end end


def test_finds_migrations def test_finds_migrations
migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/valid").migrations list = ActiveRecord::Migrator.migrations(MIGRATIONS_ROOT + "/valid")
migrations = ActiveRecord::Migrator.new(:up, list).migrations


[[1, 'ValidPeopleHaveLastNames'], [2, 'WeNeedReminders'], [3, 'InnocentJointable']].each_with_index do |pair, i| [[1, 'ValidPeopleHaveLastNames'], [2, 'WeNeedReminders'], [3, 'InnocentJointable']].each_with_index do |pair, i|
assert_equal migrations[i].version, pair.first assert_equal migrations[i].version, pair.first
Expand All @@ -899,7 +900,8 @@ def test_finds_migrations
end end


def test_finds_migrations_in_subdirectories def test_finds_migrations_in_subdirectories
migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/valid_with_subdirectories").migrations list = ActiveRecord::Migrator.migrations(MIGRATIONS_ROOT + "/valid_with_subdirectories")
migrations = ActiveRecord::Migrator.new(:up, list).migrations


[[1, 'ValidPeopleHaveLastNames'], [2, 'WeNeedReminders'], [3, 'InnocentJointable']].each_with_index do |pair, i| [[1, 'ValidPeopleHaveLastNames'], [2, 'WeNeedReminders'], [3, 'InnocentJointable']].each_with_index do |pair, i|
assert_equal migrations[i].version, pair.first assert_equal migrations[i].version, pair.first
Expand All @@ -909,7 +911,8 @@ def test_finds_migrations_in_subdirectories


def test_finds_migrations_from_two_directories def test_finds_migrations_from_two_directories
directories = [MIGRATIONS_ROOT + '/valid_with_timestamps', MIGRATIONS_ROOT + '/to_copy_with_timestamps'] directories = [MIGRATIONS_ROOT + '/valid_with_timestamps', MIGRATIONS_ROOT + '/to_copy_with_timestamps']
migrations = ActiveRecord::Migrator.new(:up, directories).migrations list = ActiveRecord::Migrator.migrations directories
migrations = ActiveRecord::Migrator.new(:up, list).migrations


[[20090101010101, "PeopleHaveHobbies"], [[20090101010101, "PeopleHaveHobbies"],
[20090101010202, "PeopleHaveDescriptions"], [20090101010202, "PeopleHaveDescriptions"],
Expand All @@ -932,13 +935,20 @@ def test_dump_schema_information_outputs_lexically_ordered_versions


def test_finds_pending_migrations def test_finds_pending_migrations
ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_2", 1) ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_2", 1)
migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/interleaved/pass_2").pending_migrations migration_list = ActiveRecord::Migrator.migrations(MIGRATIONS_ROOT + "/interleaved/pass_2")
migrations = ActiveRecord::Migrator.new(:up, migration_list).pending_migrations


assert_equal 1, migrations.size assert_equal 1, migrations.size
assert_equal migrations[0].version, 3 assert_equal migrations[0].version, 3
assert_equal migrations[0].name, 'InterleavedInnocentJointable' assert_equal migrations[0].name, 'InterleavedInnocentJointable'
end end


def test_deprecated_constructor
assert_deprecated do
ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/interleaved/pass_2")
end
end

def test_relative_migrations def test_relative_migrations
list = Dir.chdir(MIGRATIONS_ROOT) do list = Dir.chdir(MIGRATIONS_ROOT) do
ActiveRecord::Migrator.up("valid/", 1) ActiveRecord::Migrator.up("valid/", 1)
Expand Down

0 comments on commit bc276fb

Please sign in to comment.