OSDN Git Service

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