Changeset 7422
- Timestamp:
- 09/09/07 05:40:34 (10 months ago)
- Files:
-
- trunk/railties/CHANGELOG (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/migration/migration_generator.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/migration/templates/migration.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/migration/USAGE (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/railties/CHANGELOG
r7235 r7422 1 1 *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 2 12 3 13 * 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 1 class MigrationGenerator < Rails::Generator::NamedBase 2 2 def manifest 3 3 record do |m| 4 m.migration_template 'migration.rb', 'db/migrate' 4 m.migration_template 'migration.rb', 'db/migrate', :assigns => get_local_assigns 5 5 end 6 6 end 7 8 def auto_migration direction9 case class_name.underscore10 when /^(add|remove)_(.*)_(?:to|from)_(.*)/ then11 action, col, tbl = $1, $2, $3.pluralize12 13 unless (action == "add") ^ (direction == :up) then14 %(\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 15 15 else 16 %(\n remove_column :#{tbl}, :#{col})16 assigns[:attributes] = [] 17 17 end 18 18 end trunk/railties/lib/rails_generator/generators/components/migration/templates/migration.rb
r7216 r7422 1 1 class <%= 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 %> 3 5 end 4 6 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 %> 6 10 end 7 11 end trunk/railties/lib/rails_generator/generators/components/migration/USAGE
r7216 r7422 1 1 Description: 2 2 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. 5 6 6 7 You can name your migration in either of these formats to generate add/remove 7 column lines : AddColumnToTable or RemoveColumnFromTable8 column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable 8 9 9 10 Example: … … 13 14 db/migrate/005_add_ssl_flag.rb 14 15 15 `./script/generate migration Add SslFlagToAccount`16 `./script/generate migration AddTitleBodyToPost title:string body:text published:boolean` 16 17 17 This will create the Add SslFlagToAccount in db/migrate/005_add_ssl_flag_to_account.rb with18 This will create the AddTitleBodyToPost in db/migrate/005_add_title_body_to_post.rb with 18 19 this in the Up migration: 19 20 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 21 24 22 25 And this in the Down migration: 23 26 24 remove_column :accounts, :ssl_flag 27 remove_column :posts, :published 28 remove_column :posts, :body 29 remove_column :posts, :title