strong_migrations 1.5.0 → 1.6.0
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 +4 -0
- data/README.md +35 -1
- data/lib/strong_migrations/checker.rb +3 -0
- data/lib/strong_migrations/checks.rb +19 -0
- data/lib/strong_migrations/error_messages.rb +7 -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: 23737504a1d3beb08bf2412b161b8e59a8a620b4f4d993f11fe931e7f1772452
|
4
|
+
data.tar.gz: 993dc5f9d78cba148540a22772c18ed115ea6ae8075dd209cc3a869cc2a647a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e906dc1c79d12a72a7d0e6fb4ca87319095d3b0a39f2da1dbd08b453d19e3215892745c86f80bf9ea26bbf07173d87022aab21b424a881f24744b65396fcd9bc
|
7
|
+
data.tar.gz: 56a0dadcb236f09a1a7d5450456ae343ac8cb6662b65f983c28b4268edad6e7cbecfa47c86ebb0d1da72dcc1b305e7196521811a48838c25beb1119846a2b928
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -62,7 +62,7 @@ Potentially dangerous operations:
|
|
62
62
|
- [removing a column](#removing-a-column)
|
63
63
|
- [adding a column with a default value](#adding-a-column-with-a-default-value)
|
64
64
|
- [backfilling data](#backfilling-data)
|
65
|
-
- [adding a stored generated column](#adding-a-stored-generated-column)
|
65
|
+
- [adding a stored generated column](#adding-a-stored-generated-column)
|
66
66
|
- [changing the type of a column](#changing-the-type-of-a-column)
|
67
67
|
- [renaming a column](#renaming-a-column)
|
68
68
|
- [renaming a table](#renaming-a-table)
|
@@ -79,6 +79,10 @@ Postgres-specific checks:
|
|
79
79
|
- [adding a json column](#adding-a-json-column)
|
80
80
|
- [setting NOT NULL on an existing column](#setting-not-null-on-an-existing-column)
|
81
81
|
|
82
|
+
Config-specific checks:
|
83
|
+
|
84
|
+
- [changing the default value of a column](#changing-the-default-value-of-a-column)
|
85
|
+
|
82
86
|
Best practices:
|
83
87
|
|
84
88
|
- [keeping non-unique indexes to three columns or less](#keeping-non-unique-indexes-to-three-columns-or-less)
|
@@ -628,6 +632,36 @@ class ValidateSomeColumnNotNull < ActiveRecord::Migration[6.0]
|
|
628
632
|
end
|
629
633
|
```
|
630
634
|
|
635
|
+
### Changing the default value of a column
|
636
|
+
|
637
|
+
#### Bad
|
638
|
+
|
639
|
+
Rails < 7 enables partial writes by default, which can cause incorrect values to be inserted when changing the default value of a column.
|
640
|
+
|
641
|
+
```ruby
|
642
|
+
class ChangeSomeColumnDefault < ActiveRecord::Migration[6.1]
|
643
|
+
def change
|
644
|
+
change_column_default :users, :some_column, from: "old", to: "new"
|
645
|
+
end
|
646
|
+
end
|
647
|
+
|
648
|
+
User.create!(some_column: "old") # can insert "new"
|
649
|
+
```
|
650
|
+
|
651
|
+
#### Good
|
652
|
+
|
653
|
+
Disable partial writes in `config/application.rb`. For Rails < 7, use:
|
654
|
+
|
655
|
+
```ruby
|
656
|
+
config.active_record.partial_writes = false
|
657
|
+
```
|
658
|
+
|
659
|
+
For Rails 7, use:
|
660
|
+
|
661
|
+
```ruby
|
662
|
+
config.active_record.partial_inserts = false
|
663
|
+
```
|
664
|
+
|
631
665
|
### Keeping non-unique indexes to three columns or less
|
632
666
|
|
633
667
|
#### Bad
|
@@ -8,6 +8,7 @@ module StrongMigrations
|
|
8
8
|
def initialize(migration)
|
9
9
|
@migration = migration
|
10
10
|
@new_tables = []
|
11
|
+
@new_columns = []
|
11
12
|
@safe = false
|
12
13
|
@timeouts_set = false
|
13
14
|
@committed = false
|
@@ -46,6 +47,8 @@ module StrongMigrations
|
|
46
47
|
check_add_reference(method, *args)
|
47
48
|
when :change_column
|
48
49
|
check_change_column(*args)
|
50
|
+
when :change_column_default
|
51
|
+
check_change_column_default(*args)
|
49
52
|
when :change_column_null
|
50
53
|
check_change_column_null(*args)
|
51
54
|
when :change_table
|
@@ -32,6 +32,9 @@ module StrongMigrations
|
|
32
32
|
table, column, type = args
|
33
33
|
default = options[:default]
|
34
34
|
|
35
|
+
# keep track of new columns of change_column_default check
|
36
|
+
@new_columns << [table.to_s, column.to_s]
|
37
|
+
|
35
38
|
# Check key since DEFAULT NULL behaves differently from no default
|
36
39
|
#
|
37
40
|
# Also, Active Record has special case for uuid columns that allows function default values
|
@@ -198,6 +201,18 @@ Then add the foreign key in separate migrations."
|
|
198
201
|
raise_error :change_column, rewrite_blocks: adapter.rewrite_blocks unless safe
|
199
202
|
end
|
200
203
|
|
204
|
+
def check_change_column_default(*args)
|
205
|
+
table, column, _default_or_changes = args
|
206
|
+
|
207
|
+
# just check ActiveRecord::Base, even though can override on model
|
208
|
+
partial_inserts = ar_version >= 7 ? ActiveRecord::Base.partial_inserts : ActiveRecord::Base.partial_writes
|
209
|
+
|
210
|
+
if partial_inserts && !new_column?(table, column)
|
211
|
+
raise_error :change_column_default,
|
212
|
+
config: ar_version >= 7 ? "partial_inserts" : "partial_writes"
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
201
216
|
def check_change_column_null(*args)
|
202
217
|
table, column, null, default = args
|
203
218
|
if !null
|
@@ -460,5 +475,9 @@ Then add the foreign key in separate migrations."
|
|
460
475
|
def new_table?(table)
|
461
476
|
@new_tables.include?(table.to_s)
|
462
477
|
end
|
478
|
+
|
479
|
+
def new_column?(table, column)
|
480
|
+
new_table?(table) || @new_columns.include?([table.to_s, column.to_s])
|
481
|
+
end
|
463
482
|
end
|
464
483
|
end
|
@@ -161,6 +161,13 @@ Otherwise, remove the force option.",
|
|
161
161
|
execute call, so cannot help you here. Please make really sure that what
|
162
162
|
you're doing is safe before proceeding, then wrap it in a safety_assured { ... } block.",
|
163
163
|
|
164
|
+
change_column_default:
|
165
|
+
"Partial writes are enabled, which can cause incorrect values
|
166
|
+
to be inserted when changing the default value of a column.
|
167
|
+
Disable partial writes in config/application.rb:
|
168
|
+
|
169
|
+
config.active_record.%{config} = false",
|
170
|
+
|
164
171
|
change_column_null:
|
165
172
|
"Passing a default value to change_column_null runs a single UPDATE query,
|
166
173
|
which can cause downtime. Instead, backfill the existing rows in the
|
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.
|
4
|
+
version: 1.6.0
|
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: 2023-07-
|
13
|
+
date: 2023-07-22 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|