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

Changeset 7216

Show
Ignore:
Timestamp:
07/24/07 04:10:22 (1 year ago)
Author:
rick
Message:

Automatically generate add/remove column commands in specially named migrations like AddLocationToEvent. Closes #9006 [zenspider]

Files:

Legend:

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

    r7183 r7216  
    11*SVN* 
     2 
     3* Automatically generate add/remove column commands in specially named migrations like AddLocationToEvent.  Closes #9006 [zenspider] 
    24 
    35* Default to plural table name in Rails Generator if ActiveRecord is not present.  Closes #8963 [evan] 
  • trunk/railties/lib/rails_generator/generators/components/migration/migration_generator.rb

    r3101 r7216  
    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?) 
     15      else 
     16        %(\n    remove_column :#{tbl}, :#{col}) 
     17      end 
     18    end 
     19  end 
    720end 
  • trunk/railties/lib/rails_generator/generators/components/migration/templates/migration.rb

    r4553 r7216  
    11class <%= class_name.underscore.camelize %> < ActiveRecord::Migration 
    2   def self.up 
     2  def self.up<%= auto_migration :up %> 
    33  end 
    44 
    5   def self.down 
     5  def self.down<%= auto_migration :down %> 
    66  end 
    77end 
  • trunk/railties/lib/rails_generator/generators/components/migration/USAGE

    r6884 r7216  
    33    CamelCased or under_scored, as an argument. A migration class is generated 
    44    in db/migrate prefixed by the latest migration number. 
     5 
     6    You can name your migration in either of these formats to generate add/remove 
     7    column lines: AddColumnToTable or RemoveColumnFromTable 
    58 
    69Example: 
     
    912    With 4 existing migrations, this creates the AddSslFlag migration in 
    1013    db/migrate/005_add_ssl_flag.rb 
     14 
     15    `./script/generate migration AddSslFlagToAccount` 
     16     
     17    This will create the AddSslFlagToAccount in db/migrate/005_add_ssl_flag_to_account.rb with 
     18    this in the Up migration: 
     19 
     20      add_column :accounts, :ssl_flag, :type, :null => :no?, :default => :maybe? 
     21 
     22    And this in the Down migration: 
     23     
     24      remove_column :accounts, :ssl_flag