ridgepole 0.8.7 → 0.8.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +63 -0
  3. data/.rubocop.yml +11 -2
  4. data/README.md +14 -2
  5. data/lib/ridgepole/delta.rb +28 -21
  6. data/lib/ridgepole/diff.rb +12 -0
  7. data/lib/ridgepole/dsl_parser.rb +3 -4
  8. data/lib/ridgepole/dsl_parser/context.rb +2 -3
  9. data/lib/ridgepole/dsl_parser/table_definition.rb +7 -0
  10. data/lib/ridgepole/ext/abstract_mysql_adapter/use_alter_index.rb +1 -1
  11. data/lib/ridgepole/external_sql_executer.rb +1 -1
  12. data/lib/ridgepole/version.rb +1 -1
  13. data/ridgepole.gemspec +1 -1
  14. data/spec/erb_helper.rb +4 -4
  15. data/spec/mysql/collation/collation_spec.rb +6 -0
  16. data/spec/mysql/diff/diff2_spec.rb +3 -3
  17. data/spec/mysql/diff/diff_spec.rb +3 -3
  18. data/spec/mysql/fk/migrate_create_fk_spec.rb +79 -10
  19. data/spec/mysql/fk/migrate_drop_fk_spec.rb +1 -1
  20. data/spec/mysql/fk/migrate_fk_with_column_spec.rb +2 -2
  21. data/spec/mysql/migrate/migrate_add_column_spec.rb +5 -5
  22. data/spec/mysql/migrate/migrate_change_column3_spec.rb +109 -18
  23. data/spec/mysql/migrate/migrate_change_column_spec.rb +5 -5
  24. data/spec/mysql/migrate/migrate_change_index_spec.rb +9 -9
  25. data/spec/mysql/migrate/migrate_create_index_spec.rb +6 -6
  26. data/spec/mysql/migrate/migrate_create_table_with_index_spec.rb +7 -7
  27. data/spec/mysql/migrate/migrate_create_table_with_options_spec.rb +8 -8
  28. data/spec/mysql/migrate/migrate_drop_column_and_index_spec.rb +4 -4
  29. data/spec/mysql/migrate/migrate_drop_column_spec.rb +3 -3
  30. data/spec/mysql/migrate/migrate_drop_index_spec.rb +6 -6
  31. data/spec/mysql/migrate/migrate_rename_column_spec.rb +3 -3
  32. data/spec/mysql/migrate/migrate_script_error_spec.rb +1 -1
  33. data/spec/mysql/text_blob_types/text_blob_types_spec.rb +4 -1
  34. data/spec/mysql/~default_name_fk/migrate_create_fk_spec.rb +1 -1
  35. data/spec/mysql/~default_name_fk/migrate_drop_fk_spec.rb +1 -1
  36. data/spec/mysql/~dump_auto_increment/migrate_create_table_with_index_spec.rb +7 -7
  37. data/spec/postgresql/diff/diff_spec.rb +2 -2
  38. data/spec/postgresql/fk/migrate_create_fk_spec.rb +2 -2
  39. data/spec/postgresql/fk/migrate_drop_fk_spec.rb +1 -1
  40. data/spec/postgresql/migrate/migrate_add_column_spec.rb +5 -5
  41. data/spec/postgresql/migrate/migrate_change_column_spec.rb +4 -4
  42. data/spec/postgresql/migrate/migrate_change_index_spec.rb +9 -9
  43. data/spec/postgresql/migrate/migrate_drop_column_spec.rb +3 -3
  44. data/spec/postgresql/migrate/migrate_drop_column_with_index_spec.rb +5 -5
  45. data/spec/postgresql/migrate/migrate_drop_expression_index_spec.rb +1 -1
  46. data/spec/postgresql/migrate/migrate_drop_index_spec.rb +6 -6
  47. data/spec/postgresql/migrate/migrate_rename_column_spec.rb +2 -2
  48. data/spec/postgresql/~default_name_fk/migrate_create_fk_spec.rb +1 -1
  49. data/spec/postgresql/~default_name_fk/migrate_drop_fk_spec.rb +1 -1
  50. data/spec/spec_condition.rb +4 -4
  51. data/spec/spec_helper.rb +2 -1
  52. metadata +7 -7
  53. data/.travis.yml +0 -44
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1663947686d2cff34fc79b5f06e6b9df2568b6c52f4e37be19a55dd3881f02f6
4
- data.tar.gz: '0656469c72c0085c89fbdf29e180da8dca898e3a1d80d415cb0e3b2f03fb808f'
3
+ metadata.gz: 91c51bf51596b61eff43704847bd41b9f8035ceafd1f1e05e1cd72fc6632e582
4
+ data.tar.gz: 32aff893209c48b839286b7efe24d465e3dd808d3b8820f52a5fb4eb9ecbf606
5
5
  SHA512:
6
- metadata.gz: da61fdbb7da55e9dd9d8fb7333f267292183397dc6454b246c3b6e5ba20b452213db281ad82a23547b814ede7b3f22aa5c9b1c3800b6357dd44ef4c038bf99a0
7
- data.tar.gz: 28c19d1c188746f28be8ea271193946eb9f19107adb81a92cf461af416b661df5babe75ed06a74234368fdd24b18f914ec0bdf3ff8b0e4b5b145fbd186f572bb
6
+ metadata.gz: 92c24eca95293b83ab73871c9baaff349e8d28be8ddc5583746d3beecf0b6986eb58127361654fc0742c95b68f9ac2c060ca689af98a9ca6558b78c960b2a35b
7
+ data.tar.gz: 6864c16bbd8723436219540b7a3ab0f5eccfb32ae5301ac827957b9671c8cf83d7a91ea13a9a364f3b94ef236d2b3334742adce0524f7cb22801d298757cb0cd
@@ -0,0 +1,63 @@
1
+ name: test
2
+
3
+ on:
4
+ push:
5
+ pull_request:
6
+
7
+ jobs:
8
+ build:
9
+ runs-on: ubuntu-latest
10
+ strategy:
11
+ fail-fast: false
12
+ matrix:
13
+ ruby:
14
+ - 2.4
15
+ - 2.5
16
+ - 2.6
17
+ - 2.7
18
+ env:
19
+ - MYSQL56=1
20
+ - MYSQL57=1
21
+ - POSTGRESQL=1
22
+ gemfile:
23
+ - gemfiles/activerecord_5.0.gemfile
24
+ - gemfiles/activerecord_5.1.gemfile
25
+ - gemfiles/activerecord_5.2.gemfile
26
+ - gemfiles/activerecord_6.0.gemfile
27
+ exclude:
28
+ - ruby: 2.4
29
+ gemfile: gemfiles/activerecord_6.0.gemfile
30
+
31
+ steps:
32
+ - uses: actions/checkout@v2
33
+ - uses: actions/setup-ruby@v1
34
+ with:
35
+ ruby-version: ${{ matrix.ruby }}
36
+ - uses: actions/cache@v2
37
+ with:
38
+ path: gemfiles/vendor/bundle
39
+ key: ${{ runner.os }}-gems-${{ matrix.ruby }}-${{ matrix.gemfile }}-${{ hashFiles('ridgepole.gemspec', '**/Gemfile', '${{ matrix.gemfile }}') }}
40
+ restore-keys: |
41
+ ${{ runner.os }}-gems-${{ matrix.ruby }}-${{ matrix.gemfile }}-
42
+
43
+ - name: Setup dependencies
44
+ run: |
45
+ for i in {1..60}; do docker-compose up -d && break; sleep 1; done
46
+
47
+ gem install bundler
48
+ bundle config path vendor/bundle
49
+ bundle install --jobs 4 --retry 3
50
+
51
+ # Wait until database servers start
52
+ function mysql_ping { mysqladmin -u root -h 127.0.0.1 -P 13316 -ppassword ping; }
53
+ function mysql57_ping { mysqladmin -u root -h 127.0.0.1 -P 13317 -ppassword ping; }
54
+ function pg_ping { PGPASSWORD=password pg_isready -U postgres -h 127.0.0.1 -p 15442; }
55
+ for i in {1..60}; do mysql_ping && break; sleep 1; done
56
+ for i in {1..60}; do mysql57_ping && break; sleep 1; done
57
+ for i in {1..60}; do pg_ping && break; sleep 1; done
58
+ env:
59
+ BUNDLE_GEMFILE: ${{ matrix.gemfile }}
60
+
61
+ - run: ${{ matrix.env }} bundle exec rake
62
+ env:
63
+ BUNDLE_GEMFILE: ${{ matrix.gemfile }}
@@ -3,6 +3,7 @@ AllCops:
3
3
  - 'gemfiles/**/*'
4
4
  - 'omnibus-ridgepole/**/*'
5
5
  TargetRubyVersion: 2.4
6
+ NewCops: enable
6
7
  Bundler/OrderedGems:
7
8
  Include:
8
9
  - 'Appraisals'
@@ -30,8 +31,6 @@ Style/Documentation:
30
31
  Enabled: false
31
32
  Style/GuardClause:
32
33
  Enabled: false
33
- Style/MethodMissingSuper:
34
- Enabled: false
35
34
  Style/MixinUsage:
36
35
  Exclude:
37
36
  - 'spec/**/*'
@@ -41,3 +40,13 @@ Layout/ClosingHeredocIndentation:
41
40
  Enabled: false
42
41
  Style/NumericPredicate:
43
42
  Enabled: false
43
+ Lint/MissingSuper:
44
+ Enabled: false
45
+ Style/StringConcatenation:
46
+ Enabled: false
47
+ Style/SoleNestedConditional:
48
+ Enabled: false
49
+ Lint/DuplicateBranch:
50
+ Enabled: false
51
+ Style/OptionalBooleanParameter:
52
+ Enabled: false
data/README.md CHANGED
@@ -6,7 +6,7 @@ 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
- [![Build Status](https://travis-ci.org/winebarrel/ridgepole.svg?branch=0.8)](https://travis-ci.org/winebarrel/ridgepole)
9
+ [![Build Status](https://github.com/winebarrel/ridgepole/workflows/test/badge.svg?branch=0.8)](https://github.com/winebarrel/ridgepole/actions)
10
10
  [![Coverage Status](https://coveralls.io/repos/github/winebarrel/ridgepole/badge.svg?branch=0.8)](https://coveralls.io/github/winebarrel/ridgepole?branch=0.8)
11
11
 
12
12
  <details><summary>ChangeLog</summary>
@@ -115,6 +115,18 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
115
115
  * Support multiple databases feature ([pull#297](https://github.com/winebarrel/ridgepole/pull/297))
116
116
  * `>= 0.8.7`
117
117
  * Support `require_relative` ([pull#298](https://github.com/winebarrel/ridgepole/pull/298))
118
+ * `>= 0.8.8`
119
+ * Fix keyword arguments warnings in Ruby 2.7 ([pull#303](https://github.com/winebarrel/ridgepole/pull/303))
120
+ * `>= 0.8.9`
121
+ * Fix unexpected differences on text types and blob types on Rails 6 ([pull#306](https://github.com/winebarrel/ridgepole/pull/306))
122
+ * Fix unexpected warning when a foreign key is added on the primary key ([pull#307](https://github.com/winebarrel/ridgepole/pull/307))
123
+ * `>= 0.8.10`
124
+ * Raise an error if an InnoDB column has a foreign key but no index ([pull#310](https://github.com/winebarrel/ridgepole/pull/310))
125
+ * `>= 0.8.11`
126
+ * Fix FK index check support multiple PK ([pull#315](https://github.com/winebarrel/ridgepole/pull/315))
127
+ * Support t.reference() foreign_key option ([pull#316](https://github.com/winebarrel/ridgepole/pull/316))
128
+ * `>= 0.8.12`
129
+ * Pluralize column specified by `references` ([pull#317](https://github.com/winebarrel/ridgepole/pull/317))
118
130
  </details>
119
131
 
120
132
  ## Installation
@@ -193,6 +205,7 @@ Usage: ridgepole [options]
193
205
  --ignore-table-comment
194
206
  --skip-column-comment-change
195
207
  --create-table-with-index
208
+ --allow-pk-change
196
209
  --mysql-dump-auto-increment
197
210
  -r, --require LIBS
198
211
  --log-file LOG_FILE
@@ -309,7 +322,6 @@ end
309
322
  ```
310
323
 
311
324
  ## Collation/Charset
312
- You can use the column collation by passing `--enable-mysql-awesome` ([activerecord-mysql-awesome](https://github.com/kamipo/activerecord-mysql-awesome) is required)
313
325
 
314
326
  ```ruby
315
327
  create_table "articles", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
@@ -326,7 +326,7 @@ execute "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name
326
326
  end
327
327
 
328
328
  def append_change_table(table_name, buf)
329
- buf.puts "change_table(#{table_name.inspect}, {:bulk => true}) do |t|" if @options[:bulk_change]
329
+ buf.puts "change_table(#{table_name.inspect}, bulk: true) do |t|" if @options[:bulk_change]
330
330
  yield
331
331
  buf.puts 'end' if @options[:bulk_change]
332
332
  end
@@ -429,11 +429,11 @@ remove_column(#{table_name.inspect}, #{column_name.inspect})
429
429
 
430
430
  if force_bulk_change || @options[:bulk_change]
431
431
  buf.puts(<<-RUBY)
432
- t.index(#{column_name.inspect}, #{options.inspect})
432
+ t.index(#{column_name.inspect}, **#{options.inspect})
433
433
  RUBY
434
434
  else
435
435
  buf.puts(<<-RUBY)
436
- add_index(#{table_name.inspect}, #{column_name.inspect}, #{options.inspect})
436
+ add_index(#{table_name.inspect}, #{column_name.inspect}, **#{options.inspect})
437
437
  RUBY
438
438
  end
439
439
  end
@@ -441,15 +441,20 @@ add_index(#{table_name.inspect}, #{column_name.inspect}, #{options.inspect})
441
441
  def append_remove_index(table_name, _index_name, attrs, buf)
442
442
  column_name = attrs.fetch(:column_name)
443
443
  options = attrs[:options] || {}
444
- target = options[:name] ? { name: options[:name] } : column_name
444
+ target =
445
+ if options[:name]
446
+ "name: #{options[:name].inspect}"
447
+ else
448
+ column_name.inspect
449
+ end
445
450
 
446
451
  if @options[:bulk_change]
447
452
  buf.puts(<<-RUBY)
448
- t.remove_index(#{target.inspect})
453
+ t.remove_index(#{target})
449
454
  RUBY
450
455
  else
451
456
  buf.puts(<<-RUBY)
452
- remove_index(#{table_name.inspect}, #{target.inspect})
457
+ remove_index(#{table_name.inspect}, #{target})
453
458
  RUBY
454
459
  end
455
460
  end
@@ -469,7 +474,7 @@ remove_index(#{table_name.inspect}, #{target.inspect})
469
474
  attrs_options = attrs[:options] || {}
470
475
 
471
476
  buf.puts(<<-RUBY)
472
- add_foreign_key(#{table_name.inspect}, #{to_table.inspect}, #{attrs_options.inspect})
477
+ add_foreign_key(#{table_name.inspect}, #{to_table.inspect}, **#{attrs_options.inspect})
473
478
  RUBY
474
479
  end
475
480
 
@@ -478,13 +483,13 @@ add_foreign_key(#{table_name.inspect}, #{to_table.inspect}, #{attrs_options.insp
478
483
  fk_name = attrs_options[:name]
479
484
 
480
485
  target = if fk_name
481
- { name: fk_name }
486
+ "name: #{fk_name.inspect}"
482
487
  else
483
- attrs.fetch(:to_table)
488
+ attrs.fetch(:to_table).inspect
484
489
  end
485
490
 
486
491
  buf.puts(<<-RUBY)
487
- remove_foreign_key(#{table_name.inspect}, #{target.inspect})
492
+ remove_foreign_key(#{table_name.inspect}, #{target})
488
493
  RUBY
489
494
  end
490
495
 
@@ -500,17 +505,19 @@ remove_foreign_key(#{table_name.inspect}, #{target.inspect})
500
505
  def inspect_options_include_default_proc(options)
501
506
  options = options.dup
502
507
 
503
- if options[:default].is_a?(Proc)
504
- proc_default = options.delete(:default)
505
- proc_default = ":default=>proc{#{proc_default.call.inspect}}"
506
- options_inspect = options.inspect
507
- options_inspect.sub!(/\}\z/, '')
508
- options_inspect << ', ' if options_inspect !~ /\{\z/
509
- options_inspect << proc_default << '}'
510
- options_inspect
511
- else
512
- options.inspect
513
- end
508
+ kwargs =
509
+ if options[:default].is_a?(Proc)
510
+ proc_default = options.delete(:default)
511
+ proc_default = ":default=>proc{#{proc_default.call.inspect}}"
512
+ options_inspect = options.inspect
513
+ options_inspect.sub!(/\}\z/, '')
514
+ options_inspect << ', ' if options_inspect !~ /\{\z/
515
+ options_inspect << proc_default << '}'
516
+ options_inspect
517
+ else
518
+ options.inspect
519
+ end
520
+ "**#{kwargs}"
514
521
  end
515
522
  end
516
523
  end
@@ -386,6 +386,18 @@ module Ridgepole
386
386
  attrs[:type] = :bigint
387
387
  opts.delete(:limit)
388
388
  end
389
+
390
+ if opts[:size] && (attrs[:type] == :text || attrs[:type] == :blob || attrs[:type] == :binary)
391
+ case opts.delete(:size)
392
+ when :tiny
393
+ attrs[:type] = :blob if attrs[:type] == :binary
394
+ opts[:limit] = 255
395
+ when :medium
396
+ opts[:limit] = 16_777_215
397
+ when :long
398
+ opts[:limit] = 4_294_967_295
399
+ end
400
+ end
389
401
  end
390
402
  end
391
403
 
@@ -37,11 +37,10 @@ module Ridgepole
37
37
  attrs[:foreign_keys].each do |_, foreign_key_attrs|
38
38
  fk_index = foreign_key_attrs[:options][:column] || "#{foreign_key_attrs[:to_table].singularize}_id"
39
39
  next if attrs[:indices]&.any? { |_k, v| v[:column_name].first == fk_index }
40
+ # NOTE: For composite primary keys, the first column of the primary key is used as the foreign key index
41
+ next if Array(attrs[:options][:primary_key]).first == fk_index
40
42
 
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
43
+ raise "The column `#{fk_index}` of the table `#{table_name}` has a foreign key but no index. Although InnoDB creates an index automatically, please add one explicitly in order for ridgepole to manage it."
45
44
  end
46
45
  end
47
46
  end
@@ -3,8 +3,7 @@
3
3
  module Ridgepole
4
4
  class DSLParser
5
5
  class Context
6
- attr_reader :__definition
7
- attr_reader :__execute
6
+ attr_reader :__definition, :__execute
8
7
 
9
8
  def initialize(opts = {})
10
9
  @__working_dir = File.expand_path(opts[:path] ? File.dirname(opts[:path]) : Dir.pwd)
@@ -28,7 +27,7 @@ module Ridgepole
28
27
  table_name = table_name.to_s
29
28
  table_definition = TableDefinition.new(table_name, self)
30
29
 
31
- options[:primary_key] = options[:primary_key].to_s if options[:primary_key]&.is_a?(Symbol)
30
+ options[:primary_key] = options[:primary_key].to_s if options[:primary_key].is_a?(Symbol)
32
31
  if options[:id] && TableDefinition::ALIAS_TYPES.key?(options[:id])
33
32
  type, type_default_opts = TableDefinition::ALIAS_TYPES[options[:id]]
34
33
  options[:id] = type
@@ -131,6 +131,7 @@ module Ridgepole
131
131
  polymorphic_options.merge!(options.slice(:null, :first, :after))
132
132
  index_options = options.key?(:index) ? options.delete(:index) : true
133
133
  type = options.delete(:type) || DEFAULT_PRIMARY_KEY_TYPE
134
+ foreign_key_options = options.delete(:foreign_key)
134
135
 
135
136
  args.each do |col|
136
137
  column("#{col}_id", type, options)
@@ -139,6 +140,12 @@ module Ridgepole
139
140
  columns = polymorphic ? ["#{col}_type", "#{col}_id"] : ["#{col}_id"]
140
141
  index(columns, index_options.is_a?(Hash) ? index_options : {})
141
142
  end
143
+ if foreign_key_options # rubocop:disable Style/Next
144
+ fk_opts = foreign_key_options.is_a?(Hash) ? foreign_key_options.dup : {}
145
+ fk_opts.update(column: "#{col}_id") if col.to_s.singularize != col.to_s
146
+ to_table = fk_opts.delete(:to_table) || col.to_s.pluralize
147
+ @base.add_foreign_key(@table_name, to_table, fk_opts)
148
+ end
142
149
  end
143
150
  end
144
151
  alias belongs_to references
@@ -7,7 +7,7 @@ module Ridgepole
7
7
  module AbstractMysqlAdapter
8
8
  module UseAlterIndex
9
9
  def add_index(table_name, column_name, options = {})
10
- index_name, index_type, index_columns, index_options, _index_algorithm, index_using = add_index_options(table_name, column_name, options)
10
+ index_name, index_type, index_columns, index_options, _index_algorithm, index_using = add_index_options(table_name, column_name, **options)
11
11
 
12
12
  # cannot specify index_algorithm
13
13
  execute "ALTER TABLE #{quote_table_name(table_name)} ADD #{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns})#{index_options}"
@@ -41,7 +41,7 @@ module Ridgepole
41
41
  end
42
42
  end
43
43
  end
44
- rescue EOFError # rubocop:disable Lint/SuppressedException
44
+ rescue EOFError
45
45
  # nothing to do
46
46
  end
47
47
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ridgepole
4
- VERSION = '0.8.7'
4
+ VERSION = '0.8.12'
5
5
  end
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.required_ruby_version = Gem::Requirement.new('>= 2.2.7')
22
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.2.7') # rubocop:disable Gemspec/RequiredRubyVersion
23
23
 
24
24
  spec.add_dependency 'activerecord', '>= 5.0.1', '< 6.1'
25
25
  spec.add_dependency 'diffy'
@@ -18,9 +18,9 @@ ERBh.define_method(:cond) do |conds, m, e = nil|
18
18
  m
19
19
  else
20
20
  e || (begin
21
- m.class.new
22
- rescue StandardError
23
- nil
24
- end)
21
+ m.class.new
22
+ rescue StandardError
23
+ nil
24
+ end)
25
25
  end
26
26
  end
@@ -138,6 +138,12 @@ describe 'Ridgepole::Client#diff -> migrate' do
138
138
  opts = ['--dump-without-table-options']
139
139
  out, status = run_ridgepole('--diff', "'#{JSON.dump(conn_spec)}'", f.path, *opts)
140
140
 
141
+ # v6.0.3 is the oldest version that doesn't produce any kwargs warnings with Ruby 2.7
142
+ if condition('< 6.0.3')
143
+ # https://github.com/jeremyevans/ruby-warning/blob/1.1.0/lib/warning.rb#L18
144
+ out = out.lines.grep_v(/: warning: (?:Using the last argument (?:for `.+' )?as keyword parameters is deprecated; maybe \*\* should be added to the call|Passing the keyword argument (?:for `.+' )?as the last hash parameter is deprecated|Splitting the last argument (?:for `.+' )?into positional and keyword parameters is deprecated|The called method (?:`.+' )?is defined here)\n\z/).join
145
+ end
146
+
141
147
  expect(out).to be_empty
142
148
  expect(status.success?).to be_truthy
143
149
  end
@@ -175,10 +175,10 @@ describe 'Ridgepole::Client.diff' do
175
175
  delta = subject.diff(actual_dsl, expected_dsl)
176
176
  expect(delta.differ?).to be_truthy
177
177
  expect(delta.script).to match_ruby erbh(<<-ERB)
178
- change_column("employee_clubs", "club_id", :integer, <%= {:unsigned=>false, :null=>true, :default=>nil} + cond('>= 5.1', comment: nil) %>)
178
+ change_column("employee_clubs", "club_id", :integer, **<%= {:unsigned=>false, :null=>true, :default=>nil} + cond('>= 5.1', comment: nil) %>)
179
179
 
180
- change_column("employees", "last_name", :string, <%= {:limit=>20, :default=>"XXX", :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
181
- change_column("employees", "gender", :string, <%= {:limit=>2, :null=>false, :default=>nil, :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
180
+ change_column("employees", "last_name", :string, **<%= {:limit=>20, :default=>"XXX", :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
181
+ change_column("employees", "gender", :string, **<%= {:limit=>2, :null=>false, :default=>nil, :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
182
182
  ERB
183
183
  }
184
184
 
@@ -149,10 +149,10 @@ describe 'Ridgepole::Client.diff' do
149
149
  delta = subject.diff(actual_dsl, expected_dsl)
150
150
  expect(delta.differ?).to be_truthy
151
151
  expect(delta.script).to match_ruby erbh(<<-ERB)
152
- change_column("employee_clubs", "club_id", :integer, <%= {:unsigned=>false, :null=>true, :default=>nil} + cond('>= 5.1', comment: nil) %>)
152
+ change_column("employee_clubs", "club_id", :integer, **<%= {:unsigned=>false, :null=>true, :default=>nil} + cond('>= 5.1', comment: nil) %>)
153
153
 
154
- change_column("employees", "last_name", :string, <%= {:limit=>20, :default=>"XXX", :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
155
- change_column("employees", "gender", :string, <%= {:limit=>2, :null=>false, :default=>nil, :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
154
+ change_column("employees", "last_name", :string, **<%= {:limit=>20, :default=>"XXX", :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
155
+ change_column("employees", "gender", :string, **<%= {:limit=>2, :null=>false, :default=>nil, :unsigned=>false} + cond('>= 5.1', comment: nil) %>)
156
156
  ERB
157
157
  }
158
158
  end
@@ -36,7 +36,7 @@ describe 'Ridgepole::Client#diff -> migrate' do
36
36
  expect(delta.differ?).to be_truthy
37
37
  expect(subject.dump).to match_ruby actual_dsl
38
38
  expect(delta.script).to match_fuzzy <<-RUBY
39
- add_foreign_key("child", "parent", {:name=>"child_ibfk_1"})
39
+ add_foreign_key("child", "parent", **{:name=>"child_ibfk_1"})
40
40
  RUBY
41
41
  delta.migrate
42
42
  expect(subject.dump).to match_ruby expected_dsl
@@ -144,7 +144,7 @@ describe 'Ridgepole::Client#diff -> migrate' do
144
144
  expect(delta.differ?).to be_truthy
145
145
  expect(subject.dump).to match_ruby actual_dsl
146
146
  expect(delta.script).to match_fuzzy <<-RUBY
147
- add_foreign_key("child", "parent", {})
147
+ add_foreign_key("child", "parent", **{})
148
148
  RUBY
149
149
  delta.migrate
150
150
  expect(subject.dump).to match_ruby expected_dsl
@@ -186,15 +186,9 @@ describe 'Ridgepole::Client#diff -> migrate' do
186
186
  subject { client(dump_without_table_options: false) }
187
187
 
188
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
189
  expect do
196
190
  subject.diff(dsl).migrate
197
- end.to raise_error(/Mysql2::Error: Cannot drop index/)
191
+ end.to raise_error('The column `parent_id` of the table `child` has a foreign key but no index. Although InnoDB creates an index automatically, please add one explicitly in order for ridgepole to manage it.')
198
192
  }
199
193
  end
200
194
 
@@ -219,7 +213,82 @@ describe 'Ridgepole::Client#diff -> migrate' do
219
213
  expect(Ridgepole::Logger.instance).to_not receive(:warn)
220
214
  subject.diff(dsl).migrate
221
215
 
222
- expect { subject.diff(dsl).migrate }.to_not raise_error
216
+ expect(subject.diff(dsl).differ?).to be_falsey
223
217
  }
224
218
  end
219
+
220
+ context 'when create fk on the primary key' do
221
+ let(:dsl) do
222
+ erbh(<<-ERB)
223
+ create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
224
+ end
225
+
226
+ create_table "icons", primary_key: "user_id", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
227
+ end
228
+ add_foreign_key "icons", "users", name: "fk_icons_users"
229
+ ERB
230
+ end
231
+
232
+ subject { client(dump_without_table_options: false) }
233
+
234
+ it {
235
+ expect(Ridgepole::Logger.instance).to_not receive(:warn)
236
+ subject.diff(dsl).migrate
237
+
238
+ expect(subject.diff(dsl).differ?).to be_falsey
239
+ }
240
+ end
241
+ end
242
+
243
+ context 'when create fk on the first primary key' do
244
+ let(:dsl) do
245
+ erbh(<<-ERB)
246
+ create_table "users", <%= i cond('>= 5.1',id: :integer) %>, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
247
+ end
248
+
249
+ create_table "employee", <%= i cond('>= 5.1',id: :integer) %>, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
250
+ end
251
+
252
+ create_table "icons", primary_key: ["user_id", "employee_id"], force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
253
+ t.integer "user_id", null: false
254
+ t.integer "employee_id", null: false
255
+ end
256
+ add_foreign_key "icons", "users", name: "fk_icons_users"
257
+ ERB
258
+ end
259
+
260
+ subject { client(dump_without_table_options: false) }
261
+
262
+ it {
263
+ expect(Ridgepole::Logger.instance).to_not receive(:warn)
264
+ subject.diff(dsl).migrate
265
+
266
+ expect(subject.diff(dsl).differ?).to be_falsey
267
+ }
268
+ end
269
+
270
+ context 'when create fk on the second primary key' do
271
+ let(:dsl) do
272
+ erbh(<<-ERB)
273
+ create_table "users", <%= i cond('>= 5.1',id: :integer) %>, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
274
+ end
275
+
276
+ create_table "employee", <%= i cond('>= 5.1',id: :integer) %>, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
277
+ end
278
+
279
+ create_table "icons", primary_key: ["user_id", "employee_id"], force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
280
+ t.integer "user_id", null: false
281
+ t.integer "employee_id", null: false
282
+ end
283
+ add_foreign_key "icons", "employees", name: "fk_icons_employees"
284
+ ERB
285
+ end
286
+
287
+ subject { client(dump_without_table_options: false) }
288
+
289
+ it {
290
+ expect do
291
+ subject.diff(dsl).migrate
292
+ end.to raise_error('The column `employee_id` of the table `icons` has a foreign key but no index. Although InnoDB creates an index automatically, please add one explicitly in order for ridgepole to manage it.')
293
+ }
225
294
  end