ridgepole 0.7.0.beta → 0.7.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 396b70e0da23ec61bcfcbf048b12ddce3b982372
4
- data.tar.gz: 42cd06dddcc39e058530cb7593bfa24163ad3252
3
+ metadata.gz: 2de5f94990fb6c37ed124d73e965aefb87b65bf8
4
+ data.tar.gz: ed39dba231ba693c1631ea4d94f5d321004773ce
5
5
  SHA512:
6
- metadata.gz: 055eff66d521e0a05b3d7bd5000833cd59abc9f93d363dd2dba91ace2cd0d5812c77fc197d5a0d16033d280d414a7da6974daf3b35af41f5981fb16a331656e4
7
- data.tar.gz: a2340a631b404bf1341c0a6d9de8350fdb221ac33492ba16b3d08161fc362ff46f9e838d502c1a161734e77424e2004761e1b5e85562cf2e514de60ecadae2ee
6
+ metadata.gz: 4909ca73aee2594f904db3fa9dba647f812746265f88e875f951733636c9fdb9a5dc8b2ad694774e30d51e1fea9701afe220e21a1a3766a03e76ddc797dd4af6
7
+ data.tar.gz: 1861b9094313e0e668687f27ab5854798a97db35136cd10c8d0ab9cd88397940d01fb333f0cebe421c6d1e2405d7b6a5028a8aa4cb0071c2a5ccfa2c91fd010b
data/README.md CHANGED
@@ -6,9 +6,11 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
6
6
  (like Chef/Puppet)
7
7
 
8
8
  [![Gem Version](https://badge.fury.io/rb/ridgepole.svg)](http://badge.fury.io/rb/ridgepole)
9
- [![Edge Version](https://img.shields.io/badge/edge%20version-0.7.0.beta-green.svg)](https://rubygems.org/gems/ridgepole/versions/0.7.0.beta)
10
- [![Build Status](https://travis-ci.org/winebarrel/ridgepole.svg?branch=master)](https://travis-ci.org/winebarrel/ridgepole)
11
- [![Coverage Status](https://coveralls.io/repos/winebarrel/ridgepole/badge.svg?branch=master)](https://coveralls.io/r/winebarrel/ridgepole?branch=master)
9
+ [![Build Status](https://travis-ci.org/winebarrel/ridgepole.svg?branch=0.6)](https://travis-ci.org/winebarrel/ridgepole)
10
+ [![Coverage Status](https://coveralls.io/repos/github/winebarrel/ridgepole/badge.svg?branch=0.6)](https://coveralls.io/github/winebarrel/ridgepole?branch=0.6)
11
+ [![Edge Version](https://img.shields.io/badge/edge%20version-0.7.0.beta-green.svg)](https://rubygems.org/gems/ridgepole/versions/0.7.0.beta2)
12
+ [![Build Status](https://travis-ci.org/winebarrel/ridgepole.svg?branch=0.7)](https://travis-ci.org/winebarrel/ridgepole)
13
+ [![Coverage Status](https://coveralls.io/repos/github/winebarrel/ridgepole/badge.svg?branch=0.7)](https://coveralls.io/github/winebarrel/ridgepole?branch=0.7)
12
14
 
13
15
  **Notice**
14
16
 
@@ -71,6 +73,7 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
71
73
  * Support MySQL JSON Type and Generated Columns
72
74
  * Add `--mysql-change-table-options` option
73
75
  * Pass config from env
76
+ * Fix change fk order
74
77
 
75
78
  ## Installation
76
79
 
@@ -22,10 +22,11 @@ class Ridgepole::Delta
22
22
 
23
23
  def script
24
24
  buf = StringIO.new
25
- buf_for_fk = StringIO.new
25
+ pre_buf_for_fk = StringIO.new
26
+ post_buf_for_fk = StringIO.new
26
27
 
27
28
  (@delta[:add] || {}).each do |table_name, attrs|
28
- append_create_table(table_name, attrs, buf, buf_for_fk)
29
+ append_create_table(table_name, attrs, buf, post_buf_for_fk)
29
30
  end
30
31
 
31
32
  (@delta[:rename] || {}).each do |table_name, attrs|
@@ -33,14 +34,18 @@ class Ridgepole::Delta
33
34
  end
34
35
 
35
36
  (@delta[:change] || {}).each do |table_name, attrs|
36
- append_change(table_name, attrs, buf, buf_for_fk)
37
+ append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
37
38
  end
38
39
 
39
40
  (@delta[:delete] || {}).each do |table_name, attrs|
40
41
  append_drop_table(table_name, attrs, buf)
41
42
  end
42
43
 
43
- (buf.string.strip + "\n\n" + buf_for_fk.string.strip).strip
44
+ [
45
+ pre_buf_for_fk,
46
+ buf,
47
+ post_buf_for_fk,
48
+ ].map {|b| b.string.strip }.join("\n\n").strip
44
49
  end
45
50
 
46
51
  def differ?
@@ -211,7 +216,7 @@ class Ridgepole::Delta
211
216
  end
212
217
  end
213
218
 
214
- def append_create_table(table_name, attrs, buf, buf_for_fk)
219
+ def append_create_table(table_name, attrs, buf, post_buf_for_fk)
215
220
  options = attrs[:options] || {}
216
221
  options[:options] ||= @options[:table_options] if @options[:table_options]
217
222
  definition = attrs[:definition] || {}
@@ -245,12 +250,12 @@ end
245
250
 
246
251
  unless (foreign_keys = attrs[:foreign_keys] || {}).empty?
247
252
  foreign_keys.each do |_, foreign_key_attrs|
248
- append_add_foreign_key(table_name, foreign_key_attrs, buf_for_fk, @options)
253
+ append_add_foreign_key(table_name, foreign_key_attrs, post_buf_for_fk, @options)
249
254
  end
250
255
  end
251
256
 
252
257
  buf.puts
253
- buf_for_fk.puts
258
+ post_buf_for_fk.puts
254
259
  end
255
260
 
256
261
  def append_rename_table(to_table_name, from_table_name, buf)
@@ -278,7 +283,7 @@ execute "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name
278
283
  buf.puts
279
284
  end
280
285
 
281
- def append_change(table_name, attrs, buf, buf_for_fk)
286
+ def append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
282
287
  definition = attrs[:definition] || {}
283
288
  indices = attrs[:indices] || {}
284
289
  foreign_keys = attrs[:foreign_keys] || {}
@@ -292,7 +297,7 @@ execute "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name
292
297
  end
293
298
 
294
299
  unless foreign_keys.empty?
295
- append_change_foreign_keys(table_name, foreign_keys, buf_for_fk, @options)
300
+ append_change_foreign_keys(table_name, foreign_keys, pre_buf_for_fk, post_buf_for_fk, @options)
296
301
  end
297
302
 
298
303
  if table_options
@@ -300,7 +305,8 @@ execute "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name
300
305
  end
301
306
 
302
307
  buf.puts
303
- buf_for_fk.puts
308
+ pre_buf_for_fk.puts
309
+ post_buf_for_fk.puts
304
310
  end
305
311
 
306
312
  def append_change_table(table_name, buf)
@@ -428,13 +434,13 @@ remove_index(#{table_name.inspect}, #{target.inspect})
428
434
  end
429
435
  end
430
436
 
431
- def append_change_foreign_keys(table_name, delta, buf, options)
437
+ def append_change_foreign_keys(table_name, delta, pre_buf_for_fk, post_buf_for_fk, options)
432
438
  (delta[:delete] || {}).each do |_, attrs|
433
- append_remove_foreign_key(table_name, attrs, buf, options)
439
+ append_remove_foreign_key(table_name, attrs, pre_buf_for_fk, options)
434
440
  end
435
441
 
436
442
  (delta[:add] || {}).each do |_, attrs|
437
- append_add_foreign_key(table_name, attrs, buf, options)
443
+ append_add_foreign_key(table_name, attrs, post_buf_for_fk, options)
438
444
  end
439
445
  end
440
446
 
@@ -1,3 +1,3 @@
1
1
  module Ridgepole
2
- VERSION = '0.7.0.beta'
2
+ VERSION = '0.7.0.beta2'
3
3
  end
@@ -110,4 +110,86 @@ describe 'Ridgepole::Client#diff -> migrate' do
110
110
  expect(subject.dump).to match_fuzzy expected_dsl
111
111
  }
112
112
  end
113
+
114
+ context 'when drop/add fk with parent table' do
115
+ let(:actual_dsl) {
116
+ erbh(<<-EOS)
117
+ create_table "child", force: :cascade do |t|
118
+ t.integer "parent_id"
119
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
120
+ end
121
+
122
+ create_table "parent", <%= i cond(5.1, id: :integer) %>, force: :cascade do |t|
123
+ end
124
+
125
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
126
+ EOS
127
+ }
128
+
129
+ let(:expected_dsl) {
130
+ erbh(<<-EOS)
131
+ create_table "child", force: :cascade do |t|
132
+ t.integer "parent2_id"
133
+ t.index ["parent2_id"], name: "par2_id", <%= i cond(5.0, using: :btree) %>
134
+ end
135
+
136
+ create_table "parent2", <%= i cond(5.1, id: :integer) %>, force: :cascade do |t|
137
+ end
138
+
139
+ add_foreign_key "child", "parent2", name: "child_ibfk_2"
140
+ EOS
141
+ }
142
+
143
+ before { subject.diff(actual_dsl).migrate }
144
+ subject { client }
145
+
146
+ it {
147
+ delta = subject.diff(expected_dsl)
148
+ expect(delta.differ?).to be_truthy
149
+ expect(subject.dump).to match_fuzzy actual_dsl
150
+ delta.migrate
151
+ expect(subject.dump).to match_fuzzy expected_dsl
152
+ }
153
+ end
154
+
155
+ context 'when drop/add fk with parent table without name' do
156
+ let(:actual_dsl) {
157
+ erbh(<<-EOS)
158
+ create_table "child", force: :cascade do |t|
159
+ t.integer "parent_id"
160
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
161
+ end
162
+
163
+ create_table "parent", <%= i cond(5.1, id: :integer) %>, force: :cascade do |t|
164
+ end
165
+
166
+ add_foreign_key "child", "parent"
167
+ EOS
168
+ }
169
+
170
+ let(:expected_dsl) {
171
+ erbh(<<-EOS)
172
+ create_table "child", force: :cascade do |t|
173
+ t.integer "parent2_id"
174
+ t.index ["parent2_id"], name: "par2_id", <%= i cond(5.0, using: :btree) %>
175
+ end
176
+
177
+ create_table "parent2", <%= i cond(5.1, id: :integer) %>, force: :cascade do |t|
178
+ end
179
+
180
+ add_foreign_key "child", "parent2"
181
+ EOS
182
+ }
183
+
184
+ before { subject.diff(actual_dsl).migrate }
185
+ subject { client }
186
+
187
+ it {
188
+ delta = subject.diff(expected_dsl)
189
+ expect(delta.differ?).to be_truthy
190
+ expect(subject.dump).to match_fuzzy actual_dsl
191
+ delta.migrate
192
+ expect(subject.dump).to match_fuzzy expected_dsl
193
+ }
194
+ end
113
195
  end
@@ -208,4 +208,76 @@ describe 'Ridgepole::Client#diff -> migrate' do
208
208
  expect(subject.dump).to match_fuzzy ''
209
209
  }
210
210
  end
211
+
212
+ context 'when drop fk with parent table' do
213
+ let(:actual_dsl) {
214
+ erbh(<<-EOS)
215
+ create_table "child", force: :cascade do |t|
216
+ t.integer "parent_id"
217
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
218
+ end
219
+
220
+ create_table "parent", <%= i cond(5.1, id: :integer) %>, force: :cascade do |t|
221
+ end
222
+
223
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
224
+ EOS
225
+ }
226
+
227
+ let(:expected_dsl) {
228
+ erbh(<<-EOS)
229
+ create_table "child", force: :cascade do |t|
230
+ t.integer "parent_id"
231
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
232
+ end
233
+ EOS
234
+ }
235
+
236
+ before { subject.diff(actual_dsl).migrate }
237
+ subject { client }
238
+
239
+ it {
240
+ delta = subject.diff(expected_dsl)
241
+ expect(delta.differ?).to be_truthy
242
+ expect(subject.dump).to match_fuzzy actual_dsl
243
+ delta.migrate
244
+ expect(subject.dump).to match_fuzzy expected_dsl
245
+ }
246
+ end
247
+
248
+ context 'when drop fk with parent table without name' do
249
+ let(:actual_dsl) {
250
+ erbh(<<-EOS)
251
+ create_table "child", force: :cascade do |t|
252
+ t.integer "parent_id"
253
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
254
+ end
255
+
256
+ create_table "parent", <%= i cond(5.1, id: :integer) %>, force: :cascade do |t|
257
+ end
258
+
259
+ add_foreign_key "child", "parent"
260
+ EOS
261
+ }
262
+
263
+ let(:expected_dsl) {
264
+ erbh(<<-EOS)
265
+ create_table "child", force: :cascade do |t|
266
+ t.integer "parent_id"
267
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
268
+ end
269
+ EOS
270
+ }
271
+
272
+ before { subject.diff(actual_dsl).migrate }
273
+ subject { client }
274
+
275
+ it {
276
+ delta = subject.diff(expected_dsl)
277
+ expect(delta.differ?).to be_truthy
278
+ expect(subject.dump).to match_fuzzy actual_dsl
279
+ delta.migrate
280
+ expect(subject.dump).to match_fuzzy expected_dsl
281
+ }
282
+ end
211
283
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ridgepole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0.beta
4
+ version: 0.7.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara