Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / source / migrations / creating_a_migration.txt
1 == Creating A Migration ==
2
3 === Creating a model ===
4
5 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
6
7 `ruby script/generate model Product name:string description:text` will create a migration that looks like this
8
9 [source, ruby]
10 -----------------------
11 class CreateProducts < ActiveRecord::Migration
12 def self.up
13 create_table :products do |t|
14 t.string :name
15 t.text :description
16
17 t.timestamps
18 end
19 end
20
21 def self.down
22 drop_table :products
23 end
24 end
25 -----------------------
26
27 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
28 are automatically populated by Active Record) will be added for you.
29
30 === Creating a standalone migration ===
31 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:
32
33 `ruby script/generate migration AddPartNumberToProducts`
34
35 This will create an empty but appropriately named migration:
36
37 [source, ruby]
38 -----------------------
39 class AddPartNumberToProducts < ActiveRecord::Migration
40 def self.up
41 end
42
43 def self.down
44 end
45 end
46 -----------------------
47
48 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
49 the appropriate add and remove column statements will be created.
50
51 `ruby script/generate migration AddPartNumberToProducts part_number:string`
52
53 will generate
54
55 [source, ruby]
56 -----------------------
57 class AddPartNumberToProducts < ActiveRecord::Migration
58 def self.up
59 add_column :products, :part_number, :string
60 end
61
62 def self.down
63 remove_column :products, :part_number
64 end
65 end
66 -----------------------
67
68 Similarly,
69
70 `ruby script/generate migration RemovePartNumberFromProducts part_number:string`
71
72 generates
73
74 [source, ruby]
75 -----------------------
76 class RemovePartNumberFromProducts < ActiveRecord::Migration
77 def self.up
78 remove_column :products, :part_number
79 end
80
81 def self.down
82 add_column :products, :part_number, :string
83 end
84 end
85 -----------------------
86
87 You are not limited to one magically generated column, for example
88
89 `ruby script/generate migration AddDetailsToProducts part_number:string price:decimal`
90
91 generates
92
93 [source, ruby]
94 -----------------------
95 class AddDetailsToProducts < ActiveRecord::Migration
96 def self.up
97 add_column :products, :part_number, :string
98 add_column :products, :price, :decimal
99 end
100
101 def self.down
102 remove_column :products, :price
103 remove_column :products, :part_number
104 end
105 end
106 -----------------------
107
108 As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit.
109