Skip to content

Commit 1a1c455

Browse files
committed
Handle duplicate migration names in multi db
In a multidatabse setup, migrations live in seperate directories. The check for duplicate migration names, does not take this into account. So you __can__ end up having multiple migration with the same name. When running `rails db:migrate` from the console, this other migration might be loaded __before__ running the migrations for "this" database. Because they have the same name, we have to ensure that the contents of this migration is not carried over from "the other" migrations.
1 parent 2af458a commit 1a1c455

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)