strong_migrations 1.3.0 → 1.3.2
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 +8 -0
- data/lib/strong_migrations/checks.rb +28 -14
- data/lib/strong_migrations/error_messages.rb +10 -0
- 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: 227ddfd8f939d855d93232f5d30d02a0c219f46c6393d98e2be5d8756c445e33
|
4
|
+
data.tar.gz: bde562f14e23d1fc7a1209e075efffad03454594b842bcde3c7b1ec6fa3046b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 972103d956c2a8cdff88e478401f18459eabe93d7260107b5c19908cc76bef9451b19e99852511d8a8675639b076107d79d26a6046308d4627afc88f580afc76
|
7
|
+
data.tar.gz: 3bf1e422cf0d5cd4297bf63a6121570e5afa9acddccf42c4fa7a21eac40fa0cc6d5fca04e2227a45132d7efde0b7f702bb90f3be916097079f9b569cdd04ba50
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
## 1.3.2 (2022-10-09)
|
2
|
+
|
3
|
+
- Improved error message for `add_column` with `default: nil` with Postgres 10
|
4
|
+
|
5
|
+
## 1.3.1 (2022-09-21)
|
6
|
+
|
7
|
+
- Fixed check for `add_column` with `default: nil` with Postgres 10
|
8
|
+
|
1
9
|
## 1.3.0 (2022-08-30)
|
2
10
|
|
3
11
|
- Added check for `add_column` with `uuid` type and volatile default value
|
@@ -32,9 +32,11 @@ module StrongMigrations
|
|
32
32
|
table, column, type = args
|
33
33
|
default = options[:default]
|
34
34
|
|
35
|
-
#
|
35
|
+
# Check key since DEFAULT NULL behaves differently from no default
|
36
|
+
#
|
37
|
+
# Also, Active Record has special case for uuid columns that allows function default values
|
36
38
|
# https://github.com/rails/rails/blob/v7.0.3.1/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb#L92-L93
|
37
|
-
if
|
39
|
+
if options.key?(:default) && (!adapter.add_column_default_safe? || (volatile = (postgresql? && type.to_s == "uuid" && default.to_s.include?("()") && adapter.default_volatile?(default))))
|
38
40
|
if options[:null] == false
|
39
41
|
options = options.except(:null)
|
40
42
|
append = "
|
@@ -42,14 +44,21 @@ module StrongMigrations
|
|
42
44
|
Then add the NOT NULL constraint in separate migrations."
|
43
45
|
end
|
44
46
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
47
|
+
if default.nil?
|
48
|
+
raise_error :add_column_default_null,
|
49
|
+
command: command_str("add_column", [table, column, type, options.except(:default)]),
|
50
|
+
append: append,
|
51
|
+
rewrite_blocks: adapter.rewrite_blocks
|
52
|
+
else
|
53
|
+
raise_error :add_column_default,
|
54
|
+
add_command: command_str("add_column", [table, column, type, options.except(:default)]),
|
55
|
+
change_command: command_str("change_column_default", [table, column, default]),
|
56
|
+
remove_command: command_str("remove_column", [table, column]),
|
57
|
+
code: backfill_code(table, column, default, volatile),
|
58
|
+
append: append,
|
59
|
+
rewrite_blocks: adapter.rewrite_blocks,
|
60
|
+
default_type: (volatile ? "volatile" : "non-null")
|
61
|
+
end
|
53
62
|
elsif default.is_a?(Proc) && postgresql?
|
54
63
|
# adding a column with a VOLATILE default is not safe
|
55
64
|
# https://www.postgresql.org/docs/current/sql-altertable.html#SQL-ALTERTABLE-NOTES
|
@@ -222,9 +231,7 @@ Then add the foreign key in separate migrations."
|
|
222
231
|
|
223
232
|
add_constraint_code =
|
224
233
|
if constraint_methods
|
225
|
-
#
|
226
|
-
expr_column = column.to_s =~ /\A[a-z0-9_]+\z/ ? column : connection.quote_column_name(column)
|
227
|
-
command_str(:add_check_constraint, [table, "#{expr_column} IS NOT NULL", {name: constraint_name, validate: false}])
|
234
|
+
command_str(:add_check_constraint, [table, "#{quote_column_if_needed(column)} IS NOT NULL", {name: constraint_name, validate: false}])
|
228
235
|
else
|
229
236
|
safety_assured_str(add_code)
|
230
237
|
end
|
@@ -424,12 +431,19 @@ Then add the foreign key in separate migrations."
|
|
424
431
|
model = table.to_s.classify
|
425
432
|
if function
|
426
433
|
# update_all(column: Arel.sql(default)) also works in newer versions of Active Record
|
427
|
-
"#{
|
434
|
+
update_expr = "#{quote_column_if_needed(column)} = #{default}"
|
435
|
+
"#{model}.unscoped.in_batches do |relation| \n relation.where(#{column}: nil).update_all(#{update_expr.inspect})\n sleep(0.01)\n end"
|
428
436
|
else
|
429
437
|
"#{model}.unscoped.in_batches do |relation| \n relation.update_all #{column}: #{default.inspect}\n sleep(0.01)\n end"
|
430
438
|
end
|
431
439
|
end
|
432
440
|
|
441
|
+
# only quote when needed
|
442
|
+
# important! only use for display purposes
|
443
|
+
def quote_column_if_needed(column)
|
444
|
+
column.to_s =~ /\A[a-z0-9_]+\z/ ? column : connection.quote_column_name(column)
|
445
|
+
end
|
446
|
+
|
433
447
|
def new_table?(table)
|
434
448
|
@new_tables.include?(table.to_s)
|
435
449
|
end
|
@@ -25,6 +25,16 @@ class Backfill%{migration_name} < ActiveRecord::Migration%{migration_suffix}
|
|
25
25
|
end
|
26
26
|
end",
|
27
27
|
|
28
|
+
add_column_default_null:
|
29
|
+
"Adding a column with a null default blocks %{rewrite_blocks} while the entire table is rewritten.
|
30
|
+
Instead, add the column without a default value.
|
31
|
+
|
32
|
+
class %{migration_name} < ActiveRecord::Migration%{migration_suffix}
|
33
|
+
def change
|
34
|
+
%{command}
|
35
|
+
end
|
36
|
+
end",
|
37
|
+
|
28
38
|
add_column_default_callable:
|
29
39
|
"Strong Migrations does not support inspecting callable default values.
|
30
40
|
Please make really sure you're not calling a VOLATILE function,
|
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: 1.3.
|
4
|
+
version: 1.3.2
|
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: 2022-
|
13
|
+
date: 2022-10-10 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|