Permalink
Browse files

Added that an DuplicateMigrationVersionError gets raised when multipl…

…e migrations have the same version number.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2832 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent aa0f382 commit d5859d0b17ccbf5907a2eb2a979257ec6474e677 @csshsh csshsh committed Oct 31, 2005
@@ -2,6 +2,12 @@ module ActiveRecord
class IrreversibleMigration < ActiveRecordError#:nodoc:
end
+ class DuplicateMigrationVersionError < ActiveRecordError#:nodoc:
+ def initialize(version)
+ super("Multiple migrations have the version number #{version}")
+ end
+ end
+
# Migrations can manage the evolution of a schema used by several physical databases. It's a solution
# to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to
# push that change to other developers and to the production server. With migrations, you can describe the transformations
@@ -209,15 +215,22 @@ def migrate
private
def migration_classes
- migrations = migration_files.collect do |migration_file|
+ migrations = migration_files.inject([]) do |migrations, migration_file|
load(migration_file)
version, name = migration_version_and_name(migration_file)
- [ version.to_i, migration_class(name) ]
+ assert_unique_migration_version(migrations, version.to_i)
+ migrations << [ version.to_i, migration_class(name) ]
end
-
+
down? ? migrations.sort.reverse : migrations.sort
end
-
+
+ def assert_unique_migration_version(migrations, version)
+ if !migrations.empty? && migrations.transpose.first.include?(version)
+ raise DuplicateMigrationVersionError.new(version)
+ end
+ end
+
def migration_files
files = Dir["#{@migrations_path}/[0-9]*_*.rb"].sort_by do |f|
migration_version_and_name(f).first.to_i
@@ -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
@@ -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
@@ -0,0 +1,7 @@
+class Foo < ActiveRecord::Migration
+ def self.up
+ end
+
+ def self.down
+ end
+end
@@ -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
@@ -408,5 +408,11 @@ def test_add_drop_table_with_prefix_and_suffix
ActiveRecord::Base.table_name_suffix = ''
Reminder.reset_table_name
end
+
+ def test_migrator_with_duplicates
+ assert_raises(ActiveRecord::DuplicateMigrationVersionError) do
+ ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_duplicate/', nil)
+ end
+ end
end
end

0 comments on commit d5859d0

Please sign in to comment.