OSDN Git Service

Regular updates
[twpd/master.git] / rails-migrations.md
1 ---
2 title: Migrations
3 category: Rails
4 ---
5
6 ### Automatically make migrations
7
8     $ rails generate migration RemovePartNumberFromProducts part_number:string
9     $ rails generate migration remove_part_number_from_products part_number # rails assumes string if not type given - and you can use snake_case
10     
11     $ rails generate migration AddNameToWidgets name:string
12     $ rails g migration add_name_to_widgets name:string # you can use the short cut 'g' instead of generate - they both do the same thing
13
14 ### Run migrations
15
16     $ rake db:migrate
17
18 ### Creating tables
19
20     create_table :users do |t|
21       t.string :name
22       t.text   :description
23
24       t.primary_key :id
25       t.string      :title
26       t.text        :description
27       t.integer     :games_count
28       t.float       :lol
29       t.decimal     :price
30       t.decimal     :price, :precision => 2, :scale => 10
31       t.datetime    :expiration
32       t.timestamp   :time_in
33       t.time        :time_in
34       t.date        :expiry
35       t.binary      :image_data
36       t.boolean     :is_admin
37     end
38
39     # Options:
40       :null (boolean)
41       :limit (integer)
42       :default
43
44 ### Operations
45
46     add_column    :users, :first_name, :string
47     remove_column :users, :first_name, :string
48
49     change_column :users, :first_name, :text
50
51     change_column_default :users, :admin, nil
52     change_column_null    :users, :email, false # adds NOT NULL constraint
53
54     create_table
55     change_table
56     drop_table
57
58     add_column
59     change_column
60     rename_column
61     remove_column
62
63     add_index
64     remove_index
65
66 ### Use models
67
68     class AddFlagToProduct < ActiveRecord::Migration
69       class Product < ActiveRecord::Base
70       end
71      
72       def change
73         add_column :products, :flag, :boolean
74         Product.reset_column_information
75         reversible do |dir|
76           dir.up { Product.update_all flag: false }
77         end
78       end
79     end
80
81 ### Associations
82     
83     t.references :category   # kinda same as t.integer :category_id
84
85     # Can have different types
86     t.references :category, polymorphic: true
87
88 ### Auto-Add/remove columns
89   
90     $ rails generate migration RemovePartNumberFromProducts part_number:string
91
92 ### Indices
93
94     # Simple
95     add_index :suppliers, :name
96
97     # Unique
98     add_index :accounts, [:branch_id, :party_id], :unique => true
99
100     # Named (:name => ...)
101     add_index :accounts, [:branch_id, :party_id], :unique => true, :name => "by_branch_party"
102
103     # Length
104     add_index :accounts, :name, :name => ‘by_name’, :length => 10
105     add_index :accounts, [:name, :surname], :name => ‘by_name_surname’,
106       :length => {
107         :name => 10,
108         :surname => 15
109       }
110
111     # Sort order (no MySQL support)
112     add_index :accounts, [:branch_id, :party_id, :surname],
113       :order => {:branch_id => :desc, :part_id => :asc}
114
115 ### In console
116 Use `ActiveRecord::Migration`.
117
118     ActiveRecord::Migration.add_index :posts, :slug
119
120 ### References
121
122  * http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_index