diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-10-21 18:33:40 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-10-21 18:33:40 +0100 |
commit | a03e2b356c66ddc8809fa2b23a2a7d652f173b8b (patch) | |
tree | ab8552913475bf94a78e4cbfbae804b2ecd9eca2 /railties/doc/guides/source/migrations/creating_a_migration.txt | |
parent | 18542c9e00209679bdaacf64075819fb887ec856 (diff) | |
download | rails-a03e2b356c66ddc8809fa2b23a2a7d652f173b8b.tar.gz rails-a03e2b356c66ddc8809fa2b23a2a7d652f173b8b.tar.bz2 rails-a03e2b356c66ddc8809fa2b23a2a7d652f173b8b.zip |
Merge with docrails. Also add a rake task to generate guides in your rails application :
rake doc:guides
The rake task will generate guides inside doc/guides directory of your application. Open index.html to browse.
Diffstat (limited to 'railties/doc/guides/source/migrations/creating_a_migration.txt')
-rw-r--r-- | railties/doc/guides/source/migrations/creating_a_migration.txt | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/railties/doc/guides/source/migrations/creating_a_migration.txt b/railties/doc/guides/source/migrations/creating_a_migration.txt new file mode 100644 index 0000000000..892c73a533 --- /dev/null +++ b/railties/doc/guides/source/migrations/creating_a_migration.txt @@ -0,0 +1,109 @@ +== Creating A Migration == + +=== Creating a model === + +The model and scaffold generators will create migrations appropriate for adding a new model. This migration will already contain instructions for creating the relevant table. If you tell Rails what columns you want then statements for adding those will also be created. For example, running + +`ruby script/generate model Product name:string description:text` will create a migration that looks like this + +[source, ruby] +----------------------- +class CreateProducts < ActiveRecord::Migration + def self.up + create_table :products do |t| + t.string :name + t.text :description + + t.timestamps + end + end + + def self.down + drop_table :products + end +end +----------------------- + +You can append as many column name/type pairs as you want. By default `t.timestamps` (which creates the `updated_at` and `created_at` columns that +are automatically populated by Active Record) will be added for you. + +=== Creating a standalone migration === +If you are creating migrations for other purposes (for example to add a column to an existing table) then you can use the migration generator: + +`ruby script/generate migration AddPartNumberToProducts` + +This will create an empty but appropriately named migration: + +[source, ruby] +----------------------- +class AddPartNumberToProducts < ActiveRecord::Migration + def self.up + end + + def self.down + end +end +----------------------- + +If the migration name is of the form AddXXXToYYY or RemoveXXXFromY and is followed by a list of column names and types then a migration containing +the appropriate add and remove column statements will be created. + +`ruby script/generate migration AddPartNumberToProducts part_number:string` + +will generate + +[source, ruby] +----------------------- +class AddPartNumberToProducts < ActiveRecord::Migration + def self.up + add_column :products, :part_number, :string + end + + def self.down + remove_column :products, :part_number + end +end +----------------------- + +Similarly, + +`ruby script/generate migration RemovePartNumberFromProducts part_number:string` + +generates + +[source, ruby] +----------------------- +class RemovePartNumberFromProducts < ActiveRecord::Migration + def self.up + remove_column :products, :part_number + end + + def self.down + add_column :products, :part_number, :string + end +end +----------------------- + +You are not limited to one magically generated column, for example + +`ruby script/generate migration AddDetailsToProducts part_number:string price:decimal` + +generates + +[source, ruby] +----------------------- +class AddDetailsToProducts < ActiveRecord::Migration + def self.up + add_column :products, :part_number, :string + add_column :products, :price, :decimal + end + + def self.down + remove_column :products, :price + remove_column :products, :part_number + end +end +----------------------- + +As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit. + |