Permalink
Browse files

Don't fail when reinstalling because of existing migrations

  • Loading branch information...
1 parent 2a1ed3e commit a82e6f09b7a7a9a9baa8e275b20419b0c8d6d929 @jferris jferris committed Feb 11, 2011
Showing with 23 additions and 2 deletions.
  1. +4 −0 features/install_generator.feature
  2. +19 −2 lib/diesel/generators/install_base.rb
View
4 features/install_generator.feature
@@ -110,3 +110,7 @@ Feature: reusable install generator
Generate configuration, migration, and other essential files.
"""
+ Scenario: run the install generator twice
+ When I successfully run "rails generate testengine:install --trace"
+ Then the output should not contain "Another migration is already named"
+
View
21 lib/diesel/generators/install_base.rb
@@ -9,8 +9,11 @@ class InstallBase < Diesel::Generators::Base
extend ActiveRecord::Generators::Migration
def generate_migrations
- migrations.each do |migration|
- migration_template migration, migration.sub(%r{(db/migrate/)(?:\d+_)?}, '\1')
+ migrations.each do |source_file|
+ name = migration_name(source_file)
+ unless migration_exists?(name)
+ migration_template source_file, "db/migrate/#{name}"
+ end
end
end
@@ -24,6 +27,20 @@ def self.inherited(generator)
def migrations
files_within_root(".", "db/migrate/*.rb")
end
+
+ def migration_exists?(name)
+ existing_migrations.include?(name)
+ end
+
+ def existing_migrations
+ @existing_migrations ||= Dir.glob("db/migrate/*.rb").map do |file|
+ migration_name(file)
+ end
+ end
+
+ def migration_name(file)
+ file.sub(%r{^.*(db/migrate/)(?:\d+_)?}, '')
+ end
end
end
end

0 comments on commit a82e6f0

Please sign in to comment.