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 +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/strong_migrations.rb +6 -6
- data/lib/strong_migrations/migration.rb +50 -66
- data/lib/strong_migrations/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bbb45da961375e9a3499c6b651da754c164f05b0e33f9b5061a856ce508663b6
|
4
|
+
data.tar.gz: 1718eb87c7eb8b60553b1e800e8a4866cdbc134ae9c062e012caa6e8f3d4be6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63dde3000e42dbe433914159a815a1dc4a1ebca89e78f3f342e9aa664bd1e921bbb3a1ee33140d6e56d50aef400b171e27a24509fd369da0350b0f548a108a25
|
7
|
+
data.tar.gz: d187c338c085b86ad7498ea09253a9d2015415740b4c09d78ec1f9f37abbc20261255b1590210d7bbf473fcb2ecfa1bad57a9e356a9e2480ec2f90d9fb355404
|
data/CHANGELOG.md
CHANGED
data/lib/strong_migrations.rb
CHANGED
@@ -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
|
-
|
24
|
-
|
23
|
+
%{add_command}
|
24
|
+
%{change_command}
|
25
25
|
end
|
26
26
|
|
27
27
|
def down
|
28
|
-
|
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
|
-
%{
|
111
|
-
|
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
|
-
|
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:
|
42
|
+
model: args[0].to_s.classify,
|
56
43
|
code: code,
|
57
|
-
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
|
67
|
-
options
|
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
|
79
|
-
|
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
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
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:
|
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? &&
|
106
|
-
|
107
|
-
safe =
|
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
|
95
|
+
table, options = args
|
96
|
+
options ||= {}
|
97
|
+
|
112
98
|
raise_error :create_table if options[:force]
|
113
|
-
|
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
|
103
|
+
table, reference, options = args
|
104
|
+
options ||= {}
|
105
|
+
|
116
106
|
index_value = options.fetch(:index, ar5)
|
117
107
|
if postgresql? && index_value
|
118
|
-
|
119
|
-
|
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
|
-
|
124
|
-
|
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
|
-
|
117
|
+
table, column, null, default = args
|
133
118
|
if !null && !default.nil?
|
134
119
|
raise_error :change_column_null,
|
135
|
-
code: backfill_code(
|
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(
|
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
|
180
|
-
|
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
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
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
|
-
|
176
|
+
|
177
|
+
"#{command} #{str_args.join(", ")}"
|
195
178
|
end
|
196
179
|
|
197
|
-
def backfill_code(
|
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
|
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.
|
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-
|
13
|
+
date: 2018-10-19 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|