Permalink
Browse files

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
  • Loading branch information...
1 parent 1e1f93f commit 7de537dbf4cf99e2d85854fd6331a30604064656 @technoweenie technoweenie committed Jul 24, 2007
View
@@ -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]
@@ -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
@@ -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
@@ -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

0 comments on commit 7de537d

Please sign in to comment.