diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 597b876f22..7a3aa78e46 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added protection against duplicate migration names (Aslak Hellesøy) [#112] + * Base#instantiate_time_object: eliminate check for Time.zone, since we can assume this is set if time_zone_aware_attributes is set to true [Geoff Buesing] * Time zone aware attribute methods use Time.zone.parse instead of #to_time for String arguments, so that offset information in String is respected. Resolves #105. [Scott Fleckenstein, Geoff Buesing] diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index af4fb6e83c..5cc9f4e197 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -8,6 +8,12 @@ module ActiveRecord end end + class DuplicateMigrationNameError < ActiveRecordError#:nodoc: + def initialize(name) + super("Multiple migrations have the name #{name}") + end + end + class UnknownMigrationVersionError < ActiveRecordError #:nodoc: def initialize(version) super("No migration with version number #{version}") @@ -440,6 +446,10 @@ module ActiveRecord if klasses.detect { |m| m.version == version } raise DuplicateMigrationVersionError.new(version) end + + if klasses.detect { |m| m.name == name.camelize } + raise DuplicateMigrationNameError.new(name.camelize) + end load(file) diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 6be31b5f86..527856b4c0 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -984,6 +984,12 @@ if ActiveRecord::Base.connection.supports_migrations? end end + def test_migrator_with_duplicate_names + assert_raises(ActiveRecord::DuplicateMigrationNameError, "Multiple migrations have the name Chunky") do + ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/duplicate_names", nil) + end + end + def test_migrator_with_missing_version_numbers assert_raise(ActiveRecord::UnknownMigrationVersionError) do ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/missing", 500) diff --git a/activerecord/test/migrations/duplicate_names/20080507052938_chunky.rb b/activerecord/test/migrations/duplicate_names/20080507052938_chunky.rb new file mode 100644 index 0000000000..5fe5089e18 --- /dev/null +++ b/activerecord/test/migrations/duplicate_names/20080507052938_chunky.rb @@ -0,0 +1,7 @@ +class Chunky < ActiveRecord::Migration + def self.up + end + + def self.down + end +end diff --git a/activerecord/test/migrations/duplicate_names/20080507053028_chunky.rb b/activerecord/test/migrations/duplicate_names/20080507053028_chunky.rb new file mode 100644 index 0000000000..5fe5089e18 --- /dev/null +++ b/activerecord/test/migrations/duplicate_names/20080507053028_chunky.rb @@ -0,0 +1,7 @@ +class Chunky < ActiveRecord::Migration + def self.up + end + + def self.down + end +end