diff options
7 files changed, 60 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 8369a10fac..3fdc4374d2 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Migrations: gracefully handle missing migration files. #5857 [eli.gordon@gmail.com] + * MySQL: update test schema for MySQL 5 strict mode. #5861 [Tom Ward] * to_xml: correct naming of included associations. #5831 [josh.starcher@gmail.com] diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index acafda0048..cca46f1df4 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -382,7 +382,8 @@ module ActiveRecord end def reached_target_version?(version) - (up? && version.to_i - 1 == @target_version) || (down? && version.to_i == @target_version) + return false if @target_version == nil + (up? && version.to_i - 1 >= @target_version) || (down? && version.to_i <= @target_version) end def irrelevant_migration?(version) diff --git a/activerecord/test/fixtures/migrations_with_missing_versions/1000_people_have_middle_names.rb b/activerecord/test/fixtures/migrations_with_missing_versions/1000_people_have_middle_names.rb new file mode 100644 index 0000000000..753ee34152 --- /dev/null +++ b/activerecord/test/fixtures/migrations_with_missing_versions/1000_people_have_middle_names.rb @@ -0,0 +1,9 @@ +class PeopleHaveMiddleNames < ActiveRecord::Migration + def self.up + add_column "people", "middle_name", :string + end + + def self.down + remove_column "people", "middle_name" + end +end
\ No newline at end of file diff --git a/activerecord/test/fixtures/migrations_with_missing_versions/1_people_have_last_names.rb b/activerecord/test/fixtures/migrations_with_missing_versions/1_people_have_last_names.rb new file mode 100644 index 0000000000..009729b330 --- /dev/null +++ b/activerecord/test/fixtures/migrations_with_missing_versions/1_people_have_last_names.rb @@ -0,0 +1,9 @@ +class PeopleHaveLastNames < ActiveRecord::Migration + def self.up + add_column "people", "last_name", :string + end + + def self.down + remove_column "people", "last_name" + end +end
\ No newline at end of file diff --git a/activerecord/test/fixtures/migrations_with_missing_versions/3_we_need_reminders.rb b/activerecord/test/fixtures/migrations_with_missing_versions/3_we_need_reminders.rb new file mode 100644 index 0000000000..ac5918f02a --- /dev/null +++ b/activerecord/test/fixtures/migrations_with_missing_versions/3_we_need_reminders.rb @@ -0,0 +1,12 @@ +class WeNeedReminders < ActiveRecord::Migration + def self.up + create_table("reminders") do |t| + t.column :content, :text + t.column :remind_at, :datetime + end + end + + def self.down + drop_table "reminders" + end +end
\ No newline at end of file diff --git a/activerecord/test/fixtures/migrations_with_missing_versions/4_innocent_jointable.rb b/activerecord/test/fixtures/migrations_with_missing_versions/4_innocent_jointable.rb new file mode 100644 index 0000000000..21c9ca5328 --- /dev/null +++ b/activerecord/test/fixtures/migrations_with_missing_versions/4_innocent_jointable.rb @@ -0,0 +1,12 @@ +class InnocentJointable < ActiveRecord::Migration + def self.up + create_table("people_reminders", :id => false) do |t| + t.column :reminder_id, :integer + t.column :person_id, :integer + end + end + + def self.down + drop_table "people_reminders" + end +end
\ No newline at end of file diff --git a/activerecord/test/migration_test.rb b/activerecord/test/migration_test.rb index 14ce95073b..fdd21df300 100644 --- a/activerecord/test/migration_test.rb +++ b/activerecord/test/migration_test.rb @@ -44,6 +44,7 @@ if ActiveRecord::Base.connection.supports_migrations? Person.connection.remove_column('people', column) rescue nil end Person.connection.remove_column("people", "first_name") rescue nil + Person.connection.remove_column("people", "middle_name") rescue nil Person.connection.add_column("people", "first_name", :string, :limit => 40) Person.reset_column_information end @@ -661,5 +662,18 @@ if ActiveRecord::Base.connection.supports_migrations? ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_duplicate/', nil) end end + + def test_migrator_with_missing_version_numbers + ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 500) + assert !Person.column_methods_hash.include?(:middle_name) + assert_equal 4, ActiveRecord::Migrator.current_version + + ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 2) + assert !Reminder.table_exists? + assert Person.column_methods_hash.include?(:last_name) + assert_equal 2, ActiveRecord::Migrator.current_version + end + + end end |