strong_migrations 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08eb5f509b5fb98501eb6f83840c414c331c989b7d92bd29825cbaa0e2312177'
4
- data.tar.gz: 0fc4b64d44609ff2f6bbbf48d53730e35551654d3a1aaf629b7305ab8ac92caf
3
+ metadata.gz: bbb45da961375e9a3499c6b651da754c164f05b0e33f9b5061a856ce508663b6
4
+ data.tar.gz: 1718eb87c7eb8b60553b1e800e8a4866cdbc134ae9c062e012caa6e8f3d4be6b
5
5
  SHA512:
6
- metadata.gz: fc5cbfa0dbcdc47b8f5e42c18c64d77cabcc01deac0e92077d9d034bff6a47145354da066c70d2eb2906c60c895f5639de40ee9a1c00e015f80d54242f8bcb4c
7
- data.tar.gz: 5dfcdd45c56da317a83509c6f79c3d81b8c35e3f6e8aaf0af32c8144e134b919da481b2f501a87dbb581f4c61db96bccc4103cf285210f7777f0258020e1c6f3
6
+ metadata.gz: 63dde3000e42dbe433914159a815a1dc4a1ebca89e78f3f342e9aa664bd1e921bbb3a1ee33140d6e56d50aef400b171e27a24509fd369da0350b0f548a108a25
7
+ data.tar.gz: d187c338c085b86ad7498ea09253a9d2015415740b4c09d78ec1f9f37abbc20261255b1590210d7bbf473fcb2ecfa1bad57a9e356a9e2480ec2f90d9fb355404
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.3.1
2
+
3
+ - Fixed error with `remove_column` and `type` argument
4
+ - Improved message customization
5
+
1
6
  ## 0.3.0
2
7
 
3
8
  - Added support for custom checks
@@ -20,12 +20,12 @@ Instead, add the column without a default value, then change the default.
20
20
 
21
21
  class %{migration_name} < ActiveRecord::Migration%{migration_suffix}
22
22
  def up
23
- add_column %{table}, %{column}, %{type}%{options}
24
- change_column_default %{table}, %{column}, %{default}
23
+ %{add_command}
24
+ %{change_command}
25
25
  end
26
26
 
27
27
  def down
28
- remove_column %{table}, %{column}
28
+ %{remove_command}
29
29
  end
30
30
  end
31
31
 
@@ -107,8 +107,8 @@ class %{migration_name} < ActiveRecord::Migration%{migration_suffix}
107
107
  disable_ddl_transaction!
108
108
 
109
109
  def change
110
- %{command} %{table}, %{reference}, index: false%{options}
111
- add_index %{table}, %{column}, algorithm: :concurrently
110
+ %{reference_command}
111
+ %{index_command}
112
112
  end
113
113
  end",
114
114
 
@@ -119,7 +119,7 @@ class %{migration_name} < ActiveRecord::Migration%{migration_suffix}
119
119
  disable_ddl_transaction!
120
120
 
121
121
  def change
122
- add_index %{table}, %{column}, algorithm: :concurrently%{options}
122
+ %{command}
123
123
  end
124
124
  end",
125
125
 
@@ -14,11 +14,8 @@ module StrongMigrations
14
14
  end
15
15
 
16
16
  def method_missing(method, *args, &block)
17
- table = args[0].to_s
18
-
19
17
  unless @safe || ENV["SAFETY_ASSURED"] || is_a?(ActiveRecord::Schema) || @direction == :down || version_safe?
20
18
  ar5 = ActiveRecord::VERSION::MAJOR >= 5
21
- model = table.classify
22
19
 
23
20
  case method
24
21
  when :remove_column, :remove_columns, :remove_timestamps, :remove_reference, :remove_belongs_to
@@ -41,20 +38,10 @@ module StrongMigrations
41
38
 
42
39
  code = ar5 ? "self.ignored_columns = #{columns.inspect}" : "def self.columns\n super.reject { |c| #{columns.inspect}.include?(c.name) }\n end"
43
40
 
44
- command = String.new("#{method} #{sym_str(table)}")
45
- case method
46
- when :remove_column, :remove_reference, :remove_belongs_to
47
- command << ", #{sym_str(args[1])}#{options_str(args[2] || {})}"
48
- when :remove_columns
49
- columns.each do |c|
50
- command << ", #{sym_str(c)}"
51
- end
52
- end
53
-
54
41
  raise_error :remove_column,
55
- model: model,
42
+ model: args[0].to_s.classify,
56
43
  code: code,
57
- command: command,
44
+ command: command_str(method, args),
58
45
  column_suffix: columns.size > 1 ? "s" : ""
59
46
  when :change_table
60
47
  raise_error :change_table, header: "Possibly dangerous operation"
@@ -63,31 +50,26 @@ module StrongMigrations
63
50
  when :rename_column
64
51
  raise_error :rename_column
65
52
  when :add_index
66
- columns = args[1]
67
- options = args[2] || {}
53
+ table, columns, options = args
54
+ options ||= {}
55
+
68
56
  if columns.is_a?(Array) && columns.size > 3 && !options[:unique]
69
57
  raise_error :add_index_columns, header: "Best practice"
70
58
  end
71
- if postgresql? && options[:algorithm] != :concurrently && !@new_tables.to_a.include?(table)
72
- raise_error :add_index,
73
- table: sym_str(table),
74
- column: column_str(columns),
75
- options: options_str(options.except(:algorithm))
59
+ if postgresql? && options[:algorithm] != :concurrently && !@new_tables.to_a.include?(table.to_s)
60
+ raise_error :add_index, command: command_str("add_index", [table, columns, options.merge(algorithm: :concurrently)])
76
61
  end
77
62
  when :add_column
78
- column = args[1]
79
- type = args[2]
80
- options = args[3] || {}
63
+ table, column, type, options = args
64
+ options ||= {}
81
65
  default = options[:default]
82
66
 
83
67
  if !default.nil? && !(postgresql? && postgresql_version >= 110000)
84
68
  raise_error :add_column_default,
85
- table: sym_str(table),
86
- column: sym_str(column),
87
- type: sym_str(type),
88
- options: options_str(options.except(:default)),
89
- default: default.inspect,
90
- code: backfill_code(model, column, default)
69
+ add_command: command_str("add_column", [table, column, type, options.except(:default)]),
70
+ change_command: command_str("change_column_default", [table, column, default]),
71
+ remove_command: command_str("remove_column", [table, column]),
72
+ code: backfill_code(table, column, default)
91
73
  end
92
74
 
93
75
  if type.to_s == "json" && postgresql?
@@ -95,44 +77,47 @@ module StrongMigrations
95
77
  raise_error :add_column_json
96
78
  else
97
79
  raise_error :add_column_json_legacy,
98
- model: model,
99
- table: connection.quote_table_name(table)
80
+ model: table.to_s.classify,
81
+ table: connection.quote_table_name(table.to_s)
100
82
  end
101
83
  end
102
84
  when :change_column
85
+ table, column, type = args
86
+
103
87
  safe = false
104
88
  # assume Postgres 9.1+ since previous versions are EOL
105
- if postgresql? && args[2].to_s == "text"
106
- column = connection.columns(table).find { |c| c.name.to_s == args[1].to_s }
107
- safe = column && column.type == :string
89
+ if postgresql? && type.to_s == "text"
90
+ found_column = connection.columns(table).find { |c| c.name.to_s == column.to_s }
91
+ safe = found_column && found_column.type == :string
108
92
  end
109
93
  raise_error :change_column unless safe
110
94
  when :create_table
111
- options = args[1] || {}
95
+ table, options = args
96
+ options ||= {}
97
+
112
98
  raise_error :create_table if options[:force]
113
- (@new_tables ||= []) << table
99
+
100
+ # keep track of new tables of add_index check
101
+ (@new_tables ||= []) << table.to_s
114
102
  when :add_reference, :add_belongs_to
115
- options = args[2] || {}
103
+ table, reference, options = args
104
+ options ||= {}
105
+
116
106
  index_value = options.fetch(:index, ar5)
117
107
  if postgresql? && index_value
118
- reference = args[1]
119
- columns = []
120
- columns << "#{reference}_type" if options[:polymorphic]
121
- columns << "#{reference}_id"
108
+ columns = options[:polymorphic] ? [:"#{reference}_type", :"#{reference}_id"] : :"#{reference}_id"
109
+
122
110
  raise_error :add_reference,
123
- command: method,
124
- table: sym_str(table),
125
- reference: sym_str(reference),
126
- column: column_str(columns),
127
- options: options_str(options.except(:index))
111
+ reference_command: command_str(method, [table, reference, options.merge(index: false)]),
112
+ index_command: command_str("add_index", [table, columns, {algorithm: :concurrently}])
128
113
  end
129
114
  when :execute
130
115
  raise_error :execute, header: "Possibly dangerous operation"
131
116
  when :change_column_null
132
- _, column, null, default = args
117
+ table, column, null, default = args
133
118
  if !null && !default.nil?
134
119
  raise_error :change_column_null,
135
- code: backfill_code(model, column, default)
120
+ code: backfill_code(table, column, default)
136
121
  end
137
122
  end
138
123
 
@@ -144,7 +129,7 @@ module StrongMigrations
144
129
  result = super
145
130
 
146
131
  if StrongMigrations.auto_analyze && @direction == :up && postgresql? && method == :add_index
147
- connection.execute "ANALYZE VERBOSE #{connection.quote_table_name(table)}"
132
+ connection.execute "ANALYZE VERBOSE #{connection.quote_table_name(args[0].to_s)}"
148
133
  end
149
134
 
150
135
  result
@@ -176,25 +161,24 @@ module StrongMigrations
176
161
  stop!(message.gsub(/%(?!{)/, "%%") % vars, header: header || "Dangerous operation detected")
177
162
  end
178
163
 
179
- def sym_str(v)
180
- v.to_sym.inspect
181
- end
182
-
183
- def column_str(columns)
184
- columns = Array(columns).map(&:to_sym)
185
- columns = columns.first if columns.size == 1
186
- columns.inspect
187
- end
164
+ def command_str(command, args)
165
+ str_args = args[0..-2].map { |a| a.inspect }
188
166
 
189
- def options_str(options)
190
- str = String.new("")
191
- options.each do |k, v|
192
- str << ", #{k}: #{v.inspect}"
167
+ # prettier last arg
168
+ last_arg = args[-1]
169
+ if last_arg.is_a?(Hash)
170
+ if last_arg.any?
171
+ str_args << last_arg.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")
172
+ end
173
+ else
174
+ str_args << last_arg.inspect
193
175
  end
194
- str
176
+
177
+ "#{command} #{str_args.join(", ")}"
195
178
  end
196
179
 
197
- def backfill_code(model, column, default)
180
+ def backfill_code(table, column, default)
181
+ model = table.to_s.classify
198
182
  if ActiveRecord::VERSION::MAJOR >= 5
199
183
  "#{model}.in_batches.update_all #{column}: #{default.inspect}"
200
184
  else
@@ -1,3 +1,3 @@
1
1
  module StrongMigrations
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strong_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2018-10-15 00:00:00.000000000 Z
13
+ date: 2018-10-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord