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
@@ -2,7 +2,7 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "mysql2", "~> 0.4.4"
6
5
  gem "activerecord", "~> 5.2.0"
6
+ gem "mysql2", "~> 0.4.4"
7
7
 
8
8
  gemspec path: "../"
@@ -1,54 +1,54 @@
1
1
  require 'erb'
2
2
  require 'yaml'
3
3
 
4
- class Ridgepole::Config
5
- class << self
6
- def load(config, env = 'development')
7
- if config =~ /\Aenv:(.+)\z/
8
- config = ENV.fetch($1)
4
+ module Ridgepole
5
+ class Config
6
+ class << self
7
+ def load(config, env = 'development')
8
+ config = ENV.fetch(Regexp.last_match(1)) if config =~ /\Aenv:(.+)\z/
9
+
10
+ parsed_config = if File.exist?(config)
11
+ parse_config_file(config)
12
+ elsif (expanded = File.expand_path(config)) && File.exist?(expanded)
13
+ parse_config_file(expanded)
14
+ else
15
+ YAML.safe_load(ERB.new(config).result, [], [], true)
16
+ end
17
+
18
+ unless parsed_config.is_a?(Hash)
19
+ parsed_config = parse_database_url(config)
20
+ end
21
+
22
+ if parsed_config.key?(env.to_s)
23
+ parsed_config.fetch(env.to_s)
24
+ else
25
+ parsed_config
26
+ end
9
27
  end
10
28
 
11
- if File.exist?(config)
12
- parsed_config = parse_config_file(config)
13
- elsif (expanded = File.expand_path(config)) and File.exist?(expanded)
14
- parsed_config = parse_config_file(expanded)
15
- else
16
- parsed_config = YAML.load(ERB.new(config).result)
17
- end
29
+ private
18
30
 
19
- unless parsed_config.kind_of?(Hash)
20
- parsed_config = parse_database_url(config)
31
+ def parse_config_file(path)
32
+ yaml = ERB.new(File.read(path)).result
33
+ YAML.safe_load(yaml, [], [], true)
21
34
  end
22
35
 
23
- if parsed_config.has_key?(env.to_s)
24
- parsed_config.fetch(env.to_s)
25
- else
26
- parsed_config
36
+ def parse_database_url(config)
37
+ uri = URI.parse(config)
38
+
39
+ if [uri.scheme, uri.user, uri.host, uri.path].any? { |i| i.nil? || i.empty? }
40
+ raise "Invalid config: #{config.inspect}"
41
+ end
42
+
43
+ {
44
+ 'adapter' => uri.scheme,
45
+ 'username' => uri.user,
46
+ 'password' => uri.password,
47
+ 'host' => uri.host,
48
+ 'port' => uri.port,
49
+ 'database' => uri.path.sub(%r{\A/}, '')
50
+ }
27
51
  end
28
52
  end
29
-
30
- private
31
-
32
- def parse_config_file(path)
33
- yaml = ERB.new(File.read(path)).result
34
- YAML.load(yaml)
35
- end
36
-
37
- def parse_database_url(config)
38
- uri = URI.parse(config)
39
-
40
- if [uri.scheme, uri.user, uri.host, uri.path].any? {|i| i.nil? or i.empty? }
41
- raise "Invalid config: #{config.inspect}"
42
- end
43
-
44
- {
45
- 'adapter' => uri.scheme,
46
- 'username' => uri.user,
47
- 'password' => uri.password,
48
- 'host' => uri.host,
49
- 'port' => uri.port,
50
- 'database' => uri.path.sub(%r|\A/|, ''),
51
- }
52
- end
53
- end # of class methods
53
+ end
54
54
  end
@@ -1,84 +1,86 @@
1
- class Ridgepole::Client
2
- def initialize(conn_spec, options = {})
3
- @options = options
1
+ module Ridgepole
2
+ class Client
3
+ def initialize(conn_spec, options = {})
4
+ @options = options
4
5
 
5
- ActiveRecord::Base.establish_connection(conn_spec)
6
+ ActiveRecord::Base.establish_connection(conn_spec)
6
7
 
7
- # XXX: If the required processing in class method?
8
- if not @options.has_key?(:index_removed_drop_column) and Ridgepole::DefaultsLimit.adapter == :postgresql
9
- @options[:index_removed_drop_column] = true
10
- end
8
+ # XXX: If the required processing in class method?
9
+ if !@options.key?(:index_removed_drop_column) && (Ridgepole::DefaultsLimit.adapter == :postgresql)
10
+ @options[:index_removed_drop_column] = true
11
+ end
11
12
 
12
- Ridgepole::ExecuteExpander.expand_execute(ActiveRecord::Base.connection)
13
- @dumper = Ridgepole::Dumper.new(@options)
14
- @parser = Ridgepole::DSLParser.new(@options)
15
- @diff = Ridgepole::Diff.new(@options)
13
+ Ridgepole::ExecuteExpander.expand_execute(ActiveRecord::Base.connection)
14
+ @dumper = Ridgepole::Dumper.new(@options)
15
+ @parser = Ridgepole::DSLParser.new(@options)
16
+ @diff = Ridgepole::Diff.new(@options)
16
17
 
17
- if @options[:mysql_use_alter]
18
- require 'ridgepole/ext/abstract_mysql_adapter/use_alter_index'
19
- end
18
+ if @options[:mysql_use_alter]
19
+ require 'ridgepole/ext/abstract_mysql_adapter/use_alter_index'
20
+ end
20
21
 
21
- if @options[:mysql_dump_auto_increment]
22
- require 'ridgepole/ext/abstract_mysql_adapter/dump_auto_increment'
22
+ if @options[:mysql_dump_auto_increment]
23
+ require 'ridgepole/ext/abstract_mysql_adapter/dump_auto_increment'
24
+ end
23
25
  end
24
- end
25
-
26
- def dump(&block)
27
- logger = Ridgepole::Logger.instance
28
- logger.verbose_info('# Load tables')
29
- @dumper.dump(&block)
30
- end
31
26
 
32
- def diff(dsl, opts = {})
33
- logger = Ridgepole::Logger.instance
34
-
35
- logger.verbose_info('# Parse DSL')
36
- expected_definition, expected_execute = @parser.parse(dsl, opts)
37
- expected_definition.each do |table, definition|
38
- definition[:options][:options] ||= @options[:table_options] if @options[:table_options]
27
+ def dump(&block)
28
+ logger = Ridgepole::Logger.instance
29
+ logger.verbose_info('# Load tables')
30
+ @dumper.dump(&block)
39
31
  end
40
- logger.verbose_info('# Load tables')
41
- current_definition, _current_execute = @parser.parse(@dumper.dump, opts)
42
- logger.verbose_info('# Compare definitions')
43
- @diff.diff(current_definition, expected_definition, :execute => expected_execute)
44
- end
45
32
 
46
- class << self
47
- def diff(dsl_or_config1, dsl_or_config2, options = {})
33
+ def diff(dsl, opts = {})
48
34
  logger = Ridgepole::Logger.instance
49
35
 
50
- logger.verbose_info('# Parse DSL1')
51
- definition1, _execute1 = load_definition(dsl_or_config1, options)
52
- logger.verbose_info('# Parse DSL2')
53
- definition2, _execute2 = load_definition(dsl_or_config2, options)
54
-
36
+ logger.verbose_info('# Parse DSL')
37
+ expected_definition, expected_execute = @parser.parse(dsl, opts)
38
+ expected_definition.each do |_table, definition|
39
+ definition[:options][:options] ||= @options[:table_options] if @options[:table_options]
40
+ end
41
+ logger.verbose_info('# Load tables')
42
+ current_definition, _current_execute = @parser.parse(@dumper.dump, opts)
55
43
  logger.verbose_info('# Compare definitions')
56
- diff = Ridgepole::Diff.new(options)
57
- diff.diff(definition1, definition2)
44
+ @diff.diff(current_definition, expected_definition, execute: expected_execute)
58
45
  end
59
46
 
60
- def dump(conn_spec, options = {}, &block)
61
- client = self.new(conn_spec, options)
62
- client.dump(&block)
63
- end
47
+ class << self
48
+ def diff(dsl_or_config1, dsl_or_config2, options = {})
49
+ logger = Ridgepole::Logger.instance
64
50
 
65
- private
51
+ logger.verbose_info('# Parse DSL1')
52
+ definition1, _execute1 = load_definition(dsl_or_config1, options)
53
+ logger.verbose_info('# Parse DSL2')
54
+ definition2, _execute2 = load_definition(dsl_or_config2, options)
66
55
 
67
- def load_definition(dsl_or_config, options = {})
68
- parse_opts = {}
56
+ logger.verbose_info('# Compare definitions')
57
+ diff = Ridgepole::Diff.new(options)
58
+ diff.diff(definition1, definition2)
59
+ end
69
60
 
70
- case dsl_or_config
71
- when Hash
72
- dsl_or_config = dump(dsl_or_config, options)
73
- when File
74
- file = dsl_or_config
75
- parse_opts[:path] = file.path
76
- dsl_or_config = file.read
77
- file.close
61
+ def dump(conn_spec, options = {}, &block)
62
+ client = new(conn_spec, options)
63
+ client.dump(&block)
78
64
  end
79
65
 
80
- parser = Ridgepole::DSLParser.new(options)
81
- parser.parse(dsl_or_config, parse_opts)
66
+ private
67
+
68
+ def load_definition(dsl_or_config, options = {})
69
+ parse_opts = {}
70
+
71
+ case dsl_or_config
72
+ when Hash
73
+ dsl_or_config = dump(dsl_or_config, options)
74
+ when File
75
+ file = dsl_or_config
76
+ parse_opts[:path] = file.path
77
+ dsl_or_config = file.read
78
+ file.close
79
+ end
80
+
81
+ parser = Ridgepole::DSLParser.new(options)
82
+ parser.parse(dsl_or_config, parse_opts)
83
+ end
82
84
  end
83
- end # of class methods
85
+ end
84
86
  end
@@ -1,28 +1,30 @@
1
- class Ridgepole::DefaultsLimit
2
- DEFAULTS_LIMITS = {
3
- :mysql2 => {
4
- :boolean => 1,
5
- :integer => 4,
6
- :bigint => 8,
7
- :float => 24,
8
- :string => 255,
9
- :text => 65535,
10
- :binary => 65535,
11
- }
12
- }
1
+ module Ridgepole
2
+ class DefaultsLimit
3
+ DEFAULTS_LIMITS = {
4
+ mysql2: {
5
+ boolean: 1,
6
+ integer: 4,
7
+ bigint: 8,
8
+ float: 24,
9
+ string: 255,
10
+ text: 65_535,
11
+ binary: 65_535
12
+ }
13
+ }.freeze
13
14
 
14
- class << self
15
- def default_limit(column_type, options)
16
- defaults = DEFAULTS_LIMITS[adapter] || {}
17
- option_key = :"default_#{column_type}_limit"
18
- default_limit = options[option_key] || defaults[column_type] || 0
19
- default_limit.zero? ? nil : default_limit
20
- end
15
+ class << self
16
+ def default_limit(column_type, options)
17
+ defaults = DEFAULTS_LIMITS[adapter] || {}
18
+ option_key = :"default_#{column_type}_limit"
19
+ default_limit = options[option_key] || defaults[column_type] || 0
20
+ default_limit.zero? ? nil : default_limit
21
+ end
21
22
 
22
- def adapter
23
- ActiveRecord::Base.connection_config.fetch(:adapter).to_sym
24
- rescue ActiveRecord::ConnectionNotEstablished
25
- nil
23
+ def adapter
24
+ ActiveRecord::Base.connection_config.fetch(:adapter).to_sym
25
+ rescue ActiveRecord::ConnectionNotEstablished
26
+ nil
27
+ end
26
28
  end
27
- end # of class methods
29
+ end
28
30
  end
@@ -1,515 +1,513 @@
1
- class Ridgepole::Delta
2
- SCRIPT_NAME = '<Schema>'
1
+ module Ridgepole
2
+ class Delta
3
+ SCRIPT_NAME = '<Schema>'.freeze
3
4
 
4
- def initialize(delta, options = {})
5
- @delta = delta
6
- @options = options
7
- @logger = Ridgepole::Logger.instance
8
- end
5
+ def initialize(delta, options = {})
6
+ @delta = delta
7
+ @options = options
8
+ @logger = Ridgepole::Logger.instance
9
+ end
10
+
11
+ def migrate(options = {})
12
+ log_file = @options[:log_file]
13
+
14
+ if log_file
15
+ result = ActiveRecord::Migration.record_time do
16
+ migrate0(options)
17
+ end
9
18
 
10
- def migrate(options = {})
11
- if log_file = @options[:log_file]
12
- result = ActiveRecord::Migration.record_time do
19
+ File.open(log_file, 'wb') { |f| f.puts JSON.pretty_generate(result) }
20
+ result
21
+ else
13
22
  migrate0(options)
14
23
  end
15
-
16
- open(log_file, 'wb') {|f| f.puts JSON.pretty_generate(result) }
17
- result
18
- else
19
- migrate0(options)
20
24
  end
21
- end
22
25
 
23
- def script
24
- buf = StringIO.new
25
- pre_buf_for_fk = StringIO.new
26
- post_buf_for_fk = StringIO.new
26
+ def script
27
+ buf = StringIO.new
28
+ pre_buf_for_fk = StringIO.new
29
+ post_buf_for_fk = StringIO.new
27
30
 
28
- (@delta[:add] || {}).each do |table_name, attrs|
29
- append_create_table(table_name, attrs, buf, post_buf_for_fk)
30
- end
31
+ (@delta[:add] || {}).each do |table_name, attrs|
32
+ append_create_table(table_name, attrs, buf, post_buf_for_fk)
33
+ end
31
34
 
32
- (@delta[:rename] || {}).each do |table_name, attrs|
33
- append_rename_table(table_name, attrs, buf)
34
- end
35
+ (@delta[:rename] || {}).each do |table_name, attrs|
36
+ append_rename_table(table_name, attrs, buf)
37
+ end
35
38
 
36
- (@delta[:change] || {}).each do |table_name, attrs|
37
- append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
38
- end
39
+ (@delta[:change] || {}).each do |table_name, attrs|
40
+ append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
41
+ end
39
42
 
40
- (@delta[:delete] || {}).each do |table_name, attrs|
41
- append_drop_table(table_name, attrs, buf)
42
- end
43
+ (@delta[:delete] || {}).each do |table_name, attrs|
44
+ append_drop_table(table_name, attrs, buf)
45
+ end
43
46
 
44
- [
45
- pre_buf_for_fk,
46
- buf,
47
- post_buf_for_fk,
48
- ].map {|b| b.string.strip }.join("\n\n").strip
49
- end
47
+ [
48
+ pre_buf_for_fk,
49
+ buf,
50
+ post_buf_for_fk
51
+ ].map { |b| b.string.strip }.join("\n\n").strip
52
+ end
50
53
 
51
- def differ?
52
- not script.empty? or not delta_execute.empty?
53
- end
54
+ def differ?
55
+ !script.empty? || !delta_execute.empty?
56
+ end
54
57
 
55
- private
58
+ private
56
59
 
57
- def migrate0(options = {})
58
- migrated = false
59
- out = nil
60
+ def migrate0(options = {})
61
+ migrated = false
62
+ out = nil
60
63
 
61
- if options[:noop]
62
- disable_logging_orig = ActiveRecord::Migration.disable_logging
64
+ if options[:noop]
65
+ disable_logging_orig = ActiveRecord::Migration.disable_logging
63
66
 
64
- begin
65
- ActiveRecord::Migration.disable_logging = true
66
- buf = StringIO.new
67
+ begin
68
+ ActiveRecord::Migration.disable_logging = true
69
+ buf = StringIO.new
67
70
 
68
- callback = proc do |sql, name|
69
- buf.puts sql if sql =~ /\A(CREATE|ALTER|DROP|RENAME)\b/i
70
- end
71
+ callback = proc do |sql, _name|
72
+ buf.puts sql if sql =~ /\A(CREATE|ALTER|DROP|RENAME)\b/i
73
+ end
71
74
 
72
- eval_script_block = proc do
73
- Ridgepole::ExecuteExpander.without_operation(callback) do
74
- migrated = eval_script(script, options.merge(:out => buf))
75
+ eval_script_block = proc do
76
+ Ridgepole::ExecuteExpander.without_operation(callback) do
77
+ migrated = eval_script(script, options.merge(out: buf))
78
+ end
75
79
  end
76
- end
77
80
 
78
- if options[:alter_extra]
79
- Ridgepole::ExecuteExpander.with_alter_extra(options[:alter_extra]) do
81
+ if options[:alter_extra]
82
+ Ridgepole::ExecuteExpander.with_alter_extra(options[:alter_extra]) do
83
+ eval_script_block.call
84
+ end
85
+ else
80
86
  eval_script_block.call
81
87
  end
82
- else
83
- eval_script_block.call
84
- end
85
88
 
86
- out = buf.string.strip
87
- ensure
88
- ActiveRecord::Migration.disable_logging = disable_logging_orig
89
- end
90
- elsif options[:external_script]
91
- Ridgepole::ExecuteExpander.with_script(options[:external_script], Ridgepole::Logger.instance) do
92
- migrated = eval_script(script, options)
93
- end
94
- elsif options[:alter_extra]
95
- Ridgepole::ExecuteExpander.with_alter_extra(options[:alter_extra]) do
89
+ out = buf.string.strip
90
+ ensure
91
+ ActiveRecord::Migration.disable_logging = disable_logging_orig
92
+ end
93
+ elsif options[:external_script]
94
+ Ridgepole::ExecuteExpander.with_script(options[:external_script], Ridgepole::Logger.instance) do
95
+ migrated = eval_script(script, options)
96
+ end
97
+ elsif options[:alter_extra]
98
+ Ridgepole::ExecuteExpander.with_alter_extra(options[:alter_extra]) do
99
+ migrated = eval_script(script, options)
100
+ end
101
+ else
96
102
  migrated = eval_script(script, options)
97
103
  end
98
- else
99
- migrated = eval_script(script, options)
104
+
105
+ [migrated, out]
100
106
  end
101
107
 
102
- [migrated, out]
103
- end
108
+ def eval_script(script, options = {})
109
+ execute_count = 0
104
110
 
105
- def eval_script(script, options = {})
106
- execute_count = 0
111
+ begin
112
+ with_pre_post_query(options) do
113
+ unless script.empty?
114
+ ActiveRecord::Schema.new.instance_eval(script, SCRIPT_NAME, 1)
115
+ end
107
116
 
108
- begin
109
- with_pre_post_query(options) do
110
- unless script.empty?
111
- ActiveRecord::Schema.new.instance_eval(script, SCRIPT_NAME, 1)
117
+ execute_count = execute_sqls(options)
112
118
  end
113
-
114
- execute_count = execute_sqls(options)
119
+ rescue StandardError => e
120
+ raise_exception(script, e)
115
121
  end
116
- rescue => e
117
- raise_exception(script, e)
122
+
123
+ !script.empty? || execute_count.nonzero?
118
124
  end
119
125
 
120
- not script.empty? or execute_count.nonzero?
121
- end
126
+ def execute_sqls(options = {})
127
+ es = @delta[:execute] || []
128
+ out = options[:out] || $stdout
129
+ execute_count = 0
122
130
 
123
- def execute_sqls(options = {})
124
- es = @delta[:execute] || []
125
- out = options[:out] || $stdout
126
- execute_count = 0
131
+ es.each do |exec|
132
+ sql, cond = exec.values_at(:sql, :condition)
133
+ executable = false
127
134
 
128
- es.each do |exec|
129
- sql, cond = exec.values_at(:sql, :condition)
130
- executable = false
135
+ begin
136
+ executable = cond.nil? || cond.call(ActiveRecord::Base.connection)
137
+ rescue StandardError => e
138
+ errmsg = "[WARN] `#{sql}` is not executed: #{e.message}"
131
139
 
132
- begin
133
- executable = cond.nil? || cond.call(ActiveRecord::Base.connection)
134
- rescue => e
135
- errmsg = "[WARN] `#{sql}` is not executed: #{e.message}"
140
+ errmsg = ([errmsg] + e.backtrace).join("\n\tfrom ") if @options[:debug]
136
141
 
137
- if @options[:debug]
138
- errmsg = ([errmsg] + e.backtrace).join("\n\tfrom ")
142
+ Ridgepole::Logger.instance.warn(errmsg)
143
+
144
+ executable = false
139
145
  end
140
146
 
141
- Ridgepole::Logger.instance.warn(errmsg)
147
+ next unless executable
142
148
 
143
- executable = false
144
- end
145
-
146
- next unless executable
149
+ if options[:noop]
150
+ out.puts(sql.strip_heredoc)
151
+ else
152
+ @logger.info(sql.strip_heredoc)
153
+ ActiveRecord::Base.connection.execute(sql)
154
+ end
147
155
 
148
- if options[:noop]
149
- out.puts(sql.strip_heredoc)
150
- else
151
- @logger.info(sql.strip_heredoc)
152
- ActiveRecord::Base.connection.execute(sql)
156
+ execute_count += 1
153
157
  end
154
158
 
155
- execute_count += 1
159
+ execute_count
156
160
  end
157
161
 
158
- return execute_count
159
- end
160
-
161
- def with_pre_post_query(options = {})
162
- out = options[:out] || $stdout
162
+ def with_pre_post_query(options = {})
163
+ out = options[:out] || $stdout
163
164
 
164
- if (pre_query = @options[:pre_query])
165
- if options[:noop]
166
- out.puts(pre_query)
167
- else
168
- ActiveRecord::Base.connection.execute(pre_query)
165
+ if (pre_query = @options[:pre_query])
166
+ if options[:noop]
167
+ out.puts(pre_query)
168
+ else
169
+ ActiveRecord::Base.connection.execute(pre_query)
170
+ end
169
171
  end
170
- end
171
172
 
172
- retval = yield
173
+ retval = yield
173
174
 
174
- if (post_query = @options[:post_query])
175
- if options[:noop]
176
- out.puts(post_query)
177
- else
178
- ActiveRecord::Base.connection.execute(post_query)
175
+ if (post_query = @options[:post_query])
176
+ if options[:noop]
177
+ out.puts(post_query)
178
+ else
179
+ ActiveRecord::Base.connection.execute(post_query)
180
+ end
179
181
  end
182
+
183
+ retval
180
184
  end
181
185
 
182
- return retval
183
- end
186
+ def raise_exception(script, org)
187
+ lines = script.each_line
188
+ digit_number = (lines.count + 1).to_s.length
189
+ err_num = detect_error_line(org)
184
190
 
185
- def raise_exception(script, org)
186
- lines = script.each_line
187
- digit_number = (lines.count + 1).to_s.length
188
- err_num = detect_error_line(org)
191
+ errmsg = lines.with_index.map do |l, i|
192
+ line_num = i + 1
193
+ prefix = line_num == err_num ? '* ' : ' '
194
+ format("#{prefix}%*d: #{l}", digit_number, line_num)
195
+ end
189
196
 
190
- errmsg = lines.with_index.map {|l, i|
191
- line_num = i + 1
192
- prefix = (line_num == err_num) ? '* ' : ' '
193
- "#{prefix}%*d: #{l}" % [digit_number, line_num]
194
- }
197
+ if err_num > 0
198
+ from = err_num - 6
199
+ from = 0 if from < 0
200
+ to = err_num + 4
201
+ errmsg = errmsg.slice(from..to)
202
+ end
195
203
 
196
- if err_num > 0
197
- from = err_num - 6
198
- from = 0 if from < 0
199
- to = err_num + 4
200
- errmsg = errmsg.slice(from..to)
204
+ e = RuntimeError.new(org.message + "\n" + errmsg.join)
205
+ e.set_backtrace(org.backtrace)
206
+ raise e
201
207
  end
202
208
 
203
- e = RuntimeError.new(org.message + "\n" + errmsg.join)
204
- e.set_backtrace(org.backtrace)
205
- raise e
206
- end
209
+ def detect_error_line(exception)
210
+ rgx = /\A#{Regexp.escape(SCRIPT_NAME)}:(\d+):/
211
+ line = exception.backtrace.find { |i| i =~ rgx }
207
212
 
208
- def detect_error_line(e)
209
- rgx = /\A#{Regexp.escape(SCRIPT_NAME)}:(\d+):/
210
- line = e.backtrace.find {|i| i =~ rgx }
211
-
212
- if line and (m = rgx.match(line))
213
- m[1].to_i
214
- else
215
- 0
213
+ if line && (m = rgx.match(line))
214
+ m[1].to_i
215
+ else
216
+ 0
217
+ end
216
218
  end
217
- end
218
219
 
219
- def append_create_table(table_name, attrs, buf, post_buf_for_fk)
220
- options = attrs[:options] || {}
221
- definition = attrs[:definition] || {}
222
- indices = attrs[:indices] || {}
220
+ def append_create_table(table_name, attrs, buf, post_buf_for_fk)
221
+ options = attrs[:options] || {}
222
+ definition = attrs[:definition] || {}
223
+ indices = attrs[:indices] || {}
223
224
 
224
- buf.puts(<<-EOS)
225
+ buf.puts(<<-RUBY)
225
226
  create_table(#{table_name.inspect}, #{inspect_options_include_default_proc(options)}) do |t|
226
- EOS
227
+ RUBY
227
228
 
228
- definition.each do |column_name, column_attrs|
229
- column_type = column_attrs.fetch(:type)
230
- column_options = column_attrs[:options] || {}
231
- normalize_limit(column_type, column_options)
229
+ definition.each do |column_name, column_attrs|
230
+ column_type = column_attrs.fetch(:type)
231
+ column_options = column_attrs[:options] || {}
232
+ normalize_limit(column_type, column_options)
232
233
 
233
- buf.puts(<<-EOS)
234
+ buf.puts(<<-RUBY)
234
235
  t.column(#{column_name.inspect}, :#{column_type.to_s.inspect}, #{inspect_options_include_default_proc(column_options)})
235
- EOS
236
- end
236
+ RUBY
237
+ end
237
238
 
238
- if @options[:create_table_with_index] and not indices.empty?
239
- indices.each do |index_name, index_attrs|
240
- append_add_index(table_name, index_name, index_attrs, buf, true)
239
+ if @options[:create_table_with_index] && !indices.empty?
240
+ indices.each do |index_name, index_attrs|
241
+ append_add_index(table_name, index_name, index_attrs, buf, true)
242
+ end
241
243
  end
242
- end
243
244
 
244
- buf.puts(<<-EOS)
245
- end
246
- EOS
245
+ buf.puts(<<-RUBY)
246
+ end
247
+ RUBY
247
248
 
248
- if not @options[:create_table_with_index] and not indices.empty?
249
- append_change_table(table_name, buf) do
250
- indices.each do |index_name, index_attrs|
251
- append_add_index(table_name, index_name, index_attrs, buf)
249
+ if !(@options[:create_table_with_index]) && !indices.empty?
250
+ append_change_table(table_name, buf) do
251
+ indices.each do |index_name, index_attrs|
252
+ append_add_index(table_name, index_name, index_attrs, buf)
253
+ end
252
254
  end
253
255
  end
254
- end
255
256
 
256
- unless (foreign_keys = attrs[:foreign_keys] || {}).empty?
257
- foreign_keys.each do |_, foreign_key_attrs|
258
- append_add_foreign_key(table_name, foreign_key_attrs, post_buf_for_fk, @options)
257
+ unless (foreign_keys = attrs[:foreign_keys] || {}).empty?
258
+ foreign_keys.each do |_, foreign_key_attrs|
259
+ append_add_foreign_key(table_name, foreign_key_attrs, post_buf_for_fk, @options)
260
+ end
259
261
  end
260
- end
261
262
 
262
- buf.puts
263
- post_buf_for_fk.puts
264
- end
263
+ buf.puts
264
+ post_buf_for_fk.puts
265
+ end
265
266
 
266
- def append_rename_table(to_table_name, from_table_name, buf)
267
- buf.puts(<<-EOS)
267
+ def append_rename_table(to_table_name, from_table_name, buf)
268
+ buf.puts(<<-RUBY)
268
269
  rename_table(#{from_table_name.inspect}, #{to_table_name.inspect})
269
- EOS
270
+ RUBY
270
271
 
271
- buf.puts
272
- end
272
+ buf.puts
273
+ end
273
274
 
274
- def append_drop_table(table_name, attrs, buf)
275
- buf.puts(<<-EOS)
275
+ def append_drop_table(table_name, _attrs, buf)
276
+ buf.puts(<<-RUBY)
276
277
  drop_table(#{table_name.inspect})
277
- EOS
278
+ RUBY
278
279
 
279
- buf.puts
280
- end
280
+ buf.puts
281
+ end
281
282
 
282
- def append_change_table_options(table_name, table_options, buf)
283
- # XXX: MySQL only
284
- buf.puts(<<-EOS)
283
+ def append_change_table_options(table_name, table_options, buf)
284
+ # XXX: MySQL only
285
+ buf.puts(<<-RUBY)
285
286
  execute "ALTER TABLE #{ActiveRecord::Base.connection.quote_table_name(table_name)} #{table_options}"
286
- EOS
287
-
288
- buf.puts
289
- end
287
+ RUBY
290
288
 
291
- def append_change_table_comment(table_name, table_comment, buf)
292
- comment_literal = "COMMENT=#{ActiveRecord::Base.connection.quote(table_comment)}"
293
- append_change_table_options(table_name, comment_literal, buf)
294
- end
295
-
296
- def append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
297
- definition = attrs[:definition] || {}
298
- primary_key_definition = attrs[:primary_key_definition] || {}
299
- indices = attrs[:indices] || {}
300
- foreign_keys = attrs[:foreign_keys] || {}
301
- table_options = attrs[:table_options]
302
- table_comment = attrs[:table_comment]
303
-
304
- if not definition.empty? or not indices.empty? or not primary_key_definition.empty?
305
- append_change_table(table_name, buf) do
306
- append_delete_indices(table_name, indices, buf)
307
- append_change_definition(table_name, definition, buf)
308
- append_change_definition(table_name, primary_key_definition, buf)
309
- append_add_indices(table_name, indices, buf)
310
- end
289
+ buf.puts
311
290
  end
312
291
 
313
- unless foreign_keys.empty?
314
- append_change_foreign_keys(table_name, foreign_keys, pre_buf_for_fk, post_buf_for_fk, @options)
292
+ def append_change_table_comment(table_name, table_comment, buf)
293
+ comment_literal = "COMMENT=#{ActiveRecord::Base.connection.quote(table_comment)}"
294
+ append_change_table_options(table_name, comment_literal, buf)
315
295
  end
316
296
 
317
- if table_options
318
- append_change_table_options(table_name, table_options, buf)
319
- end
297
+ def append_change(table_name, attrs, buf, pre_buf_for_fk, post_buf_for_fk)
298
+ definition = attrs[:definition] || {}
299
+ primary_key_definition = attrs[:primary_key_definition] || {}
300
+ indices = attrs[:indices] || {}
301
+ foreign_keys = attrs[:foreign_keys] || {}
302
+ table_options = attrs[:table_options]
303
+ table_comment = attrs[:table_comment]
320
304
 
321
- if table_comment
322
- append_change_table_comment(table_name, table_comment, buf)
323
- end
305
+ if !definition.empty? || !indices.empty? || !primary_key_definition.empty?
306
+ append_change_table(table_name, buf) do
307
+ append_delete_indices(table_name, indices, buf)
308
+ append_change_definition(table_name, definition, buf)
309
+ append_change_definition(table_name, primary_key_definition, buf)
310
+ append_add_indices(table_name, indices, buf)
311
+ end
312
+ end
324
313
 
325
- buf.puts
326
- pre_buf_for_fk.puts
327
- post_buf_for_fk.puts
328
- end
314
+ unless foreign_keys.empty?
315
+ append_change_foreign_keys(table_name, foreign_keys, pre_buf_for_fk, post_buf_for_fk, @options)
316
+ end
329
317
 
330
- def append_change_table(table_name, buf)
331
- buf.puts "change_table(#{table_name.inspect}, {:bulk => true}) do |t|" if @options[:bulk_change]
332
- yield
333
- buf.puts 'end' if @options[:bulk_change]
334
- end
318
+ append_change_table_options(table_name, table_options, buf) if table_options
335
319
 
336
- def append_change_definition(table_name, delta, buf)
337
- (delta[:add] || {}).each do |column_name, attrs|
338
- append_add_column(table_name, column_name, attrs, buf)
339
- end
320
+ append_change_table_comment(table_name, table_comment, buf) if table_comment
340
321
 
341
- (delta[:rename] || {}).each do |column_name, attrs|
342
- append_rename_column(table_name, column_name, attrs, buf)
322
+ buf.puts
323
+ pre_buf_for_fk.puts
324
+ post_buf_for_fk.puts
343
325
  end
344
326
 
345
- (delta[:change] || {}).each do |column_name, attrs|
346
- append_change_column(table_name, column_name, attrs, buf)
327
+ def append_change_table(table_name, buf)
328
+ buf.puts "change_table(#{table_name.inspect}, {:bulk => true}) do |t|" if @options[:bulk_change]
329
+ yield
330
+ buf.puts 'end' if @options[:bulk_change]
347
331
  end
348
332
 
349
- (delta[:delete] || {}).each do |column_name, attrs|
350
- append_remove_column(table_name, column_name, attrs, buf)
333
+ def append_change_definition(table_name, delta, buf)
334
+ (delta[:add] || {}).each do |column_name, attrs|
335
+ append_add_column(table_name, column_name, attrs, buf)
336
+ end
337
+
338
+ (delta[:rename] || {}).each do |column_name, attrs|
339
+ append_rename_column(table_name, column_name, attrs, buf)
340
+ end
341
+
342
+ (delta[:change] || {}).each do |column_name, attrs|
343
+ append_change_column(table_name, column_name, attrs, buf)
344
+ end
345
+
346
+ (delta[:delete] || {}).each do |column_name, attrs|
347
+ append_remove_column(table_name, column_name, attrs, buf)
348
+ end
351
349
  end
352
- end
353
350
 
354
- def append_add_column(table_name, column_name, attrs, buf)
355
- type = attrs.fetch(:type)
356
- options = attrs[:options] || {}
357
- normalize_limit(type, options)
351
+ def append_add_column(table_name, column_name, attrs, buf)
352
+ type = attrs.fetch(:type)
353
+ options = attrs[:options] || {}
354
+ normalize_limit(type, options)
358
355
 
359
- if @options[:bulk_change]
360
- buf.puts(<<-EOS)
356
+ if @options[:bulk_change]
357
+ buf.puts(<<-RUBY)
361
358
  t.column(#{column_name.inspect}, #{type.inspect}, #{inspect_options_include_default_proc(options)})
362
- EOS
363
- else
364
- buf.puts(<<-EOS)
359
+ RUBY
360
+ else
361
+ buf.puts(<<-RUBY)
365
362
  add_column(#{table_name.inspect}, #{column_name.inspect}, #{type.inspect}, #{inspect_options_include_default_proc(options)})
366
- EOS
363
+ RUBY
364
+ end
367
365
  end
368
- end
369
366
 
370
- def append_rename_column(table_name, to_column_name, from_column_name, buf)
371
- if @options[:bulk_change]
372
- buf.puts(<<-EOS)
367
+ def append_rename_column(table_name, to_column_name, from_column_name, buf)
368
+ if @options[:bulk_change]
369
+ buf.puts(<<-RUBY)
373
370
  t.rename(#{from_column_name.inspect}, #{to_column_name.inspect})
374
- EOS
375
- else
376
- buf.puts(<<-EOS)
371
+ RUBY
372
+ else
373
+ buf.puts(<<-RUBY)
377
374
  rename_column(#{table_name.inspect}, #{from_column_name.inspect}, #{to_column_name.inspect})
378
- EOS
375
+ RUBY
376
+ end
379
377
  end
380
- end
381
378
 
382
- def append_change_column(table_name, column_name, attrs, buf)
383
- type = attrs.fetch(:type)
384
- options = attrs[:options] || {}
379
+ def append_change_column(table_name, column_name, attrs, buf)
380
+ type = attrs.fetch(:type)
381
+ options = attrs[:options] || {}
385
382
 
386
- # Fix for https://github.com/rails/rails/commit/7f0567b43b73b1bd1a16bfac9cd32fcbf1321b51
387
- if Ridgepole::ConnectionAdapters.mysql? and ActiveRecord::VERSION::STRING !~ /\A5\.0\./
388
- options[:comment] = nil unless options.has_key?(:comment)
389
- end
383
+ # Fix for https://github.com/rails/rails/commit/7f0567b43b73b1bd1a16bfac9cd32fcbf1321b51
384
+ if Ridgepole::ConnectionAdapters.mysql? && ActiveRecord::VERSION::STRING !~ /\A5\.0\./
385
+ options[:comment] = nil unless options.key?(:comment)
386
+ end
390
387
 
391
- if @options[:bulk_change]
392
- buf.puts(<<-EOS)
388
+ if @options[:bulk_change]
389
+ buf.puts(<<-RUBY)
393
390
  t.change(#{column_name.inspect}, #{type.inspect}, #{inspect_options_include_default_proc(options)})
394
- EOS
395
- else
396
- buf.puts(<<-EOS)
391
+ RUBY
392
+ else
393
+ buf.puts(<<-RUBY)
397
394
  change_column(#{table_name.inspect}, #{column_name.inspect}, #{type.inspect}, #{inspect_options_include_default_proc(options)})
398
- EOS
395
+ RUBY
396
+ end
399
397
  end
400
- end
401
398
 
402
- def append_remove_column(table_name, column_name, attrs, buf)
403
- if @options[:bulk_change]
404
- buf.puts(<<-EOS)
399
+ def append_remove_column(table_name, column_name, _attrs, buf)
400
+ if @options[:bulk_change]
401
+ buf.puts(<<-RUBY)
405
402
  t.remove(#{column_name.inspect})
406
- EOS
407
- else
408
- buf.puts(<<-EOS)
403
+ RUBY
404
+ else
405
+ buf.puts(<<-RUBY)
409
406
  remove_column(#{table_name.inspect}, #{column_name.inspect})
410
- EOS
407
+ RUBY
408
+ end
411
409
  end
412
- end
413
410
 
414
- def append_add_indices(table_name, delta, buf)
415
- (delta[:add] || {}).each do |index_name, attrs|
416
- append_add_index(table_name, index_name, attrs, buf)
411
+ def append_add_indices(table_name, delta, buf)
412
+ (delta[:add] || {}).each do |index_name, attrs|
413
+ append_add_index(table_name, index_name, attrs, buf)
414
+ end
417
415
  end
418
- end
419
416
 
420
- def append_delete_indices(table_name, delta, buf)
421
- (delta[:delete] || {}).each do |index_name, attrs|
422
- append_remove_index(table_name, index_name, attrs, buf)
417
+ def append_delete_indices(table_name, delta, buf)
418
+ (delta[:delete] || {}).each do |index_name, attrs|
419
+ append_remove_index(table_name, index_name, attrs, buf)
420
+ end
423
421
  end
424
- end
425
422
 
426
- def append_add_index(table_name, index_name, attrs, buf, force_bulk_change = false)
427
- column_name = attrs.fetch(:column_name)
428
- options = attrs[:options] || {}
423
+ def append_add_index(table_name, _index_name, attrs, buf, force_bulk_change = false)
424
+ column_name = attrs.fetch(:column_name)
425
+ options = attrs[:options] || {}
429
426
 
430
- if force_bulk_change or @options[:bulk_change]
431
- buf.puts(<<-EOS)
427
+ if force_bulk_change || @options[:bulk_change]
428
+ buf.puts(<<-RUBY)
432
429
  t.index(#{column_name.inspect}, #{options.inspect})
433
- EOS
434
- else
435
- buf.puts(<<-EOS)
430
+ RUBY
431
+ else
432
+ buf.puts(<<-RUBY)
436
433
  add_index(#{table_name.inspect}, #{column_name.inspect}, #{options.inspect})
437
- EOS
434
+ RUBY
435
+ end
438
436
  end
439
- end
440
437
 
441
- def append_remove_index(table_name, index_name, attrs, buf)
442
- column_name = attrs.fetch(:column_name)
443
- options = attrs[:options] || {}
444
- target = options[:name] ? {:name => options[:name]} : column_name
438
+ def append_remove_index(table_name, _index_name, attrs, buf)
439
+ column_name = attrs.fetch(:column_name)
440
+ options = attrs[:options] || {}
441
+ target = options[:name] ? { name: options[:name] } : column_name
445
442
 
446
- if @options[:bulk_change]
447
- buf.puts(<<-EOS)
443
+ if @options[:bulk_change]
444
+ buf.puts(<<-RUBY)
448
445
  t.remove_index(#{target.inspect})
449
- EOS
450
- else
451
- buf.puts(<<-EOS)
446
+ RUBY
447
+ else
448
+ buf.puts(<<-RUBY)
452
449
  remove_index(#{table_name.inspect}, #{target.inspect})
453
- EOS
450
+ RUBY
451
+ end
454
452
  end
455
- end
456
453
 
457
- def append_change_foreign_keys(table_name, delta, pre_buf_for_fk, post_buf_for_fk, options)
458
- (delta[:delete] || {}).each do |_, attrs|
459
- append_remove_foreign_key(table_name, attrs, pre_buf_for_fk, options)
460
- end
454
+ def append_change_foreign_keys(table_name, delta, pre_buf_for_fk, post_buf_for_fk, options)
455
+ (delta[:delete] || {}).each do |_, attrs|
456
+ append_remove_foreign_key(table_name, attrs, pre_buf_for_fk, options)
457
+ end
461
458
 
462
- (delta[:add] || {}).each do |_, attrs|
463
- append_add_foreign_key(table_name, attrs, post_buf_for_fk, options)
459
+ (delta[:add] || {}).each do |_, attrs|
460
+ append_add_foreign_key(table_name, attrs, post_buf_for_fk, options)
461
+ end
464
462
  end
465
- end
466
463
 
467
- def append_add_foreign_key(table_name, attrs, buf, options)
468
- to_table = attrs.fetch(:to_table)
469
- attrs_options = attrs[:options] || {}
464
+ def append_add_foreign_key(table_name, attrs, buf, _options)
465
+ to_table = attrs.fetch(:to_table)
466
+ attrs_options = attrs[:options] || {}
470
467
 
471
- buf.puts(<<-EOS)
468
+ buf.puts(<<-RUBY)
472
469
  add_foreign_key(#{table_name.inspect}, #{to_table.inspect}, #{attrs_options.inspect})
473
- EOS
474
- end
470
+ RUBY
471
+ end
475
472
 
476
- def append_remove_foreign_key(table_name, attrs, buf, options)
477
- attrs_options = attrs[:options] || {}
478
- fk_name = attrs_options[:name]
473
+ def append_remove_foreign_key(table_name, attrs, buf, _options)
474
+ attrs_options = attrs[:options] || {}
475
+ fk_name = attrs_options[:name]
479
476
 
480
- if fk_name
481
- target = {:name => fk_name}
482
- else
483
- target = attrs.fetch(:to_table)
484
- end
477
+ target = if fk_name
478
+ { name: fk_name }
479
+ else
480
+ attrs.fetch(:to_table)
481
+ end
485
482
 
486
- buf.puts(<<-EOS)
483
+ buf.puts(<<-RUBY)
487
484
  remove_foreign_key(#{table_name.inspect}, #{target.inspect})
488
- EOS
489
- end
485
+ RUBY
486
+ end
490
487
 
491
- def delta_execute
492
- @delta[:execute] || []
493
- end
488
+ def delta_execute
489
+ @delta[:execute] || []
490
+ end
494
491
 
495
- def normalize_limit(column_type, column_options)
496
- default_limit = Ridgepole::DefaultsLimit.default_limit(column_type, @options)
497
- column_options[:limit] ||= default_limit if default_limit
498
- end
492
+ def normalize_limit(column_type, column_options)
493
+ default_limit = Ridgepole::DefaultsLimit.default_limit(column_type, @options)
494
+ column_options[:limit] ||= default_limit if default_limit
495
+ end
496
+
497
+ def inspect_options_include_default_proc(options)
498
+ options = options.dup
499
499
 
500
- def inspect_options_include_default_proc(options)
501
- options = options.dup
502
-
503
- if options[:default].kind_of?(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
500
+ if options[:default].is_a?(Proc)
501
+ proc_default = options.delete(:default)
502
+ proc_default = ":default=>proc{#{proc_default.call.inspect}}"
503
+ options_inspect = options.inspect
504
+ options_inspect.sub!(/\}\z/, '')
505
+ options_inspect << ', ' if options_inspect !~ /\{\z/
506
+ options_inspect << proc_default << '}'
507
+ options_inspect
508
+ else
509
+ options.inspect
510
+ end
513
511
  end
514
512
  end
515
513
  end