diff options
Diffstat (limited to 'railties/guides/source/migrations.textile')
-rw-r--r-- | railties/guides/source/migrations.textile | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index e51ee0f535..6fcc3cf4a2 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -300,6 +300,7 @@ change_table :products do |t| t.rename :upccode, :upc_code end </ruby> + removes the +description+ and +name+ columns, creates a +part_number+ column and adds an index on it. Finally it renames the +upccode+ column. This is the same as doing <ruby> @@ -471,13 +472,13 @@ By default migrations tell you exactly what they're doing and how long it took. Several methods are provided that allow you to control all this: -* +suppress_messages+ suppresses any output generated by its block -* +say+ outputs text (the second argument controls whether it is indented or not) +* +suppress_messages+ takes a block as an argument and suppresses any output generated by the block. +* +say+ takes a message argument and outputs it as is. A second boolean argument can be passed to specify whether to indent or not. * +say_with_time+ outputs text along with how long it took to run its block. If the block returns an integer it assumes it is the number of rows affected. For example, this migration -<pre> +<ruby> class CreateProducts < ActiveRecord::Migration def change suppress_messages do @@ -496,7 +497,7 @@ class CreateProducts < ActiveRecord::Migration end end end -</pre> +</ruby> generates the following output @@ -510,7 +511,7 @@ generates the following output 20080906170109 CreateProducts: migrated (10.0097s) </shell> -If you just want Active Record to shut up then running +rake db:migrate VERBOSE=false+ will suppress any output. +If you just want Active Record to shut up then running +rake db:migrate VERBOSE=false+ will suppress all output. h3. Using Models in Your Migrations @@ -525,7 +526,7 @@ Bob goes on vacation. Alice creates a migration for the +products+ table which adds a new column and initializes it. She also adds a validation to the Product model for the new column. -<pre> +<ruby> # db/migrate/20100513121110_add_flag_to_product.rb class AddFlagToProduct < ActiveRecord::Migration @@ -534,19 +535,19 @@ class AddFlagToProduct < ActiveRecord::Migration Product.all.each { |f| f.update_attributes!(:flag => 'false') } end end -</pre> +</ruby> -<pre> +<ruby> # app/model/product.rb class Product < ActiveRecord::Base - validates_presence_of :flag + validates :flag, :presence => true end -</pre> +</ruby> Alice adds a second migration which adds and initializes another column to the +products+ table and also adds a validation to the Product model for the new column. -<pre> +<ruby> # db/migrate/20100515121110_add_fuzz_to_product.rb class AddFuzzToProduct < ActiveRecord::Migration @@ -555,16 +556,15 @@ class AddFuzzToProduct < ActiveRecord::Migration Product.all.each { |f| f.update_attributes! :fuzz => 'fuzzy' } end end -</pre> +</ruby> -<pre> +<ruby> # app/model/product.rb class Product < ActiveRecord::Base - validates_presence_of :flag - validates_presence_of :fuzz + validates :flag, :fuzz, :presence => true end -</pre> +</ruby> Both migrations work for Alice. @@ -575,12 +575,12 @@ Bob comes back from vacation and: The migration crashes because when the model attempts to save, it tries to validate the second added column, which is not in the database when the _first_ migration runs. -<pre> +<plain> rake aborted! An error has occurred, this and all later migrations canceled: undefined method `fuzz' for #<Product:0x000001049b14a0> -</pre> +</plain> A fix for this is to create a local model within the migration. This keeps rails from running the validations, so that the migrations run to completion. @@ -588,21 +588,22 @@ When using a faux model, it's a good idea to call +Product.reset_column_informat If Alice had done this instead, there would have been no problem: -<pre> +<ruby> # db/migrate/20100513121110_add_flag_to_product.rb class AddFlagToProduct < ActiveRecord::Migration class Product < ActiveRecord::Base end + def change add_column :products, :flag, :int - Product.reset_column_information + Product.reset_column_information Product.all.each { |f| f.update_attributes!(:flag => false) } end end -</pre> +</ruby> -<pre> +<ruby> # db/migrate/20100515121110_add_fuzz_to_product.rb class AddFuzzToProduct < ActiveRecord::Migration @@ -614,7 +615,7 @@ class AddFuzzToProduct < ActiveRecord::Migration Product.all.each { |f| f.update_attributes! :fuzz => 'fuzzy' } end end -</pre> +</ruby> h3. Schema Dumping and You |