提交 7de537db 编写于 作者: R Rick Olson

Automatically generate add/remove column commands in specially named...

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
上级 1e1f93fd
*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]
......
......@@ -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
......@@ -4,4 +4,17 @@ def manifest
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
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册