1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Rick Olson 2007-07-24 04:10:22 +00:00
parent 1e1f93fd10
commit 7de537dbf4
4 changed files with 31 additions and 2 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Automatically generate add/remove column commands in specially named migrations like AddLocationToEvent. Closes #9006 [zenspider]
* Default to plural table name in Rails Generator if ActiveRecord is not present. Closes #8963 [evan]
* Added rake routes for listing all the defined routes in the system #8795 [josh]

View file

@ -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

View file

@ -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

View file

@ -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