ridgepole 0.7.3.beta → 0.7.3.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +38 -0
  3. data/Appraisals +2 -2
  4. data/README.md +3 -9
  5. data/Rakefile +7 -1
  6. data/bin/ridgepole +97 -108
  7. data/gemfiles/activerecord_5.2.gemfile +1 -1
  8. data/lib/ridgepole/cli/config.rb +43 -43
  9. data/lib/ridgepole/client.rb +65 -63
  10. data/lib/ridgepole/default_limit.rb +26 -24
  11. data/lib/ridgepole/delta.rb +367 -369
  12. data/lib/ridgepole/diff.rb +366 -377
  13. data/lib/ridgepole/dsl_parser.rb +22 -20
  14. data/lib/ridgepole/dsl_parser/context.rb +95 -93
  15. data/lib/ridgepole/dsl_parser/table_definition.rb +125 -123
  16. data/lib/ridgepole/dumper.rb +73 -71
  17. data/lib/ridgepole/execute_expander.rb +55 -55
  18. data/lib/ridgepole/ext/abstract_mysql_adapter/dump_auto_increment.rb +3 -3
  19. data/lib/ridgepole/ext/abstract_mysql_adapter/use_alter_index.rb +1 -1
  20. data/lib/ridgepole/ext/pp_sort_hash.rb +14 -14
  21. data/lib/ridgepole/ext/schema_dumper.rb +1 -1
  22. data/lib/ridgepole/external_sql_executer.rb +20 -20
  23. data/lib/ridgepole/logger.rb +17 -15
  24. data/lib/ridgepole/migration_ext.rb +6 -8
  25. data/lib/ridgepole/schema_statements_ext.rb +3 -3
  26. data/lib/ridgepole/version.rb +1 -1
  27. data/ridgepole.gemspec +11 -10
  28. data/spec/cli_helper.rb +2 -2
  29. data/spec/erb_helper.rb +6 -2
  30. data/spec/hide_pending_formatter.rb +5 -4
  31. data/spec/mysql/_migrate/migrate_change_table_option_spec.rb +8 -8
  32. data/spec/mysql/bigint_pk/bigint_pk_spec.rb +8 -8
  33. data/spec/mysql/bigint_pk/int_pk_spec.rb +13 -13
  34. data/spec/mysql/cli/config_spec.rb +52 -53
  35. data/spec/mysql/cli/ridgepole_spec.rb +111 -111
  36. data/spec/mysql/collation/collation_spec.rb +39 -39
  37. data/spec/mysql/comment/comment_spec.rb +36 -36
  38. data/spec/mysql/default_lambda/default_lambda_spec.rb +30 -30
  39. data/spec/mysql/diff/diff2_spec.rb +18 -18
  40. data/spec/mysql/diff/diff_spec.rb +11 -11
  41. data/spec/mysql/dump/dump_class_method_spec.rb +2 -2
  42. data/spec/mysql/dump/dump_some_tables_spec.rb +14 -14
  43. data/spec/mysql/dump/dump_spec.rb +2 -2
  44. data/spec/mysql/dump/dump_unknown_column_type_spec.rb +3 -3
  45. data/spec/mysql/dump/dump_without_table_options_spec.rb +8 -8
  46. data/spec/mysql/fk/migrate_change_fk_spec.rb +40 -40
  47. data/spec/mysql/fk/migrate_create_fk_spec.rb +40 -40
  48. data/spec/mysql/fk/migrate_drop_fk_spec.rb +60 -60
  49. data/spec/mysql/migrate/check_orphan_index_spec.rb +6 -6
  50. data/spec/mysql/migrate/migrate_add_column2_spec.rb +43 -43
  51. data/spec/mysql/migrate/migrate_add_column_order_spec.rb +50 -50
  52. data/spec/mysql/migrate/migrate_add_column_spec.rb +11 -11
  53. data/spec/mysql/migrate/migrate_add_column_with_alter_extra_spec.rb +8 -8
  54. data/spec/mysql/migrate/migrate_add_column_with_script_spec.rb +11 -11
  55. data/spec/mysql/migrate/migrate_change_column2_spec.rb +20 -20
  56. data/spec/mysql/migrate/migrate_change_column3_spec.rb +60 -60
  57. data/spec/mysql/migrate/migrate_change_column4_spec.rb +16 -16
  58. data/spec/mysql/migrate/migrate_change_column5_spec.rb +24 -24
  59. data/spec/mysql/migrate/migrate_change_column6_spec.rb +40 -40
  60. data/spec/mysql/migrate/migrate_change_column7_spec.rb +4 -4
  61. data/spec/mysql/migrate/migrate_change_column8_spec.rb +9 -9
  62. data/spec/mysql/migrate/migrate_change_column_default_spec.rb +48 -48
  63. data/spec/mysql/migrate/migrate_change_column_float_spec.rb +16 -16
  64. data/spec/mysql/migrate/migrate_change_column_spec.rb +11 -11
  65. data/spec/mysql/migrate/migrate_change_index2_spec.rb +20 -20
  66. data/spec/mysql/migrate/migrate_change_index3_spec.rb +43 -43
  67. data/spec/mysql/migrate/migrate_change_index4_spec.rb +24 -24
  68. data/spec/mysql/migrate/migrate_change_index5_spec.rb +16 -16
  69. data/spec/mysql/migrate/migrate_change_index6_spec.rb +12 -12
  70. data/spec/mysql/migrate/migrate_change_index7_spec.rb +20 -20
  71. data/spec/mysql/migrate/migrate_change_index8_spec.rb +20 -20
  72. data/spec/mysql/migrate/migrate_change_index_spec.rb +17 -17
  73. data/spec/mysql/migrate/migrate_change_table_comment_spec.rb +10 -10
  74. data/spec/mysql/migrate/migrate_change_table_option_spec.rb +18 -18
  75. data/spec/mysql/migrate/migrate_check_relation_column_type_spec.rb +37 -37
  76. data/spec/mysql/migrate/migrate_create_index2_spec.rb +12 -12
  77. data/spec/mysql/migrate/migrate_create_index_spec.rb +11 -11
  78. data/spec/mysql/migrate/migrate_create_table_spec.rb +8 -8
  79. data/spec/mysql/migrate/migrate_create_table_with_index_spec.rb +6 -6
  80. data/spec/mysql/migrate/migrate_create_table_with_options_spec.rb +12 -12
  81. data/spec/mysql/migrate/migrate_create_table_with_script_spec.rb +11 -11
  82. data/spec/mysql/migrate/migrate_drop_column_and_index2_spec.rb +9 -9
  83. data/spec/mysql/migrate/migrate_drop_column_and_index_spec.rb +12 -12
  84. data/spec/mysql/migrate/migrate_drop_column_and_unique_index_spec.rb +10 -10
  85. data/spec/mysql/migrate/migrate_drop_column_spec.rb +11 -11
  86. data/spec/mysql/migrate/migrate_drop_index_spec.rb +11 -11
  87. data/spec/mysql/migrate/migrate_drop_table_spec.rb +8 -8
  88. data/spec/mysql/migrate/migrate_duplicate_index_spec.rb +6 -6
  89. data/spec/mysql/migrate/migrate_duplicate_table_spec.rb +6 -6
  90. data/spec/mysql/migrate/migrate_empty_spec.rb +4 -4
  91. data/spec/mysql/migrate/migrate_execute_spec.rb +55 -55
  92. data/spec/mysql/migrate/migrate_log_file_spec.rb +9 -9
  93. data/spec/mysql/migrate/migrate_merge_mode_spec.rb +9 -9
  94. data/spec/mysql/migrate/migrate_noop_spec.rb +19 -19
  95. data/spec/mysql/migrate/migrate_primary_key_spec.rb +18 -18
  96. data/spec/mysql/migrate/migrate_rename_column_spec.rb +17 -17
  97. data/spec/mysql/migrate/migrate_rename_table_spec.rb +26 -26
  98. data/spec/mysql/migrate/migrate_same_default_null_spec.rb +16 -16
  99. data/spec/mysql/migrate/migrate_same_spec.rb +4 -4
  100. data/spec/mysql/migrate/migrate_script_error_spec.rb +8 -8
  101. data/spec/mysql/migrate/migrate_skip_column_comment_change_spec.rb +8 -8
  102. data/spec/mysql/migrate/migrate_skip_drop_table_spec.rb +9 -9
  103. data/spec/mysql/migrate/migrate_skip_rename_column_spec.rb +8 -8
  104. data/spec/mysql/migrate/migrate_skip_rename_table_spec.rb +9 -9
  105. data/spec/mysql/migrate/migrate_with_ignore_tables_spec.rb +64 -64
  106. data/spec/mysql/migrate/migrate_with_pre_post_query_spec.rb +12 -12
  107. data/spec/mysql/migrate/migrate_with_tables_spec.rb +29 -29
  108. data/spec/mysql/migrate/migrate_with_verbose_log_spec.rb +18 -18
  109. data/spec/mysql/migrate_/migrate_create_index_with_alter_spec.rb +8 -8
  110. data/spec/mysql/migrate_/migrate_drop_index_with_alter_spec.rb +8 -8
  111. data/spec/mysql/text_blob_types/text_blob_types_spec.rb +8 -8
  112. data/spec/mysql/~default_name_fk/migrate_change_fk_spec.rb +12 -12
  113. data/spec/mysql/~default_name_fk/migrate_create_fk_spec.rb +30 -30
  114. data/spec/mysql/~default_name_fk/migrate_drop_fk_spec.rb +22 -22
  115. data/spec/mysql/~dump_auto_increment/migrate_create_table_with_index_spec.rb +7 -7
  116. data/spec/mysql57/json/add_json_column_spec.rb +8 -8
  117. data/spec/mysql57/json/change_json_column_spec.rb +24 -24
  118. data/spec/mysql57/json/drop_json_column_spec.rb +8 -8
  119. data/spec/mysql57/virtual/add_virtual_column_spec.rb +9 -9
  120. data/spec/mysql57/virtual/change_virtual_column_spec.rb +9 -9
  121. data/spec/mysql57/virtual/drop_virtual_column_spec.rb +9 -9
  122. data/spec/postgresql/diff/diff_spec.rb +18 -18
  123. data/spec/postgresql/dump/dump_spec.rb +2 -2
  124. data/spec/postgresql/fk/migrate_change_fk_spec.rb +24 -24
  125. data/spec/postgresql/fk/migrate_create_fk_spec.rb +40 -40
  126. data/spec/postgresql/fk/migrate_drop_fk_spec.rb +44 -44
  127. data/spec/postgresql/migrate/migrate_add_column_spec.rb +21 -21
  128. data/spec/postgresql/migrate/migrate_add_expression_index_spec.rb +2 -2
  129. data/spec/postgresql/migrate/migrate_bigint_spec.rb +12 -12
  130. data/spec/postgresql/migrate/migrate_change_column_default_spec.rb +8 -8
  131. data/spec/postgresql/migrate/migrate_change_column_spec.rb +19 -19
  132. data/spec/postgresql/migrate/migrate_change_index_spec.rb +13 -13
  133. data/spec/postgresql/migrate/migrate_check_relation_column_type_spec.rb +19 -19
  134. data/spec/postgresql/migrate/migrate_create_table_spec.rb +8 -8
  135. data/spec/postgresql/migrate/migrate_create_table_with_default_proc_spec.rb +18 -18
  136. data/spec/postgresql/migrate/migrate_drop_column_spec.rb +11 -11
  137. data/spec/postgresql/migrate/migrate_drop_column_with_index_spec.rb +12 -12
  138. data/spec/postgresql/migrate/migrate_drop_expression_index_spec.rb +8 -8
  139. data/spec/postgresql/migrate/migrate_drop_index_spec.rb +11 -11
  140. data/spec/postgresql/migrate/migrate_drop_table_spec.rb +8 -8
  141. data/spec/postgresql/migrate/migrate_ext_cols_spec.rb +8 -8
  142. data/spec/postgresql/migrate/migrate_primary_key_spec.rb +97 -0
  143. data/spec/postgresql/migrate/migrate_references_spec.rb +24 -24
  144. data/spec/postgresql/migrate/migrate_rename_column_spec.rb +11 -11
  145. data/spec/postgresql/migrate/migrate_rename_table_spec.rb +16 -16
  146. data/spec/postgresql/migrate/migrate_same_spec.rb +4 -4
  147. data/spec/postgresql/~default_name_fk/migrate_change_fk_spec.rb +12 -12
  148. data/spec/postgresql/~default_name_fk/migrate_create_fk_spec.rb +30 -30
  149. data/spec/postgresql/~default_name_fk/migrate_drop_fk_spec.rb +22 -22
  150. data/spec/processing_for_travis.rb +1 -1
  151. data/spec/spec_condition.rb +2 -2
  152. data/spec/spec_const.rb +9 -9
  153. data/spec/spec_helper.rb +21 -23
  154. metadata +54 -37
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d1090705cdbaf9295339b121d07f08efd6fdbc7
4
- data.tar.gz: 781eb9b40326854690496a54e120e51fbf98b33d
3
+ metadata.gz: acad17ac1fb19603bbf36ba03f17510ddd752db8
4
+ data.tar.gz: 93ccceda898278fb96684aaaa23bff4a6b1a0fb2
5
5
  SHA512:
6
- metadata.gz: 6b94add224450fbc51140fe4acf7e7ce34bb654dcc88c61fcf54edabd356d4e72bdd78ec5d8a13c0d7b893e39631a3395e1af31a5f946fa28d9508358caecf4f
7
- data.tar.gz: f9bb43e119c95da180813260fe620e2b57bd8af2d7ae0fbebd64f80c1fb65ba94856b81f1d0fefa5c064e76c68a0eb7dd17ad6fb81c530a9c4cf4f04668bf87c
6
+ metadata.gz: 4013ac5fff9afef7c70282b2b1a26543ec08832e30e8551b18f8ae9f3e019629d6143a8167084bc0556d2d116a4a2822b449af34fcc64e0a00480cc01ceeaadf
7
+ data.tar.gz: a081d8554675a28c9b55fefcb959d39d48c640ce768935b53eb0ccefdaff2a5e8b1eb84a3a1540ae4bde82c765af096dd17abb620412018f96b01c64247b3d46
@@ -0,0 +1,38 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'gemfiles/**/*'
4
+ TargetRubyVersion: 2.2
5
+ Bundler/OrderedGems:
6
+ Include:
7
+ - 'Appraisals'
8
+ Layout/IndentHeredoc:
9
+ Enabled: false
10
+ Metrics/AbcSize:
11
+ Enabled: false
12
+ Metrics/BlockLength:
13
+ Enabled: false
14
+ Metrics/BlockNesting:
15
+ Enabled: false
16
+ Metrics/ClassLength:
17
+ Enabled: false
18
+ Metrics/CyclomaticComplexity:
19
+ Enabled: false
20
+ Metrics/LineLength:
21
+ Enabled: false
22
+ Metrics/MethodLength:
23
+ Enabled: false
24
+ Metrics/ParameterLists:
25
+ Enabled: false
26
+ Metrics/PerceivedComplexity:
27
+ Enabled: false
28
+ Style/Documentation:
29
+ Enabled: false
30
+ Style/GuardClause:
31
+ Enabled: false
32
+ Style/MethodMissingSuper:
33
+ Enabled: false
34
+ Style/MixinUsage:
35
+ Exclude:
36
+ - 'spec/**/*'
37
+ Layout/ClosingHeredocIndentation:
38
+ Enabled: false
data/Appraisals CHANGED
@@ -1,6 +1,6 @@
1
1
  appraise 'activerecord-5.0' do
2
2
  gem 'activerecord', '~> 5.0.1'
3
- gem "pg", "< 1.0.0"
3
+ gem 'pg', '< 1.0.0'
4
4
  end
5
5
 
6
6
  appraise 'activerecord-5.1' do
@@ -8,6 +8,6 @@ appraise 'activerecord-5.1' do
8
8
  end
9
9
 
10
10
  appraise 'activerecord-5.2' do
11
- gem 'mysql2', '~> 0.4.4'
12
11
  gem 'activerecord', '~> 5.2.0'
12
+ gem 'mysql2', '~> 0.4.4'
13
13
  end
data/README.md CHANGED
@@ -6,6 +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
+ [![Unstable Version](https://img.shields.io/badge/unstable-0.7.3.beta-green.svg?longCache=true&style=flat)](https://rubygems.org/gems/ridgepole/versions/0.7.3.beta)
9
10
  [![Build Status](https://travis-ci.org/winebarrel/ridgepole.svg?branch=0.7)](https://travis-ci.org/winebarrel/ridgepole)
10
11
  [![Coverage Status](https://coveralls.io/repos/github/winebarrel/ridgepole/badge.svg?branch=0.7)](https://coveralls.io/github/winebarrel/ridgepole?branch=0.7)
11
12
 
@@ -84,6 +85,8 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
84
85
  * Support Rails 5.2
85
86
  * `>= 0.7.3`
86
87
  * Add `--mysql-change-table-comment option` ([pull#166](https://github.com/winebarrel/ridgepole/pull/166))
88
+ * Refactoring with RuboCop
89
+ * Support primary key dropping ([issue#246](https://github.com/winebarrel/ridgepole/issues/246))
87
90
 
88
91
  ## Installation
89
92
 
@@ -379,15 +382,6 @@ bundle exec appraisal activerecord-5.1 rake
379
382
 
380
383
  **Notice:** mysql-client/postgresql-client is required.
381
384
 
382
- ### on OS X (docker-machine & VirtualBox)
383
-
384
- Port forwarding is required.
385
-
386
- ```sh
387
- VBoxManage controlvm default natpf1 "mysql,tcp,127.0.0.1,3306,,3306"
388
- VBoxManage controlvm default natpf1 "psql,tcp,127.0.0.1,5432,,5432"
389
- ```
390
-
391
385
  ## Demo
392
386
 
393
387
  * [asciinema.org/a/9349](https://asciinema.org/a/9349)
data/Rakefile CHANGED
@@ -1,5 +1,11 @@
1
1
  require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
3
4
 
4
5
  RSpec::Core::RakeTask.new('spec')
5
- task :default => :spec
6
+
7
+ RuboCop::RakeTask.new do |task|
8
+ task.options = %w[-c .rubocop.yml]
9
+ end
10
+
11
+ task default: %i[rubocop spec]
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- $: << File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH << File.expand_path('../lib', __dir__)
3
3
  require 'yaml'
4
4
  require 'optparse'
5
5
  require 'fileutils'
@@ -10,22 +10,22 @@ $stdout.sync = true
10
10
  $stderr.sync = true
11
11
 
12
12
  Version = Ridgepole::VERSION
13
- DEFAULT_FILENAME = 'Schemafile'
13
+ DEFAULT_FILENAME = 'Schemafile'.freeze
14
14
 
15
- MAGIC_COMMENT = <<-EOS
15
+ MAGIC_COMMENT = <<-RUBY.freeze
16
16
  # -*- mode: ruby -*-
17
17
  # vi: set ft=ruby :
18
- EOS
18
+ RUBY
19
19
 
20
20
  COLUMN_TYPES = {
21
- :boolean => :bool,
22
- :integer => :int,
23
- :bigint => :bigint,
24
- :float => :float,
25
- :string => :string,
26
- :text => :text,
27
- :binary => :binary,
28
- }
21
+ boolean: :bool,
22
+ integer: :int,
23
+ bigint: :bigint,
24
+ float: :float,
25
+ string: :string,
26
+ text: :text,
27
+ binary: :binary
28
+ }.freeze
29
29
 
30
30
  config = nil
31
31
  env = 'development'
@@ -38,9 +38,9 @@ diff_with_apply = false
38
38
  exit_code = 0
39
39
 
40
40
  options = {
41
- :dry_run => false,
42
- :debug => false,
43
- :color => $stdout.tty?,
41
+ dry_run: false,
42
+ debug: false,
43
+ color: $stdout.tty?
44
44
  }
45
45
 
46
46
  set_mode = proc do |m|
@@ -49,13 +49,11 @@ set_mode = proc do |m|
49
49
  end
50
50
 
51
51
  def noop_migrate(delta, options)
52
- unless delta.script.empty?
53
- puts delta.script + "\n\n"
54
- end
52
+ puts delta.script + "\n\n" unless delta.script.empty?
55
53
 
56
54
  migrated, out = delta.migrate(
57
- :noop => true,
58
- :alter_extra => options[:alter_extra]
55
+ noop: true,
56
+ alter_extra: options[:alter_extra]
59
57
  )
60
58
 
61
59
  if migrated
@@ -63,99 +61,102 @@ def noop_migrate(delta, options)
63
61
  if line =~ /\A\s+/
64
62
  puts "# #{line}"
65
63
  else
66
- puts line.strip.gsub(/([^\d])([(),])([^\d])/) { "#{$1}#{$2}\n#{$3}" }.each_line.map {|i| "# #{i.gsub(/^\s+/, '')}"}.join + "\n"
64
+ puts line.strip.gsub(/([^\d])([(),])([^\d])/) { "#{Regexp.last_match(1)}#{Regexp.last_match(2)}\n#{Regexp.last_match(3)}" }.each_line.map { |i| "# #{i.gsub(/^\s+/, '')}" }.join + "\n"
67
65
  end
68
66
  end
69
67
  end
70
68
 
71
- return migrated
69
+ migrated
72
70
  end
73
71
 
74
72
  ARGV.options do |opt|
75
73
  begin
76
- opt.on('-c', '--config CONF_OR_FILE') {|v| config = v }
77
- opt.on('-E', '--env ENVIRONMENT') {|v| env = v }
78
- opt.on('-a', '--apply') { set_mode[:apply] }
79
- opt.on('-m', '--merge') { set_mode[:apply]; options[:merge] = true }
80
- opt.on('-f', '--file SCHEMAFILE') {|v| file = v }
81
- opt.on('', '--dry-run') { options[:dry_run] = true }
82
- opt.on('', '--table-options OPTIONS') {|v| options[:table_options] = v }
83
- opt.on('', '--alter-extra ALTER_SPEC') {|v| options[:alter_extra] = v }
84
- opt.on('', '--external-script SCRIPT') {|v| options[:external_script] = v }
85
- opt.on('', '--bulk-change') {
86
- raise OptionParser::InvalidOption, "Cannot use `bulk-change` in `merge`" if options[:merge]
74
+ opt.on('-c', '--config CONF_OR_FILE') { |v| config = v }
75
+ opt.on('-E', '--env ENVIRONMENT') { |v| env = v }
76
+ opt.on('-a', '--apply') { set_mode[:apply] }
77
+ opt.on('-m', '--merge') do
78
+ set_mode[:apply]
79
+ options[:merge] = true
80
+ end
81
+ opt.on('-f', '--file SCHEMAFILE') { |v| file = v }
82
+ opt.on('', '--dry-run') { options[:dry_run] = true }
83
+ opt.on('', '--table-options OPTIONS') { |v| options[:table_options] = v }
84
+ opt.on('', '--alter-extra ALTER_SPEC') { |v| options[:alter_extra] = v }
85
+ opt.on('', '--external-script SCRIPT') { |v| options[:external_script] = v }
86
+ opt.on('', '--bulk-change') do
87
+ raise OptionParser::InvalidOption, 'Cannot use `bulk-change` in `merge`' if options[:merge]
87
88
  options[:bulk_change] = true
88
- }
89
+ end
89
90
 
90
91
  COLUMN_TYPES.each do |column_type, column_type_alias|
91
- opt.on('', "--default-#{column_type_alias}-limit LIMIT", Integer) {|v|
92
+ opt.on('', "--default-#{column_type_alias}-limit LIMIT", Integer) do |v|
92
93
  options[:"default_#{column_type}_limit"] = v
93
- }
94
+ end
94
95
  end
95
96
 
96
- opt.on('', '--pre-query QUERY') {|v| options[:pre_query] = v }
97
- opt.on('', '--post-query QUERY') {|v| options[:post_query] = v }
98
- opt.on('-e', '--export') { set_mode[:export] }
99
- opt.on('', '--split') {|v| split = true }
100
- opt.on('', '--split-with-dir') {|v| split = :with_dir }
101
- opt.on('-d', '--diff DSL1 DSL2') {|diff_arg1|
97
+ opt.on('', '--pre-query QUERY') { |v| options[:pre_query] = v }
98
+ opt.on('', '--post-query QUERY') { |v| options[:post_query] = v }
99
+ opt.on('-e', '--export') { set_mode[:export] }
100
+ opt.on('', '--split') { split = true }
101
+ opt.on('', '--split-with-dir') { split = :with_dir }
102
+ opt.on('-d', '--diff DSL1 DSL2') do |diff_arg1|
102
103
  set_mode[:diff]
103
104
  diff_arg2 = ARGV.first
104
105
 
105
- if [diff_arg1, diff_arg2].any? {|i| i.nil? or i =~ /\A-/ }
106
+ if [diff_arg1, diff_arg2].any? { |i| i.nil? || i.start_with?('-') }
106
107
  puts opt.help
107
108
  exit 1
108
109
  end
109
110
 
110
111
  ARGV.shift
111
112
  diff_files = [diff_arg1, diff_arg2]
112
- }
113
- opt.on('', '--with-apply') { diff_with_apply = true }
114
- opt.on('-o', '--output SCHEMAFILE') {|v| output_file = v }
115
- opt.on('-t', '--tables TABLES', Array) {|v| options[:tables] = v }
116
- opt.on('', '--ignore-tables REGEX_LIST', Array) {|v| options[:ignore_tables] = v.map {|i| Regexp.new(i) } }
117
- opt.on('', '--mysql-use-alter') { options[:mysql_use_alter] = true }
118
- opt.on('', '--dump-without-table-options') { options[:dump_without_table_options] = true }
119
- opt.on('', '--dump-with-default-fk-name') { options[:dump_with_default_fk_name] = true }
120
- opt.on('', '--index-removed-drop-column') { options[:index_removed_drop_column] = true }
121
- opt.on('', '--skip-drop-table') { options[:skip_drop_table] = true }
122
- opt.on('', '--mysql-change-table-options') { options[:mysql_change_table_options] = true }
123
- opt.on('', '--mysql-change-table-comment') { options[:mysql_change_table_comment] = true }
124
- opt.on('', '--check-relation-type DEF_PK') {|v| options[:check_relation_type] = v }
125
- opt.on('', '--ignore-table-comment') { options[:ignore_table_comment] = true }
126
- opt.on('', '--skip-column-comment-change') { options[:skip_column_comment_change] = true }
127
- opt.on('', '--allow-pk-change') { options[:allow_pk_change] = true }
128
- opt.on('', '--create-table-with-index') { options[:create_table_with_index] = true }
129
-
130
- opt.on('', '--mysql-dump-auto-increment') {
113
+ end
114
+ opt.on('', '--with-apply') { diff_with_apply = true }
115
+ opt.on('-o', '--output SCHEMAFILE') { |v| output_file = v }
116
+ opt.on('-t', '--tables TABLES', Array) { |v| options[:tables] = v }
117
+ opt.on('', '--ignore-tables REGEX_LIST', Array) { |v| options[:ignore_tables] = v.map { |i| Regexp.new(i) } }
118
+ opt.on('', '--mysql-use-alter') { options[:mysql_use_alter] = true }
119
+ opt.on('', '--dump-without-table-options') { options[:dump_without_table_options] = true }
120
+ opt.on('', '--dump-with-default-fk-name') { options[:dump_with_default_fk_name] = true }
121
+ opt.on('', '--index-removed-drop-column') { options[:index_removed_drop_column] = true }
122
+ opt.on('', '--skip-drop-table') { options[:skip_drop_table] = true }
123
+ opt.on('', '--mysql-change-table-options') { options[:mysql_change_table_options] = true }
124
+ opt.on('', '--mysql-change-table-comment') { options[:mysql_change_table_comment] = true }
125
+ opt.on('', '--check-relation-type DEF_PK') { |v| options[:check_relation_type] = v }
126
+ opt.on('', '--ignore-table-comment') { options[:ignore_table_comment] = true }
127
+ opt.on('', '--skip-column-comment-change') { options[:skip_column_comment_change] = true }
128
+ opt.on('', '--allow-pk-change') { options[:allow_pk_change] = true }
129
+ opt.on('', '--create-table-with-index') { options[:create_table_with_index] = true }
130
+
131
+ opt.on('', '--mysql-dump-auto-increment') do
131
132
  if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('5.1')
132
133
  raise OptionParser::InvalidOption, '`mysql-dump-auto-increment` is not available in `activerecord < 5.1`'
133
134
  end
134
135
 
135
136
  options[:mysql_dump_auto_increment] = true
136
- }
137
+ end
137
138
 
138
- opt.on('-r', '--require LIBS', Array) {|v| v.each {|i| require i } }
139
- opt.on('' , '--log-file LOG_FILE') {|v| options[:log_file] = v }
140
- opt.on('' , '--verbose') { Ridgepole::Logger.verbose = true }
141
- opt.on('' , '--debug') { options[:debug] = true }
142
- opt.on('' , '--[no-]color') {|v| options[:color] = v }
139
+ opt.on('-r', '--require LIBS', Array) { |v| v.each { |i| require i } }
140
+ opt.on('', '--log-file LOG_FILE') { |v| options[:log_file] = v }
141
+ opt.on('', '--verbose') { Ridgepole::Logger.verbose = true }
142
+ opt.on('', '--debug') { options[:debug] = true }
143
+ opt.on('', '--[no-]color') { |v| options[:color] = v }
143
144
 
144
- opt.on('-v', '--version') {
145
+ opt.on('-v', '--version') do
145
146
  puts opt.ver
146
147
  exit
147
- }
148
+ end
148
149
 
149
150
  opt.parse!
150
151
 
151
- if not mode or ([:apply, :export].include?(mode) and not config) or (options[:with_apply] and not config)
152
+ if !mode || (%i[apply export].include?(mode) && !config) || (options[:with_apply] && !config)
152
153
  puts opt.help
153
154
  exit 1
154
155
  end
155
- rescue => e
156
- $stderr.puts("[ERROR] #{e.message}")
156
+ rescue StandardError => e
157
+ warn("[ERROR] #{e.message}")
157
158
 
158
- unless e.kind_of?(OptionParser::ParseError)
159
+ unless e.is_a?(OptionParser::ParseError)
159
160
  puts "\t" + e.backtrace.join("\n\t")
160
161
  end
161
162
 
@@ -165,7 +166,7 @@ end
165
166
 
166
167
  begin
167
168
  logger = Ridgepole::Logger.instance
168
- logger.set_debug(options[:debug])
169
+ logger.debug = options[:debug]
169
170
 
170
171
  client = Ridgepole::Client.new(Ridgepole::Config.load(config, env), options) if config
171
172
 
@@ -192,7 +193,7 @@ begin
192
193
  logger.info(" write `#{schema_file}`")
193
194
  FileUtils.mkdir_p(schema_dir)
194
195
 
195
- open(schema_file, 'wb') do |f|
196
+ File.open(schema_file, 'wb') do |f|
196
197
  f.puts MAGIC_COMMENT
197
198
  f.puts definition
198
199
  end
@@ -200,66 +201,58 @@ begin
200
201
 
201
202
  logger.info(" write `#{output_file}`")
202
203
 
203
- open(output_file, 'wb') do |f|
204
+ File.open(output_file, 'wb') do |f|
204
205
  f.puts MAGIC_COMMENT
205
206
 
206
207
  requires.each do |require_path|
207
208
  f.puts "require '#{require_path}'"
208
209
  end
209
210
  end
211
+ elsif output_file == '-'
212
+ logger.info('# Export Schema')
213
+ puts client.dump
210
214
  else
211
- if output_file == '-'
212
- logger.info('# Export Schema')
213
- puts client.dump
214
- else
215
- logger.info("Export Schema to `#{output_file}`")
216
- open(output_file, 'wb') do |f|
217
- f.puts MAGIC_COMMENT
218
- f.puts client.dump
219
- end
215
+ logger.info("Export Schema to `#{output_file}`")
216
+ File.open(output_file, 'wb') do |f|
217
+ f.puts MAGIC_COMMENT
218
+ f.puts client.dump
220
219
  end
221
220
  end
222
221
  when :apply
223
- unless File.exist?(file)
224
- raise "No Schemafile found (looking for: #{file})"
225
- end
222
+ raise "No Schemafile found (looking for: #{file})" unless File.exist?(file)
226
223
 
227
224
  msg = (options[:merge] ? 'Merge' : 'Apply') + " `#{file}`"
228
225
  msg << ' (dry-run)' if options[:dry_run]
229
226
  logger.info(msg)
230
227
 
231
228
  dsl = File.read(file)
232
- delta = client.diff(dsl, :path => file)
229
+ delta = client.diff(dsl, path: file)
233
230
  differ = delta.differ?
234
231
 
235
232
  if options[:dry_run]
236
- if differ
237
- differ = noop_migrate(delta, options)
238
- end
233
+ differ = noop_migrate(delta, options) if differ
239
234
  else
240
235
  logger.verbose_info('# Update schema')
241
236
 
242
237
  differ, _out = delta.migrate(
243
- :external_script => options[:external_script],
244
- :alter_extra => options[:alter_extra]
238
+ external_script: options[:external_script],
239
+ alter_extra: options[:alter_extra]
245
240
  )
246
241
  end
247
242
 
248
- unless differ
249
- logger.info('No change')
250
- end
243
+ logger.info('No change') unless differ
251
244
  when :diff
252
245
  diff_files = diff_files.map do |diff_file|
253
246
  if File.exist?(diff_file)
254
247
  file_ext = File.extname(diff_file)
255
248
 
256
- if %w(.yml .yaml).include?(file_ext)
249
+ if %w[.yml .yaml].include?(file_ext)
257
250
  Ridgepole::Config.load(diff_file, env)
258
251
  else
259
252
  File.open(diff_file)
260
253
  end
261
254
  else
262
- YAML.load(diff_file)
255
+ YAML.safe_load(diff_file, [], [], true)
263
256
  end
264
257
  end
265
258
 
@@ -269,23 +262,19 @@ begin
269
262
  logger.verbose_info('# Update schema')
270
263
  differ = delta.differ?
271
264
 
272
- if differ
273
- differ, _out = delta.migrate
274
- end
265
+ differ, _out = delta.migrate if differ
275
266
 
276
- if differ
277
- logger.info('No change')
278
- end
267
+ logger.info('No change') if differ
279
268
  elsif delta.differ?
280
269
  differ = noop_migrate(delta, options)
281
270
  exit_code = 1 if differ
282
271
  end
283
272
  end
284
- rescue => e
273
+ rescue StandardError => e
285
274
  if options[:debug]
286
275
  raise e
287
276
  else
288
- $stderr.puts("[ERROR] #{[e.message, e.backtrace.first].join("\n\t")}")
277
+ warn("[ERROR] #{[e.message, e.backtrace.first].join("\n\t")}")
289
278
  exit 1
290
279
  end
291
280
  end