aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2007-09-09 05:40:34 +0000
committerMichael Koziarski <michael@koziarski.com>2007-09-09 05:40:34 +0000
commitfa602bb86d31450c488d087b00c2d12198ab6ec8 (patch)
tree781e2a3b02df7d05e19a28c7270ce2ffe1d02a32 /railties
parent80ff0b9f1c07eae7668680fd12335ffa218e53ac (diff)
downloadrails-fa602bb86d31450c488d087b00c2d12198ab6ec8.tar.gz
rails-fa602bb86d31450c488d087b00c2d12198ab6ec8.tar.bz2
rails-fa602bb86d31450c488d087b00c2d12198ab6ec8.zip
Use attribute pairs instead of the migration name to create add and remove column migrations. Closes #9166 [lifofifo]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7422 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties')
-rw-r--r--railties/CHANGELOG10
-rw-r--r--railties/lib/rails_generator/generators/components/migration/USAGE19
-rw-r--r--railties/lib/rails_generator/generators/components/migration/migration_generator.rb22
-rw-r--r--railties/lib/rails_generator/generators/components/migration/templates/migration.rb8
4 files changed, 39 insertions, 20 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index 0fdaafa672..bd90449127 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,5 +1,15 @@
*SVN*
+* Use attribute pairs instead of the migration name to create add and remove column migrations. Closes #9166 [lifofifo]
+
+ For example:
+
+ ruby script/generation migration AddSomeStuffToCustomers first_name:string last_name:string
+
+ or
+
+ ruby script/generation migration RemoveSomeStuffFromCustomers first_name:string last_name:string
+
* Add ActiveResource to Rails::Info. Closes #8741 [kampers]
* use Gem.find_name instead of search when freezing gems. Prevent false positives for other gems with rails in the name. Closes #8729 [wselman]
diff --git a/railties/lib/rails_generator/generators/components/migration/USAGE b/railties/lib/rails_generator/generators/components/migration/USAGE
index c3f8ce0987..3e914a5d7b 100644
--- a/railties/lib/rails_generator/generators/components/migration/USAGE
+++ b/railties/lib/rails_generator/generators/components/migration/USAGE
@@ -1,10 +1,11 @@
Description:
Stubs out a new database migration. Pass the migration name, either
- CamelCased or under_scored, as an argument. A migration class is generated
- in db/migrate prefixed by the latest migration number.
+ CamelCased or under_scored, and an optional list of attribute pairs as arguments.
+
+ 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
+ column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable
Example:
`./script/generate migration AddSslFlag`
@@ -12,13 +13,17 @@ Example:
With 4 existing migrations, this creates the AddSslFlag migration in
db/migrate/005_add_ssl_flag.rb
- `./script/generate migration AddSslFlagToAccount`
+ `./script/generate migration AddTitleBodyToPost title:string body:text published:boolean`
- This will create the AddSslFlagToAccount in db/migrate/005_add_ssl_flag_to_account.rb with
+ This will create the AddTitleBodyToPost in db/migrate/005_add_title_body_to_post.rb with
this in the Up migration:
- add_column :accounts, :ssl_flag, :type, :null => :no?, :default => :maybe?
+ add_column :posts, :title, :string
+ add_column :posts, :body, :text
+ add_column :posts, :published, :boolean
And this in the Down migration:
- remove_column :accounts, :ssl_flag \ No newline at end of file
+ remove_column :posts, :published
+ remove_column :posts, :body
+ remove_column :posts, :title
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 2fd348e47b..446a421210 100644
--- a/railties/lib/rails_generator/generators/components/migration/migration_generator.rb
+++ b/railties/lib/rails_generator/generators/components/migration/migration_generator.rb
@@ -1,19 +1,19 @@
-class MigrationGenerator < Rails::Generator::NamedBase
+class MigrationGenerator < Rails::Generator::NamedBase
def manifest
record do |m|
- m.migration_template 'migration.rb', 'db/migrate'
+ m.migration_template 'migration.rb', 'db/migrate', :assigns => get_local_assigns
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?)
+
+ private
+
+ def get_local_assigns
+ returning(assigns = {}) do
+ if class_name.underscore =~ /^(add|remove)_.*_(?:to|from)_(.*)/
+ assigns[:migration_action] = $1
+ assigns[:table_name] = $2.pluralize
else
- %(\n remove_column :#{tbl}, :#{col})
+ assigns[:attributes] = []
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 86edcac261..e47f3afd49 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,11 @@
class <%= class_name.underscore.camelize %> < ActiveRecord::Migration
- def self.up<%= auto_migration :up %>
+ def self.up<% attributes.each do |attribute| %>
+ <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end -%>
+ <% end %>
end
- def self.down<%= auto_migration :down %>
+ def self.down<% attributes.reverse.each do |attribute| %>
+ <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end -%>
+ <% end %>
end
end