strong_migrations 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b830fe1881c2a85f578d28bb4b938c38204627897639cfc28ae44486f81dfb90
4
- data.tar.gz: '09b4ef459fa407a8143120ec2dc19c98b5b02a885b1537cce26d63bea648eeab'
3
+ metadata.gz: 23737504a1d3beb08bf2412b161b8e59a8a620b4f4d993f11fe931e7f1772452
4
+ data.tar.gz: 993dc5f9d78cba148540a22772c18ed115ea6ae8075dd209cc3a869cc2a647a4
5
5
  SHA512:
6
- metadata.gz: 7e34db2fb7db9d0b22c9c3c2a8ecca150308cb70b54e1e2b08eec2ccb52460f8aa9818a409aec0d7782678523bda35471fbd693cf4b025bbbd9dffe26a127afb
7
- data.tar.gz: aad5de96c3886caa4c34f78a85edd6a8ada109f3acc31d8d72bcc6ff8d3ee41787f052cfcf56598103c30dddccd22a61d958e40ae6ac8bcdafeed22b4bc6d3c3
6
+ metadata.gz: e906dc1c79d12a72a7d0e6fb4ca87319095d3b0a39f2da1dbd08b453d19e3215892745c86f80bf9ea26bbf07173d87022aab21b424a881f24744b65396fcd9bc
7
+ data.tar.gz: 56a0dadcb236f09a1a7d5450456ae343ac8cb6662b65f983c28b4268edad6e7cbecfa47c86ebb0d1da72dcc1b305e7196521811a48838c25beb1119846a2b928
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 1.6.0 (2023-07-22)
2
+
3
+ - Added check for `change_column_default`
4
+
1
5
  ## 1.5.0 (2023-07-02)
2
6
 
3
7
  - Added check for `add_column` with stored generated columns
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) [unreleased]
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
@@ -1,3 +1,3 @@
1
1
  module StrongMigrations
2
- VERSION = "1.5.0"
2
+ VERSION = "1.6.0"
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: 1.5.0
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-02 00:00:00.000000000 Z
13
+ date: 2023-07-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord