diff options
author | Rick Olson <technoweenie@gmail.com> | 2007-07-24 04:10:22 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2007-07-24 04:10:22 +0000 |
commit | 7de537dbf4cf99e2d85854fd6331a30604064656 (patch) | |
tree | c7aba66bb8e8d60a48dc6271266363c9cffc57d2 /railties/lib/rails_generator | |
parent | 1e1f93fd10f186a764cdff0e89936c55274171a9 (diff) | |
download | rails-7de537dbf4cf99e2d85854fd6331a30604064656.tar.gz rails-7de537dbf4cf99e2d85854fd6331a30604064656.tar.bz2 rails-7de537dbf4cf99e2d85854fd6331a30604064656.zip |
Automatically generate add/remove column commands in specially named migrations like AddLocationToEvent. Closes #9006 [zenspider]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7216 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/lib/rails_generator')
3 files changed, 29 insertions, 2 deletions
diff --git a/railties/lib/rails_generator/generators/components/migration/USAGE b/railties/lib/rails_generator/generators/components/migration/USAGE index 152e2abdd6..c3f8ce0987 100644 --- a/railties/lib/rails_generator/generators/components/migration/USAGE +++ b/railties/lib/rails_generator/generators/components/migration/USAGE @@ -3,8 +3,22 @@ Description: CamelCased or under_scored, as an argument. A migration class is generated in db/migrate prefixed by the latest migration number. + You can name your migration in either of these formats to generate add/remove + column lines: AddColumnToTable or RemoveColumnFromTable + Example: `./script/generate migration AddSslFlag` With 4 existing migrations, this creates the AddSslFlag migration in db/migrate/005_add_ssl_flag.rb + + `./script/generate migration AddSslFlagToAccount` + + This will create the AddSslFlagToAccount in db/migrate/005_add_ssl_flag_to_account.rb with + this in the Up migration: + + add_column :accounts, :ssl_flag, :type, :null => :no?, :default => :maybe? + + And this in the Down migration: + + remove_column :accounts, :ssl_flag
\ No newline at end of file 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 a0d0d472c2..2fd348e47b 100644 --- a/railties/lib/rails_generator/generators/components/migration/migration_generator.rb +++ b/railties/lib/rails_generator/generators/components/migration/migration_generator.rb @@ -4,4 +4,17 @@ class MigrationGenerator < Rails::Generator::NamedBase m.migration_template 'migration.rb', 'db/migrate' end end + + def auto_migration direction + case class_name.underscore + when /^(add|remove)_(.*)_(?:to|from)_(.*)/ then + action, col, tbl = $1, $2, $3.pluralize + + unless (action == "add") ^ (direction == :up) then + %(\n add_column :#{tbl}, :#{col}, :type, :null => :no?, :default => :maybe?) + else + %(\n remove_column :#{tbl}, :#{col}) + end + end + end end diff --git a/railties/lib/rails_generator/generators/components/migration/templates/migration.rb b/railties/lib/rails_generator/generators/components/migration/templates/migration.rb index 2600610fc6..86edcac261 100644 --- a/railties/lib/rails_generator/generators/components/migration/templates/migration.rb +++ b/railties/lib/rails_generator/generators/components/migration/templates/migration.rb @@ -1,7 +1,7 @@ class <%= class_name.underscore.camelize %> < ActiveRecord::Migration - def self.up + def self.up<%= auto_migration :up %> end - def self.down + def self.down<%= auto_migration :down %> end end |