Skip to content

Commit b09c110

Browse files
authored
Merge pull request rails#42198 from zetlanddk/remove-dangling-migrations
Handle duplicate migration names in multi db
2 parents 2367654 + 1a1c455 commit b09c110

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

activerecord/lib/active_record/migration.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,9 @@ def migration
10441044
end
10451045

10461046
def load_migration
1047-
require(File.expand_path(filename))
1047+
Object.send(:remove_const, name) rescue nil
1048+
1049+
load(File.expand_path(filename))
10481050
name.constantize.new(name, version)
10491051
end
10501052
end

activerecord/test/cases/migration_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
require MIGRATIONS_ROOT + "/rename/2_rename_things"
1616
require MIGRATIONS_ROOT + "/decimal/1_give_me_big_numbers"
1717

18+
class ValidPeopleHaveLastNames < ActiveRecord::Migration::Current
19+
def change
20+
drop_table :people
21+
end
22+
end
23+
1824
class BigNumber < ActiveRecord::Base
1925
unless current_adapter?(:PostgreSQLAdapter, :SQLite3Adapter)
2026
attribute :value_of_e, :integer
@@ -108,6 +114,14 @@ def test_migrator_versions
108114
assert_equal true, migrator.needs_migration?
109115
end
110116

117+
def test_name_collision_across_dbs
118+
migrations_path = MIGRATIONS_ROOT + "/valid"
119+
migrator = ActiveRecord::MigrationContext.new(migrations_path)
120+
migrator.up
121+
122+
assert_column Person, :last_name
123+
end
124+
111125
def test_migration_detection_without_schema_migration_table
112126
ActiveRecord::Base.connection.drop_table "schema_migrations", if_exists: true
113127

0 commit comments

Comments
 (0)