online_migrations 0.36.1 → 0.37.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3d28d3f9893b7c26240f94069032fa65ede6b14a1fb79b8c205111b8eedd934
4
- data.tar.gz: da807a14d87705650c05bb7d3151129c534a3c50344b60d0e8e865cdecaa04b5
3
+ metadata.gz: cdad1569af01bacbaa092898b51ba64ad8f6c9746c091441af38b11921bbe2cc
4
+ data.tar.gz: 7f3d23f9e0d5b78fb958322e852bcc274e73ef9c035e05e2e072bec1ebcca086
5
5
  SHA512:
6
- metadata.gz: e8101340c889e837247992f15201b6d1065ae9ea00493f245aad1fe092a7dd2def02965882a5cd29acb9af21504959fd8bbfd95c0f48f911af284ae8fa87baee
7
- data.tar.gz: 916c40fec0d7bfe7d08d7402e13ad2b56989921074f566474aeef0280579064ea92377a5b621c427b64205ecf098f3868c104be5772e3a5dd76b68a76f1a16e0
6
+ metadata.gz: 2fce7817c096a2ca24fed7f8f4984a28bb12a5b4992dd395da4b6ae843ed39684f0917ad0f112c81d7b46ee178a2f3424d7ac9426d32b8ed4a1a83251398985f
7
+ data.tar.gz: e2cd67a8215dbb287ebdd306c574cbbbfaaafcd5c55b0fb2ec2f393f363870ad3e228dd83fedca3b44287baf148504edf2ca191103e179b63e92675636328c4b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## master (unreleased)
2
2
 
3
+ ## 0.37.0 (2026-09-23)
4
+
5
+ - Add ability to restart background migrations
6
+ - Fix collecting referenced tables when using `foreign_key: { to_table: ... }`
7
+
3
8
  ## 0.36.1 (2026-09-23)
4
9
 
5
10
  - Fix compatibility with future Rails 8.2
@@ -354,6 +354,18 @@ migration.retry # => `true` if scheduled to be retried, `false` - if not
354
354
 
355
355
  The migration will be retried on the next Scheduler run.
356
356
 
357
+ ## Restarting a migration
358
+
359
+ To restart a migration, run:
360
+
361
+ ```ruby
362
+ migration = OnlineMigrations::DataMigrations::Migration.find(id)
363
+ migration.force_restart
364
+ ```
365
+
366
+ It ignores any previous status and marks the migration for the restart. Can be useful if the migration is stuck or
367
+ you need to rerun an already finished migration etc.
368
+
357
369
  ## Configuring
358
370
 
359
371
  There are a few configurable options for the data migrations. Custom configurations should be placed in a `online_migrations.rb` initializer.
@@ -77,7 +77,7 @@ You shouldn't depend on the schema until the background schema migration is fini
77
77
  To retry a failed migration, run:
78
78
 
79
79
  ```ruby
80
- migration = OnlineMigrations::BackgroundSchemaMigrations::Migration.find(id)
80
+ migration = OnlineMigrations::SchemaMigrations::Migration.find(id)
81
81
  migration.retry # => `true` if scheduled to be retried, `false` - if not
82
82
  ```
83
83
 
@@ -88,10 +88,22 @@ The migration will be retried on the next Scheduler run.
88
88
  To cancel an existing migration from future performing, run:
89
89
 
90
90
  ```ruby
91
- migration = OnlineMigrations::BackgroundSchemaMigrations::Migration.find(id)
91
+ migration = OnlineMigrations::SchemaMigrations::Migration.find(id)
92
92
  migration.cancel
93
93
  ```
94
94
 
95
+ ## Restarting a migration
96
+
97
+ To restart a migration, run:
98
+
99
+ ```ruby
100
+ migration = OnlineMigrations::SchemaMigrations::Migration.find(id)
101
+ migration.force_restart
102
+ ```
103
+
104
+ It ignores any previous status and marks the migration for the restart. Can be useful if the migration is stuck or
105
+ you need to rerun a cancelled migration.
106
+
95
107
  ## Instrumentation
96
108
 
97
109
  Background schema migrations use the [ActiveSupport::Notifications](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html) API.
@@ -156,7 +168,7 @@ Check the [source code](https://github.com/fatkodima/online_migrations/blob/mast
156
168
  **Note**: You can dynamically change certain migration parameters while the migration is run.
157
169
  For example,
158
170
  ```ruby
159
- migration = OnlineMigrations::BackgroundSchemaMigrations::Migration.find(id)
171
+ migration = OnlineMigrations::SchemaMigrations::Migration.find(id)
160
172
  migration.update!(
161
173
  statement_timeout: 2.hours, # The statement timeout value used when running the migration
162
174
  max_attempts: 10 # The # of attempts the failing migration will be retried
@@ -182,7 +194,7 @@ end
182
194
  The error handler should be a lambda that accepts 2 arguments:
183
195
 
184
196
  * `error`: The exception that was raised.
185
- * `errored_migration`: An `OnlineMigrations::BackgroundSchemaMigrations::Migration` object that represents a failed migration.
197
+ * `errored_migration`: An `OnlineMigrations::SchemaMigrations::Migration` object that represents a failed migration.
186
198
 
187
199
  ### Multiple databases and sharding
188
200
 
@@ -240,6 +240,24 @@ module OnlineMigrations
240
240
  true
241
241
  end
242
242
 
243
+ # Force restart this data migration.
244
+ #
245
+ def force_restart
246
+ update_columns(
247
+ status: :pending,
248
+ cursor: nil,
249
+ jid: nil,
250
+ started_at: nil,
251
+ finished_at: nil,
252
+ tick_total: nil,
253
+ tick_count: 0,
254
+ time_running: 0.0,
255
+ error_class: nil,
256
+ error_message: nil,
257
+ backtrace: nil
258
+ )
259
+ end
260
+
243
261
  # @private
244
262
  def persist_progress(cursor, number_of_ticks, duration)
245
263
  update!(
@@ -135,6 +135,20 @@ module OnlineMigrations
135
135
  end
136
136
  end
137
137
 
138
+ # Force restart this schema migration.
139
+ #
140
+ def force_restart
141
+ update_columns(
142
+ status: :pending,
143
+ started_at: nil,
144
+ finished_at: nil,
145
+ attempts: 0,
146
+ error_class: nil,
147
+ error_message: nil,
148
+ backtrace: nil
149
+ )
150
+ end
151
+
138
152
  def index_addition?
139
153
  definition.match?(/create (unique )?index/i)
140
154
  end
@@ -490,9 +490,10 @@ module OnlineMigrations
490
490
  bad_index = index && !concurrently_set
491
491
 
492
492
  foreign_key = options.fetch(:foreign_key, false)
493
+ foreign_key_options = foreign_key.is_a?(Hash) ? foreign_key : {}
493
494
 
494
495
  if foreign_key
495
- foreign_table_name = Utils.foreign_table_name(ref_name, options)
496
+ foreign_table_name = Utils.foreign_table_name(ref_name, foreign_key_options)
496
497
  @foreign_key_tables << foreign_table_name.to_s
497
498
  end
498
499
 
@@ -519,9 +520,10 @@ module OnlineMigrations
519
520
 
520
521
  def add_reference_concurrently(table_name, ref_name, **options)
521
522
  foreign_key = options.fetch(:foreign_key, false)
523
+ foreign_key_options = foreign_key.is_a?(Hash) ? foreign_key : {}
522
524
 
523
525
  if foreign_key
524
- foreign_table_name = Utils.foreign_table_name(ref_name, options)
526
+ foreign_table_name = Utils.foreign_table_name(ref_name, foreign_key_options)
525
527
  @foreign_key_tables << foreign_table_name.to_s
526
528
  end
527
529
 
@@ -529,7 +531,6 @@ module OnlineMigrations
529
531
  type = (options[:type] || :bigint).to_sym
530
532
  column_name = "#{ref_name}_id"
531
533
 
532
- foreign_key_options = foreign_key.is_a?(Hash) ? foreign_key : {}
533
534
  check_mismatched_foreign_key_type(table_name, column_name, type, **foreign_key_options)
534
535
  end
535
536
  end
@@ -18,9 +18,12 @@ module OnlineMigrations
18
18
  end
19
19
 
20
20
  def references(*ref_names, **options)
21
- if options[:foreign_key]
21
+ foreign_key = options[:foreign_key]
22
+
23
+ if foreign_key
24
+ foreign_key_options = foreign_key.is_a?(Hash) ? foreign_key : {}
22
25
  ref_names.each do |ref_name|
23
- @referenced_tables << Utils.foreign_table_name(ref_name, options)
26
+ @referenced_tables << Utils.foreign_table_name(ref_name, foreign_key_options)
24
27
  end
25
28
  end
26
29
  end
@@ -79,6 +79,14 @@ module OnlineMigrations
79
79
  new_column = old_column.dup
80
80
  # Active Record defines only reader for :name
81
81
  new_column.instance_variable_set(:@name, new_column_name)
82
+
83
+ # https://github.com/rails/rails/commit/0c5b92705960ceff44324e69a921e760f1646503
84
+ # New in Active Record 8.2+.
85
+ if new_column.respond_to?(:slot)
86
+ max_slot = columns.map(&:slot).max
87
+ new_column.instance_variable_set(:@slot, max_slot + 1)
88
+ end
89
+
82
90
  # Correspond to the Active Record freezing of each column
83
91
  columns << new_column.freeze
84
92
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OnlineMigrations
4
- VERSION = "0.36.1"
4
+ VERSION = "0.37.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: online_migrations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.36.1
4
+ version: 0.37.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - fatkodima
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  requirements: []
119
- rubygems_version: 4.0.16
119
+ rubygems_version: 4.0.20
120
120
  specification_version: 4
121
121
  summary: Catch unsafe PostgreSQL migrations in development and run them easier in
122
122
  production