ridgepole 0.8.3 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a86332195811295a61f945779cad2b09c6fd8b01df116e505f89995c576f978c
4
- data.tar.gz: 491952fd6d83ffb361bbae4153b0fff3d216512948ed0f6b521e408375f898ab
3
+ metadata.gz: 8a09a114ad24ead5072a479593d757ea2dad1d0f42ebf9ae84febad36db8ba78
4
+ data.tar.gz: e4e14f15a869ac1b66cff3b869f4ca5d246975e4b62e4a8f5e8ecf040efe9d9c
5
5
  SHA512:
6
- metadata.gz: e7868cb379bb0329e9efef9a5402c9ef5f369740aa4cd1ff68e80798582fbf32bdb6b15840e9866c0dff7aa3fe903aab8f7d7b07359137072face3c462407698
7
- data.tar.gz: 94a72fd2146fbcdaa81c235916b3445c13d8eb3297b5c686c011938f6918d85383fb44b00513c52e96fca78b34b1a7b035558d03f36991e1cc0d277f9aa75ebe
6
+ metadata.gz: a664d190502ef67c91b9dc96f95cf4289812195939f8193354fea005de1bb83a31e479758dc0c64115dae02b6c7d62588fcd89f9129ebc2664c0bf8eef5906c1
7
+ data.tar.gz: ac81928fe43c71c13542686361af7d54868782728a1d02731f431827dba66204098baf6b2a674cf817b239f285b94b5e2edf48d842997e7cdf47b238234aac71
data/README.md CHANGED
@@ -107,6 +107,8 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
107
107
  * Support `postgres://` schema ([pull#285](https://github.com/winebarrel/ridgepole/pull/285))
108
108
  * `>= 0.8.3`
109
109
  * Fix "topological sort failed" error ([pull#287](https://github.com/winebarrel/ridgepole/pull/287))
110
+ * `>= 0.8.4`
111
+ * Display a warning if an InnoDB table doesn't have any indexes on a column where it has a foreign key ([pull#290](https://github.com/winebarrel/ridgepole/pull/290))
110
112
  </details>
111
113
 
112
114
  ## Installation
@@ -191,7 +191,7 @@ module Ridgepole
191
191
  errmsg = lines.with_index.map do |l, i|
192
192
  line_num = i + 1
193
193
  prefix = line_num == err_num ? '* ' : ' '
194
- format("#{prefix}%*d: #{l}", digit_number, line_num)
194
+ format("#{prefix}%<line_num>#{digit_number}d: #{l}", line_num: line_num)
195
195
  end
196
196
 
197
197
  if err_num > 0
@@ -565,10 +565,10 @@ module Ridgepole
565
565
  child_label = "#{child_table}.#{column_name}"
566
566
  label_len = [parent_label.length, child_label.length].max
567
567
 
568
- @logger.warn(format(<<-MSG, label_len, parent_label, label_len, child_label))
568
+ @logger.warn(format(<<-MSG, parent_label: parent_label, child_label: child_label))
569
569
  [WARNING] Relation column type is different.
570
- %*s: #{parent_column_info}
571
- %*s: #{child_column_info}
570
+ %<parent_label>#{label_len}s: #{parent_column_info}
571
+ %<child_label>#{label_len}s: #{child_column_info}
572
572
  MSG
573
573
  end
574
574
  end
@@ -8,22 +8,40 @@ module Ridgepole
8
8
 
9
9
  def parse(dsl, opts = {})
10
10
  definition, execute = Context.eval(dsl, opts)
11
- check_orphan_index(definition)
12
- check_orphan_foreign_key(definition)
11
+ check_definition(definition)
13
12
  [definition, execute]
14
13
  end
15
14
 
16
15
  private
17
16
 
18
- def check_orphan_index(definition)
17
+ def check_definition(definition)
19
18
  definition.each do |table_name, attrs|
20
- raise "Table `#{table_name}` to create the index is not defined: #{attrs[:indices].keys.join(',')}" if attrs[:indices] && !(attrs[:definition])
19
+ check_orphan_index(table_name, attrs)
20
+ check_orphan_foreign_key(table_name, attrs)
21
+ check_foreign_key_without_index(table_name, attrs)
21
22
  end
22
23
  end
23
24
 
24
- def check_orphan_foreign_key(definition)
25
- definition.each do |table_name, attrs|
26
- raise "Table `#{table_name}` to create the foreign key is not defined: #{attrs[:foreign_keys].keys.join(',')}" if attrs[:foreign_keys] && !(attrs[:definition])
25
+ def check_orphan_index(table_name, attrs)
26
+ raise "Table `#{table_name}` to create the index is not defined: #{attrs[:indices].keys.join(',')}" if attrs[:indices] && !(attrs[:definition])
27
+ end
28
+
29
+ def check_orphan_foreign_key(table_name, attrs)
30
+ raise "Table `#{table_name}` to create the foreign key is not defined: #{attrs[:foreign_keys].keys.join(',')}" if attrs[:foreign_keys] && !(attrs[:definition])
31
+ end
32
+
33
+ def check_foreign_key_without_index(table_name, attrs)
34
+ return unless attrs[:foreign_keys]
35
+ return unless attrs[:options][:options]&.include?('ENGINE=InnoDB')
36
+
37
+ attrs[:foreign_keys].each do |_, foreign_key_attrs|
38
+ fk_index = foreign_key_attrs[:options][:column] || "#{foreign_key_attrs[:to_table].singularize}_id"
39
+ next if attrs[:indices]&.any? { |_k, v| v[:column_name].first == fk_index }
40
+
41
+ Ridgepole::Logger.instance.warn(<<-MSG)
42
+ [WARNING] Table `#{table_name}` has a foreign key on `#{fk_index}` column, but doesn't have any indexes on the column.
43
+ Although an index will be added automatically by InnoDB, please add an index explicitly for your future operations.
44
+ MSG
27
45
  end
28
46
  end
29
47
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ridgepole
4
- VERSION = '0.8.3'
4
+ VERSION = '0.8.4'
5
5
  end
@@ -169,4 +169,57 @@ describe 'Ridgepole::Client#diff -> migrate' do
169
169
  end.to raise_error('Table `child` to create the foreign key is not defined: child_ibfk_1')
170
170
  }
171
171
  end
172
+
173
+ context 'when create fk without any indexes for its column' do
174
+ let(:dsl) do
175
+ erbh(<<-ERB)
176
+ create_table "parent", <%= i cond('>= 5.1',id: :integer) %>, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
177
+ end
178
+
179
+ create_table "child", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
180
+ t.integer "parent_id"
181
+ end
182
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
183
+ ERB
184
+ end
185
+
186
+ subject { client(dump_without_table_options: false) }
187
+
188
+ it {
189
+ expect(Ridgepole::Logger.instance).to receive(:warn).with(<<-MSG).twice
190
+ [WARNING] Table `child` has a foreign key on `parent_id` column, but doesn't have any indexes on the column.
191
+ Although an index will be added automatically by InnoDB, please add an index explicitly for your future operations.
192
+ MSG
193
+ subject.diff(dsl).migrate
194
+
195
+ expect do
196
+ subject.diff(dsl).migrate
197
+ end.to raise_error(/Mysql2::Error: Cannot drop index/)
198
+ }
199
+ end
200
+
201
+ context 'when create fk with first key of multiple column indexes for its column' do
202
+ let(:dsl) do
203
+ erbh(<<-ERB)
204
+ create_table "parent", <%= i cond('>= 5.1',id: :integer) %>, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
205
+ end
206
+
207
+ create_table "child", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
208
+ t.integer "parent_id"
209
+ t.string "name"
210
+ t.index ["parent_id", "name"], name: "par_id", <%= i cond(5.0, using: :btree) %>
211
+ end
212
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
213
+ ERB
214
+ end
215
+
216
+ subject { client(dump_without_table_options: false) }
217
+
218
+ it {
219
+ expect(Ridgepole::Logger.instance).to_not receive(:warn)
220
+ subject.diff(dsl).migrate
221
+
222
+ expect { subject.diff(dsl).migrate }.to_not raise_error
223
+ }
224
+ end
172
225
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ridgepole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-17 00:00:00.000000000 Z
11
+ date: 2019-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord