strong_migrations 2.2.0 → 2.2.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: e155e036c26788c7c9fa7f513131422b784b895fe1cbfdfc8052ad1f5233cd6c
4
- data.tar.gz: 5fe21cada2e29c433313c686332d96c67e2c55d057fd6c68839fe1993307c0f8
3
+ metadata.gz: a78b56711a718210a3d14c82a4c30734c0b653827c1a2bdcc7617c27a7eed0e5
4
+ data.tar.gz: 9ef73e9cd8783a34d330c3517d98df50266427bd10bd92656cc528753aceb045
5
5
  SHA512:
6
- metadata.gz: 7f40abaf84f10ce6ce7255d083b3ec0940c2f7380705857767b0cde56b23802dcf844c27251c634006559dfb4489b799ab3758bf9928b6cb57b6bceb1bf051ec
7
- data.tar.gz: c283b8b93715a4aabd2c0e8dc15b022e45ebfbe3bb52fccc037b9c95d3b2279d6002b78354cab8600cf2972f96a6966af0fd80aa9b58cde53343b5035d8bc59c
6
+ metadata.gz: 0ece935c993dad2d316d183be938db27a9595cac2d0880f5fcd3f516965440bda73da9e8ad5239269451b991b04a897de03b8f0ed11b30400074daf30fa353b7
7
+ data.tar.gz: c8a18a098a6356daae4bf6bb827fa7f11c0a37e8b88712083af88f4eeb0f8dcda7f893491f1a044cc2399afc8147c31c3b07bf46a2155b972c78bb2aebbd479e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 2.2.1 (2025-03-21)
2
+
3
+ - Added support for `change_column_null` with default value with `safe_by_default` option
4
+ - Improved backfill instructions
5
+ - Fixed `safe_by_default` applying to migrations before `start_after`
6
+
1
7
  ## 2.2.0 (2025-02-01)
2
8
 
3
9
  - Fixed constraint name for long table and column names with `change_column_null`
data/README.md CHANGED
@@ -377,8 +377,8 @@ class BackfillSomeColumn < ActiveRecord::Migration[8.0]
377
377
  disable_ddl_transaction!
378
378
 
379
379
  def up
380
- User.unscoped.in_batches do |relation|
381
- relation.update_all some_column: "default_value"
380
+ User.unscoped.in_batches(of: 10000) do |relation|
381
+ relation.where(some_column: nil).update_all some_column: "default_value"
382
382
  sleep(0.01) # throttle
383
383
  end
384
384
  end
@@ -251,7 +251,7 @@ module StrongMigrations
251
251
  remove_args = [table, {name: constraint_name}]
252
252
 
253
253
  if StrongMigrations.safe_by_default
254
- safe_change_column_null(add_args, validate_args, change_args, remove_args, default, constraints)
254
+ safe_change_column_null(add_args, validate_args, change_args, remove_args, table, column, default, constraints)
255
255
  throw :safe
256
256
  end
257
257
 
@@ -444,9 +444,9 @@ module StrongMigrations
444
444
  if function
445
445
  # update_all(column: Arel.sql(default)) also works in newer versions of Active Record
446
446
  update_expr = "#{quote_column_if_needed(column)} = #{default}"
447
- "#{model}.unscoped.in_batches do |relation| \n relation.where(#{column}: nil).update_all(#{update_expr.inspect})\n sleep(0.01)\n end"
447
+ "#{model}.unscoped.in_batches(of: 10000) do |relation| \n relation.where(#{column}: nil).update_all(#{update_expr.inspect})\n sleep(0.01)\n end"
448
448
  else
449
- "#{model}.unscoped.in_batches do |relation| \n relation.update_all #{column}: #{default.inspect}\n sleep(0.01)\n end"
449
+ "#{model}.unscoped.in_batches(of: 10000) do |relation| \n relation.where(#{column}: nil).update_all #{column}: #{default.inspect}\n sleep(0.01)\n end"
450
450
  end
451
451
  end
452
452
 
@@ -1,7 +1,7 @@
1
1
  module StrongMigrations
2
2
  module SafeMethods
3
3
  def safe_by_default_method?(method)
4
- StrongMigrations.safe_by_default && [:add_index, :add_belongs_to, :add_reference, :remove_index, :add_foreign_key, :add_check_constraint, :change_column_null].include?(method)
4
+ StrongMigrations.safe_by_default && !version_safe? && [:add_index, :add_belongs_to, :add_reference, :remove_index, :add_foreign_key, :add_check_constraint, :change_column_null].include?(method)
5
5
  end
6
6
 
7
7
  def safe_add_index(*args, **options)
@@ -76,11 +76,38 @@ module StrongMigrations
76
76
  end
77
77
  end
78
78
 
79
- def safe_change_column_null(add_args, validate_args, change_args, remove_args, default, constraints)
79
+ def safe_change_column_null(add_args, validate_args, change_args, remove_args, table, column, default, constraints)
80
80
  @migration.reversible do |dir|
81
81
  dir.up do
82
82
  unless default.nil?
83
- raise Error, "default value not supported yet with safe_by_default"
83
+ # TODO search for parent model if needed
84
+ if connection.pool != ActiveRecord::Base.connection_pool
85
+ raise_error :change_column_null,
86
+ code: backfill_code(table, column, default)
87
+ end
88
+
89
+ model =
90
+ Class.new(ActiveRecord::Base) do
91
+ self.table_name = table
92
+
93
+ def self.to_s
94
+ "Backfill"
95
+ end
96
+ end
97
+
98
+ update_sql =
99
+ model.connection_pool.with_connection do |c|
100
+ quoted_column = c.quote_column_name(column)
101
+ quoted_default = c.quote_default_expression(default, c.send(:column_for, table, column))
102
+ "#{quoted_column} = #{quoted_default}"
103
+ end
104
+
105
+ @migration.say("Backfilling default")
106
+ disable_transaction
107
+ model.unscoped.in_batches(of: 10000) do |relation|
108
+ relation.where(column => nil).update_all(update_sql)
109
+ sleep(0.01)
110
+ end
84
111
  end
85
112
 
86
113
  add_options = add_args.extract_options!
@@ -1,3 +1,3 @@
1
1
  module StrongMigrations
2
- VERSION = "2.2.0"
2
+ VERSION = "2.2.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: 2.2.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
@@ -9,7 +9,7 @@ authors:
9
9
  - David Waller
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2025-02-01 00:00:00.000000000 Z
12
+ date: 2025-03-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord