online_migrations 0.19.6 → 0.20.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: cd8711d56259535675633074868019a5c4b3de068bb30d783c9ae16dc906afb1
4
- data.tar.gz: 3b417e176d143092d69e4551c1f96a32e1a2acf1d74d268fc33f9b5a598be553
3
+ metadata.gz: 2fd0059b044a120c61044268bfc32c3622163ae1871f85655ae51fe3cfb99c60
4
+ data.tar.gz: 1e1842002d3d229014011ee2a6af8eb122eaa7a6822912bec2d276f67e6555b4
5
5
  SHA512:
6
- metadata.gz: ad26fd5662f2636479b697933042b8d7e60b4ee42f136ddeb8cff8256c65a4491b8017d09d2da849faf65cb6f8d4ecc95595816b7669c8364e9f31bbe75232c4
7
- data.tar.gz: 21fec027462ff6f96baaf0fc65d186fc68fefbb50fd3de6af8f561aae691e6f9909d70d7053557d39f0a56ea498841816c2853cce0b1acef161411b2d968b196
6
+ metadata.gz: 590aa0a0d36f94316cf02c0b3c8ec2dcf7be08e17cd687eb889d6fb9d8a4cfc892424cffb49c1988691720a7cbede5d58f1a2e712a0ddd8ab8545ce6df6395ed
7
+ data.tar.gz: a981da87aae5a6e0a5872be04ec9839285fa6b53693c73a4f8ceac354d30f5623d7aef473a8d5c07992e14654083253d3603881f031877692c6718c8f5588407
data/CHANGELOG.md CHANGED
@@ -1,8 +1,18 @@
1
1
  ## master (unreleased)
2
2
 
3
+ ## 0.20.1 (2024-11-05)
4
+
5
+ - Fix background data migrations to work with `includes`/`eager_load` on relation
6
+ - Fix problem with running migrations for `activerecord` 8.0.0.rc2
7
+
8
+ ## 0.20.0 (2024-10-21)
9
+
10
+ - Enhance columns removal check to check indexes `where` and `include` options
11
+ - Do not wait before running retried background migrations
12
+
3
13
  ## 0.19.6 (2024-09-26)
4
14
 
5
- - Fix `add_refernce_concurrently` when adding non polymorphic references
15
+ - Fix `add_reference_concurrently` when adding non polymorphic references
6
16
  - Fix progress for background migrations with small number of records
7
17
 
8
18
  ## 0.19.5 (2024-09-20)
data/docs/configuring.md CHANGED
@@ -51,7 +51,7 @@ config.check_down = true
51
51
  You can customize specific error messages:
52
52
 
53
53
  ```ruby
54
- config.error_messages[:add_column_default] = "Your custom instructions"
54
+ config.error_messages[:add_column_with_default] = "Your custom instructions"
55
55
  ```
56
56
 
57
57
  Check the [source code](https://github.com/fatkodima/online_migrations/blob/master/lib/online_migrations/error_messages.rb) for the list of keys.
@@ -4,18 +4,25 @@ module <%= migrations_module %>
4
4
  <% module_namespacing do -%>
5
5
  class <%= class_name %> < OnlineMigrations::BackgroundMigration
6
6
  def relation
7
- # ActiveRecord::Relation to be iterated over
7
+ # return ActiveRecord::Relation to be iterated over
8
8
  end
9
9
 
10
10
  def process_batch(relation)
11
+ # 'relation' is an ActiveRecord::Relation instance containing a batch to process.
12
+ #
11
13
  # The work to be done in a single iteration of the background migration.
12
14
  # This should be idempotent, as the same batch may be processed more
13
15
  # than once if the background migration is interrupted and resumed.
14
16
  end
15
17
 
18
+ # Optional.
16
19
  def count
17
- # Optionally, define the number of rows that will be iterated over.
20
+ # Define the number of rows that will be iterated over.
18
21
  # This is used to track the background migration's progress.
22
+ # Usually this is configured to be 'relation.count'.
23
+ #
24
+ # If the query to calculate the count is heavy, it is recommended to use
25
+ # some approximate hardcode number or skip defining this method entirely.
19
26
  end
20
27
  end
21
28
  <% end -%>
@@ -17,7 +17,7 @@ module OnlineMigrations
17
17
  scope :completed, -> { where(status: [:failed, :succeeded]) }
18
18
  scope :stuck, -> do
19
19
  timeout = OnlineMigrations.config.background_migrations.stuck_jobs_timeout
20
- active.where("updated_at <= ?", timeout.seconds.ago)
20
+ running.where("updated_at <= ?", timeout.seconds.ago)
21
21
  end
22
22
 
23
23
  scope :retriable, -> do
@@ -115,7 +115,7 @@ module OnlineMigrations
115
115
  if min_value && max_value
116
116
  create_migration_job!(min_value, max_value)
117
117
  else
118
- migration.migration_jobs.retriable.first
118
+ migration.migration_jobs.enqueued.first || migration.migration_jobs.retriable.first
119
119
  end
120
120
  end
121
121
 
@@ -123,7 +123,7 @@ module OnlineMigrations
123
123
  return false if composite?
124
124
 
125
125
  stuck_timeout = (statement_timeout || 1.day) + 10.minutes
126
- (enqueued? || running?) && updated_at <= stuck_timeout.seconds.ago
126
+ running? && updated_at <= stuck_timeout.seconds.ago
127
127
  end
128
128
 
129
129
  # Mark this migration as ready to be processed again.
@@ -134,7 +134,7 @@ module OnlineMigrations
134
134
  if composite? && failed?
135
135
  children.failed.each(&:retry)
136
136
  update!(
137
- status: self.class.statuses[:running],
137
+ status: self.class.statuses[:enqueued],
138
138
  finished_at: nil
139
139
  )
140
140
  true
@@ -19,7 +19,8 @@ module OnlineMigrations
19
19
  end
20
20
 
21
21
  relation = apply_limits(self.relation, column, start, finish, order)
22
- base_relation = relation.reselect(column).reorder(column => order)
22
+ unscopes = Utils.ar_version < 7.1 ? [:includes] : [:includes, :preload, :eager_load]
23
+ base_relation = relation.unscope(*unscopes).reselect(column).reorder(column => order)
23
24
 
24
25
  start_id = start || begin
25
26
  start_row = base_relation.uncached { base_relation.first }
@@ -118,7 +118,7 @@ module OnlineMigrations
118
118
  type_cast_functions[column_name] = type_cast_function if type_cast_function
119
119
  tmp_column_name = conversions[column_name]
120
120
 
121
- if raw_connection.server_version >= 11_00_00
121
+ if database_version >= 11_00_00
122
122
  if primary_key(table_name) == column_name.to_s && old_col.type == :integer
123
123
  # For PG < 11 and Primary Key conversions, setting a column as the PK
124
124
  # converts even check constraints to NOT NULL column constraints
@@ -497,7 +497,7 @@ module OnlineMigrations
497
497
  def __set_not_null(table_name, column_name)
498
498
  # For PG >= 12 we can "promote" CHECK constraint to NOT NULL constraint:
499
499
  # https://github.com/postgres/postgres/commit/bbb96c3704c041d139181c6601e5bc770e045d26
500
- if raw_connection.server_version >= 12_00_00
500
+ if database_version >= 12_00_00
501
501
  execute(<<~SQL)
502
502
  ALTER TABLE #{quote_table_name(table_name)}
503
503
  ALTER #{quote_column_name(column_name)}
@@ -453,13 +453,7 @@ module OnlineMigrations
453
453
 
454
454
  if !new_table?(table_name)
455
455
  indexes = connection.indexes(table_name).select do |index|
456
- case index.columns
457
- when String
458
- # Expression index
459
- columns.any? { |column| index.columns.include?(column) }
460
- else
461
- (index.columns & columns).any?
462
- end
456
+ columns.any? { |column| index_include_column?(index, column) }
463
457
  end
464
458
 
465
459
  raise_error :remove_column,
@@ -892,6 +886,14 @@ module OnlineMigrations
892
886
  postgresql_version < Gem::Version.new("14.4")
893
887
  end
894
888
 
889
+ def index_include_column?(index, column)
890
+ # Expression index
891
+ (index.columns.is_a?(String) && index.columns.include?(column)) ||
892
+ index.columns.include?(column) ||
893
+ (Utils.ar_version >= 7.1 && index.include && index.include.include?(column)) ||
894
+ (index.where && index.where.include?(column))
895
+ end
896
+
895
897
  def run_custom_checks(method, args)
896
898
  OnlineMigrations.config.checks.each do |options, check|
897
899
  if !options[:start_after] || version > options[:start_after]
@@ -3,7 +3,7 @@
3
3
  module OnlineMigrations
4
4
  # @private
5
5
  module DatabaseTasks
6
- def migrate(*)
6
+ def migrate(*, **)
7
7
  super
8
8
  rescue => e
9
9
  if e.cause.is_a?(OnlineMigrations::Error)
@@ -279,8 +279,8 @@ end",
279
279
 
280
280
  remove_column:
281
281
  "<% if !small_table && indexes.any? %>
282
- Removing a column will automatically remove all of the indexes that involved the removed column.
283
- But the indexes would be removed non-concurrently, so you need to safely remove the indexes first:
282
+ Removing a column will automatically remove all the indexes that include this column.
283
+ Indexes will be removed non-concurrently, so you need to safely remove them first:
284
284
 
285
285
  class <%= migration_name %>RemoveIndexes < <%= migration_parent %>
286
286
  disable_ddl_transaction!
@@ -439,7 +439,7 @@ module OnlineMigrations
439
439
  def add_column_with_default(table_name, column_name, type, **options)
440
440
  default = options.fetch(:default)
441
441
 
442
- if raw_connection.server_version >= 11_00_00 && !Utils.volatile_default?(self, type, default)
442
+ if database_version >= 11_00_00 && !Utils.volatile_default?(self, type, default)
443
443
  add_column(table_name, column_name, type, **options)
444
444
  else
445
445
  __ensure_not_in_transaction!
@@ -465,7 +465,7 @@ module OnlineMigrations
465
465
  add_not_null_constraint(table_name, column_name, validate: false)
466
466
  validate_not_null_constraint(table_name, column_name)
467
467
 
468
- if raw_connection.server_version >= 12_00_00
468
+ if database_version >= 12_00_00
469
469
  # In PostgreSQL 12+ it is safe to "promote" a CHECK constraint to `NOT NULL` for the column
470
470
  change_column_null(table_name, column_name, false)
471
471
  remove_not_null_constraint(table_name, column_name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OnlineMigrations
4
- VERSION = "0.19.6"
4
+ VERSION = "0.20.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: online_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.6
4
+ version: 0.20.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - fatkodima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-26 00:00:00.000000000 Z
11
+ date: 2024-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord