Skip to content

Commit

Permalink
Make the migration generator handle pre-existing migrations with the …
Browse files Browse the repository at this point in the history
…same timestamp.

In the event a migration already exists with that number, the new migration's timestamp will be incremented by 1.

[#4412 state:resolved]

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information
phs authored and NZKoz committed Apr 19, 2010
1 parent eb83c9c commit 6437393
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions activerecord/lib/rails/generators/active_record.rb
Expand Up @@ -19,10 +19,11 @@ def self.source_root
# Implement the required interface for Rails::Generators::Migration.
#
def self.next_migration_number(dirname) #:nodoc:
next_migration_number = current_migration_number(dirname) + 1
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("%Y%m%d%H%M%S")
[Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
else
"%.3d" % (current_migration_number(dirname) + 1)
"%.3d" % next_migration_number
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions railties/test/generators/migration_generator_test.rb
Expand Up @@ -10,6 +10,19 @@ def test_migration
assert_migration "db/migrate/#{migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration/
end

def test_migrations_generated_simultaneously
migrations = ["change_title_body_from_posts", "change_email_from_comments"]

first_migration_number, second_migration_number = migrations.collect do |migration|
run_generator [migration]
file_name = migration_file_name "db/migrate/#{migration}.rb"

File.basename(file_name).split('_').first
end

assert_not_equal first_migration_number, second_migration_number
end

def test_migration_with_class_name
migration = "ChangeTitleBodyFromPosts"
run_generator [migration]
Expand Down

1 comment on commit 6437393

@parndt
Copy link
Contributor

@parndt parndt commented on 6437393 Apr 19, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an excellent move, thanks!

Please sign in to comment.