/
Rails_Migrations_CLI.rb
122 lines (105 loc) · 2.71 KB
/
Rails_Migrations_CLI.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
cheatsheet do
title 'Rails Migrations CLI'
docset_file_name 'Rails_Migrations_CLI'
keyword 'railscli'
source_url 'http://cheat.kapeli.com'
introduction <<-'END'
Basic migration syntax:
```
rails generate migration AddPartNumberToProducts
```
It will generate a named migration:
```ruby
class AddPartNumberToProducts < ActiveRecord::Migration
def change
end
end
```
Below is the naming convention for generating code for migrations automatically.
END
category do
id 'Creating a Migration'
entry do
name 'AddXXXToYYY \<fields\>'
notes <<-'END'
```
rails generate migration AddPartNumberToProducts part_number:string
```
```ruby
class AddPartNumberToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
end
end
```
END
end
entry do
name 'RemoveXXXFromYYY \<fields\>'
notes <<-'END'
```
rails generate migration RemovePartNumberFromProducts part_number:string
```
```ruby
class RemovePartNumberFromProducts < ActiveRecord::Migration
def change
remove_column :products, :part_number, :string
end
end
```
END
end
entry do
name 'CreateXXX \<fields\>'
notes <<-'END'
```
rails generate migration CreateProducts name:string part_number:string
```
```ruby
class CreateProducts < ActiveRecord::Migration
def change
create_table :products do |t|
t.string :name
t.string :part_number
end
end
end
```
END
end
entry do
name 'AddXXXRefToYYY \<fields\>'
notes <<-'END'
```
rails generate migration AddUserRefToProducts user:references
```
```ruby
class AddUserRefToProducts < ActiveRecord::Migration
def change
add_reference :products, :user, index: true
end
end
```
END
end
entry do
name 'CreateJoinTableXXXYYY \<xxx\> \<yyy\>'
notes <<-'END'
```
rails g migration CreateJoinTableCustomerProduct customer product
```
```ruby
class CreateJoinTableCustomerProduct < ActiveRecord::Migration
def change
create_join_table :customers, :products do |t|
# t.index [:customer_id, :product_id]
# t.index [:product_id, :customer_id]
end
end
end
```
END
end
end
notes 'Based on http://guides.rubyonrails.org/migrations.html'
end