Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7422

Show
Ignore:
Timestamp:
09/09/07 05:40:34 (10 months ago)
Author:
nzkoz
Message:

Use attribute pairs instead of the migration name to create add and remove column migrations. Closes #9166 [lifofifo]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/CHANGELOG

    r7235 r7422  
    11*SVN* 
     2 
     3* Use attribute pairs instead of the migration name to create add and remove column migrations.  Closes #9166 [lifofifo] 
     4 
     5        For example: 
     6 
     7        ruby script/generation migration AddSomeStuffToCustomers first_name:string last_name:string 
     8         
     9        or 
     10         
     11        ruby script/generation migration RemoveSomeStuffFromCustomers first_name:string last_name:string 
    212 
    313* Add ActiveResource to Rails::Info.  Closes #8741 [kampers] 
  • trunk/railties/lib/rails_generator/generators/components/migration/migration_generator.rb

    r7216 r7422  
    1 class MigrationGenerator < Rails::Generator::NamedBase 
     1class MigrationGenerator < Rails::Generator::NamedBase   
    22  def manifest 
    33    record do |m| 
    4       m.migration_template 'migration.rb', 'db/migrate' 
     4      m.migration_template 'migration.rb', 'db/migrate', :assigns => get_local_assigns 
    55    end 
    66  end 
    7  
    8   def auto_migration direction 
    9     case class_name.underscore 
    10     when /^(add|remove)_(.*)_(?:to|from)_(.*)/ then 
    11       action, col, tbl = $1, $2, $3.pluralize 
    12  
    13       unless (action == "add") ^ (direction == :up) then 
    14         %(\n    add_column :#{tbl}, :#{col}, :type, :null => :no?, :default => :maybe?) 
     7   
     8  private 
     9   
     10  def get_local_assigns 
     11    returning(assigns = {}) do 
     12      if class_name.underscore =~ /^(add|remove)_.*_(?:to|from)_(.*)/ 
     13        assigns[:migration_action] = $1 
     14        assigns[:table_name]       = $2.pluralize 
    1515      else 
    16         %(\n    remove_column :#{tbl}, :#{col}) 
     16        assigns[:attributes] = [] 
    1717      end 
    1818    end 
  • trunk/railties/lib/rails_generator/generators/components/migration/templates/migration.rb

    r7216 r7422  
    11class <%= class_name.underscore.camelize %> < ActiveRecord::Migration 
    2   def self.up<%= auto_migration :up %> 
     2  def self.up<% attributes.each do |attribute| %> 
     3    <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><% end -%> 
     4  <% end %> 
    35  end 
    46 
    5   def self.down<%= auto_migration :down %> 
     7  def self.down<% attributes.reverse.each do |attribute| %> 
     8    <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end -%> 
     9  <% end %> 
    610  end 
    711end 
  • trunk/railties/lib/rails_generator/generators/components/migration/USAGE

    r7216 r7422  
    11Description: 
    22    Stubs out a new database migration. Pass the migration name, either 
    3     CamelCased or under_scored, as an argument. A migration class is generated 
    4     in db/migrate prefixed by the latest migration number. 
     3    CamelCased or under_scored, and an optional list of attribute pairs as arguments. 
     4 
     5    A migration class is generated in db/migrate prefixed by the latest migration number. 
    56 
    67    You can name your migration in either of these formats to generate add/remove 
    7     column lines: AddColumnToTable or RemoveColumnFromTable 
     8    column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable 
    89 
    910Example: 
     
    1314    db/migrate/005_add_ssl_flag.rb 
    1415 
    15     `./script/generate migration AddSslFlagToAccount
     16    `./script/generate migration AddTitleBodyToPost title:string body:text published:boolean
    1617     
    17     This will create the AddSslFlagToAccount in db/migrate/005_add_ssl_flag_to_account.rb with 
     18    This will create the AddTitleBodyToPost in db/migrate/005_add_title_body_to_post.rb with 
    1819    this in the Up migration: 
    1920 
    20       add_column :accounts, :ssl_flag, :type, :null => :no?, :default => :maybe? 
     21      add_column :posts, :title, :string   
     22      add_column :posts, :body, :text   
     23      add_column :posts, :published, :boolean 
    2124 
    2225    And this in the Down migration: 
    2326     
    24       remove_column :accounts, :ssl_flag 
     27      remove_column :posts, :published   
     28      remove_column :posts, :body   
     29      remove_column :posts, :title