diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2005-10-29 04:47:51 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2005-10-29 04:47:51 +0000 |
commit | 85513e37bf76bc79c0075da8b4d81c9c5f075b1d (patch) | |
tree | 1d7e98e7a4d40a2e3e21c1048ee67b9feb3c6d66 /railties/lib/rails_generator | |
parent | 25410d53cf0e645b853776539c963aedc30d49b1 (diff) | |
download | rails-85513e37bf76bc79c0075da8b4d81c9c5f075b1d.tar.gz rails-85513e37bf76bc79c0075da8b4d81c9c5f075b1d.tar.bz2 rails-85513e37bf76bc79c0075da8b4d81c9c5f075b1d.zip |
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
Diffstat (limited to 'railties/lib/rails_generator')
-rw-r--r-- | railties/lib/rails_generator/generators/components/migration/migration_generator.rb | 57 |
1 files changed, 52 insertions, 5 deletions
diff --git a/railties/lib/rails_generator/generators/components/migration/migration_generator.rb b/railties/lib/rails_generator/generators/components/migration/migration_generator.rb index 3dff3bf396..65f5e6c74e 100644 --- a/railties/lib/rails_generator/generators/components/migration/migration_generator.rb +++ b/railties/lib/rails_generator/generators/components/migration/migration_generator.rb @@ -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 |