Permalink
Browse files

Migrations may be destroyed: script/destroy migration foo. Closes #2635.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2798 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 25410d5 commit 85513e37bf76bc79c0075da8b4d81c9c5f075b1d @jeremy jeremy committed Oct 29, 2005
View
@@ -1,5 +1,7 @@
*SVN*
+* Migrations may be destroyed: script/destroy migration foo. #2635 [Charles M. Gerungan <charles.gerungan@gmail.com>, Jamis Buck, Jeremy Kemper]
+
* Added that plugins can carry generators and that generator stub files can be created along with new plugins using script/generate plugin <name> --with-generator [DHH]
* Removed app/apis as a default empty dir since its automatically created when using script/generate web_service [DHH]
@@ -1,11 +1,58 @@
class MigrationGenerator < Rails::Generator::NamedBase
def manifest
record do |m|
- m.directory File.join('db/migrate')
- existing_migrations = Dir.glob("db/migrate/[0-9]*_#{file_name}.rb")
- raise "Another migration already exists with the same name" unless existing_migrations.empty?
- next_migration_number = Dir.glob("db/migrate/[0-9]*.rb").size + 1
- m.template 'migration.rb', File.join('db/migrate', "#{next_migration_number}_#{file_name}.rb")
+ m.directory 'db/migrate'
+ m.migration_template 'migration.rb', 'db/migrate'
+ end
+ end
+end
+
+module Rails::Generator::Commands
+ class Base
+ def existing_migrations(file_name)
+ Dir.glob("db/migrate/[0-9]*_#{file_name}.rb")
+ end
+
+ def migration_exists?(file_name)
+ not existing_migrations(file_name).empty?
+ end
+
+ def current_migration_number
+ Dir.glob('db/migrate/[0-9]*.rb').inject(0) do |max, file_path|
+ n = File.basename(file_path).split('_', 2).first.to_i
+ if n > max then n else max end
+ end
+ end
+
+ def next_migration_number
+ current_migration_number + 1
+ end
+
+ def next_migration_string(padding = 3)
+ "%.#{padding}d" % next_migration_number
+ end
+ end
+
+ # When creating, it knows to find the first available file in db/migrate and use the migration.rb template. When deleting, it knows to delete every file named "[0-9]*_#{file_name}".
+ class Create
+ def migration_template(relative_source, relative_destination, template_options = {})
+ raise "Another migration is already named #{file_name}: #{existing_migrations(file_name).first}" if migration_exists?(file_name)
+ template(relative_source, "#{relative_destination}/#{next_migration_string}_#{file_name}.rb", template_options)
+ end
+ end
+
+ class Destroy
+ def migration_template(relative_source, relative_destination, template_options = {})
+ raise "There is no migration named #{file_name}" unless migration_exists?(file_name)
+ existing_migrations(file_name).each do |file_path|
+ file(relative_source, file_path, template_options)
+ end
+ end
+ end
+
+ class List
+ def migration_template(relative_source, relative_destination, options = {})
+ logger.migration_template file_name
end
end
end

0 comments on commit 85513e3

Please sign in to comment.