diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-01-11 11:41:27 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-01-13 14:33:53 -0800 |
commit | bc276fbc2a94778686e3ec4a950926083b4e9778 (patch) | |
tree | 6a479b692cddb5534d8f571057227579fa0bf38a | |
parent | 29fdd8c7779ec2ba74cfb742e8fcef47bfae6c09 (diff) | |
download | rails-bc276fbc2a94778686e3ec4a950926083b4e9778.tar.gz rails-bc276fbc2a94778686e3ec4a950926083b4e9778.tar.bz2 rails-bc276fbc2a94778686e3ec4a950926083b4e9778.zip |
construct a migrator with a list of migrations rather than a list of paths
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 47 | ||||
-rw-r--r-- | activerecord/test/cases/migration_test.rb | 18 |
2 files changed, 43 insertions, 22 deletions
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 10073e2b37..8dade3b576 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -546,14 +546,14 @@ module ActiveRecord def migrate(migrations_paths, target_version = nil, &block) case - when target_version.nil? - up(migrations_paths, target_version, &block) - when current_version == 0 && target_version == 0 - [] - when current_version > target_version - down(migrations_paths, target_version, &block) - else - up(migrations_paths, target_version, &block) + when target_version.nil? + up(migrations_paths, target_version, &block) + when current_version == 0 && target_version == 0 + [] + when current_version > target_version + down(migrations_paths, target_version, &block) + else + up(migrations_paths, target_version, &block) end end @@ -566,15 +566,19 @@ module ActiveRecord end 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 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 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 def schema_migrations_table_name @@ -638,7 +642,7 @@ module ActiveRecord private 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) if start_index @@ -649,10 +653,20 @@ module ActiveRecord 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? 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" + @migrations = self.class.migrations(migrations) + end + + @target_version = target_version end def current_version @@ -721,10 +735,7 @@ module ActiveRecord end def migrations - @migrations ||= begin - migrations = self.class.migrations(@migrations_paths) - down? ? migrations.reverse : migrations - end + down? ? @migrations.reverse : @migrations end def pending_migrations diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 41a060b706..9038ebd320 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -890,7 +890,8 @@ class MigrationTest < ActiveRecord::TestCase end 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| assert_equal migrations[i].version, pair.first @@ -899,7 +900,8 @@ class MigrationTest < ActiveRecord::TestCase end 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| assert_equal migrations[i].version, pair.first @@ -909,7 +911,8 @@ class MigrationTest < ActiveRecord::TestCase def test_finds_migrations_from_two_directories 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"], [20090101010202, "PeopleHaveDescriptions"], @@ -932,13 +935,20 @@ class MigrationTest < ActiveRecord::TestCase def test_finds_pending_migrations 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 migrations[0].version, 3 assert_equal migrations[0].name, 'InterleavedInnocentJointable' end + def test_deprecated_constructor + assert_deprecated do + ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/interleaved/pass_2") + end + end + def test_relative_migrations list = Dir.chdir(MIGRATIONS_ROOT) do ActiveRecord::Migrator.up("valid/", 1) |