online_migrations 0.18.0 → 0.19.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: 31bbf9d9c3a619e7a878717b8d4bcffae2e71e819bd4d2ea140495fdbb951452
4
- data.tar.gz: f9a92ef118577b4bd2e837c95687d19a6d69fba988e61625f51bf2807a855e62
3
+ metadata.gz: 57661cb5e096da3eaed31d4dbe5cda862f9f75271b59c28045412642acaf786b
4
+ data.tar.gz: 50ed46eaa89a4ee95ecd8b249b0fce616047006c91339dbc1069e572a96f0454
5
5
  SHA512:
6
- metadata.gz: b786ffdeb3d57f72d2582a222915b21c2c09f2755b049d99b9faf68fd1fe5a0655a577b37bb427473af9e24c564b5db825051da86a87e82787c29122c7e5053a
7
- data.tar.gz: 33ec438be94ff7b69dee15770a730e6d946b2e1ccf0fb71a3be8819690701b7057d4ba4d5f4b297810265be83225f78b5b7e990b91e5affc35d5961a807ffc93
6
+ metadata.gz: be91a43493896dbf0787cf9045c0ac187920a55946b98b0c653b8a50604031aee1bba3b59591fd4c22f7996af05254c71001e098289ded180eec9cdd8ae47d43
7
+ data.tar.gz: 840c35a008b4b949d1d686222d0722f98aa579fb31ccb14ffe3f6bd80147100bbcb323a9bdba657f230efbd3b2a97f799385e21f43218d8dbf19ab9a484d1147
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## master (unreleased)
2
2
 
3
+ ## 0.19.0 (2024-05-21)
4
+
5
+ - Add ability to cancel background migrations
6
+
3
7
  ## 0.18.0 (2024-05-07)
4
8
 
5
9
  - Fix setting `started_at`/`finished_at` for parents of sharded background schema migrations
@@ -237,6 +237,7 @@ Background Migrations can be in various states during its execution:
237
237
  Note: In normal circumstances, this should not be used since background migrations should be run and finished by the scheduler.
238
238
  * **failed**: A migration raises an exception when running.
239
239
  * **succeeded**: A migration finished without error.
240
+ * **cancelled**: A migration was cancelled by the user.
240
241
 
241
242
  To get the progress (assuming `#count` method on background migration class was defined):
242
243
 
@@ -258,6 +259,15 @@ migration.retry # => `true` if scheduled to be retried, `false` - if not
258
259
 
259
260
  The migration will be retried on the next Scheduler run.
260
261
 
262
+ ## Cancelling a migration
263
+
264
+ To cancel an existing migration from future performing, run:
265
+
266
+ ```ruby
267
+ migration = OnlineMigrations::BackgroundMigrations::Migration.find(id)
268
+ migration.cancel
269
+ ```
270
+
261
271
  ## Configuring
262
272
 
263
273
  There are a few configurable options for the Background Migrations. Custom configurations should be placed in a `online_migrations.rb` initializer.
@@ -77,6 +77,15 @@ migration.retry # => `true` if scheduled to be retried, `false` - if not
77
77
 
78
78
  The migration will be retried on the next Scheduler run.
79
79
 
80
+ ## Cancelling a migration
81
+
82
+ To cancel an existing migration from future performing, run:
83
+
84
+ ```ruby
85
+ migration = OnlineMigrations::BackgroundSchemaMigrations::Migration.find(id)
86
+ migration.cancel
87
+ ```
88
+
80
89
  ## Instrumentation
81
90
 
82
91
  Background schema migrations use the [ActiveSupport::Notifications](http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html) API.
@@ -121,6 +130,7 @@ Background Schema Migrations can be in various states during its execution:
121
130
  * **running**: A migration is being performed by a migration executor.
122
131
  * **failed**: A migration raises an exception when running.
123
132
  * **succeeded**: A migration finished without error.
133
+ * **cancelled**: A migration was cancelled by the user.
124
134
 
125
135
  ## Configuring
126
136
 
@@ -15,6 +15,7 @@ module OnlineMigrations
15
15
  :finishing, # The migration is being manually finishing inline by the user.
16
16
  :failed, # The migration raises an exception when running.
17
17
  :succeeded, # The migration finished without error.
18
+ :cancelled, # The migration was cancelled by the user.
18
19
  ]
19
20
 
20
21
  self.table_name = :background_migrations
@@ -98,6 +99,17 @@ module OnlineMigrations
98
99
  end
99
100
  end
100
101
 
102
+ # Overwrite enum's generated method to correctly work for composite migrations.
103
+ def cancelled!
104
+ return super if !composite?
105
+
106
+ transaction do
107
+ super
108
+ children.each { |child| child.cancelled! if !child.succeeded? }
109
+ end
110
+ end
111
+ alias cancel cancelled!
112
+
101
113
  def last_job
102
114
  migration_jobs.order(:max_value).last
103
115
  end
@@ -8,6 +8,7 @@ module OnlineMigrations
8
8
  :running,
9
9
  :failed,
10
10
  :succeeded,
11
+ :cancelled,
11
12
  ]
12
13
 
13
14
  self.table_name = :background_migration_jobs
@@ -5,9 +5,9 @@ module OnlineMigrations
5
5
  # @private
6
6
  class MigrationJobStatusValidator < ActiveModel::Validator
7
7
  VALID_STATUS_TRANSITIONS = {
8
- "enqueued" => ["running"],
9
- "running" => ["succeeded", "failed"],
10
- "failed" => ["enqueued", "running"],
8
+ "enqueued" => ["running", "cancelled"],
9
+ "running" => ["succeeded", "failed", "cancelled"],
10
+ "failed" => ["enqueued", "running", "cancelled"],
11
11
  }
12
12
 
13
13
  def validate(record)
@@ -13,6 +13,7 @@ module OnlineMigrations
13
13
  # Runs one background migration job.
14
14
  def run_migration_job
15
15
  raise "Should not be called on a composite (with sharding) migration" if migration.composite?
16
+ return if migration.cancelled?
16
17
 
17
18
  mark_as_running if migration.enqueued?
18
19
  migration_payload = notifications_payload(migration)
@@ -56,7 +57,7 @@ module OnlineMigrations
56
57
  raise "This method is not intended for use in production environments"
57
58
  end
58
59
 
59
- return if migration.completed?
60
+ return if migration.completed? || migration.cancelled?
60
61
 
61
62
  mark_as_running
62
63
 
@@ -77,7 +78,7 @@ module OnlineMigrations
77
78
  # Keep running until the migration is finished.
78
79
  #
79
80
  def finish
80
- return if migration.completed?
81
+ return if migration.completed? || migration.cancelled?
81
82
 
82
83
  if migration.composite?
83
84
  migration.children.each do |child_migration|
@@ -7,7 +7,7 @@ module OnlineMigrations
7
7
  VALID_STATUS_TRANSITIONS = {
8
8
  # enqueued -> running occurs when the migration starts performing.
9
9
  # enqueued -> paused occurs when the migration is paused before starting.
10
- "enqueued" => ["running", "paused"],
10
+ "enqueued" => ["running", "paused", "cancelled"],
11
11
  # running -> paused occurs when a user pauses the migration as
12
12
  # it's performing.
13
13
  # running -> finishing occurs when a user manually finishes the migration.
@@ -18,15 +18,16 @@ module OnlineMigrations
18
18
  "finishing",
19
19
  "succeeded",
20
20
  "failed",
21
+ "cancelled",
21
22
  ],
22
23
  # finishing -> succeeded occurs when the migration completes successfully.
23
24
  # finishing -> failed occurs when the migration raises an exception when running.
24
- "finishing" => ["succeeded", "failed"],
25
+ "finishing" => ["succeeded", "failed", "cancelled"],
25
26
  # paused -> running occurs when the migration is resumed after being paused.
26
- "paused" => ["running"],
27
+ "paused" => ["running", "cancelled"],
27
28
  # failed -> enqueued occurs when the failed migration jobs are retried after being failed.
28
29
  # failed -> running occurs when the failed migration is retried.
29
- "failed" => ["enqueued", "running"],
30
+ "failed" => ["enqueued", "running", "cancelled"],
30
31
  }
31
32
 
32
33
  def validate(record)
@@ -13,6 +13,7 @@ module OnlineMigrations
13
13
  :running, # The migration is being performed by a migration executor.
14
14
  :failed, # The migration raises an exception when running.
15
15
  :succeeded, # The migration finished without error.
16
+ :cancelled, # The migration was cancelled by the user.
16
17
  ]
17
18
 
18
19
  MAX_IDENTIFIER_LENGTH = 63
@@ -82,6 +83,17 @@ module OnlineMigrations
82
83
  succeeded? || failed?
83
84
  end
84
85
 
86
+ # Overwrite enum's generated method to correctly work for composite migrations.
87
+ def cancelled!
88
+ return super if !composite?
89
+
90
+ transaction do
91
+ super
92
+ children.each { |child| child.cancelled! if !child.succeeded? }
93
+ end
94
+ end
95
+ alias cancel cancelled!
96
+
85
97
  # Returns the progress of the background schema migration.
86
98
  #
87
99
  # @return [Float] value in range from 0.0 to 100.0
@@ -11,6 +11,8 @@ module OnlineMigrations
11
11
  end
12
12
 
13
13
  def run
14
+ return if migration.cancelled?
15
+
14
16
  mark_as_running if migration.enqueued? || migration.failed?
15
17
 
16
18
  if migration.composite?
@@ -6,13 +6,13 @@ module OnlineMigrations
6
6
  class MigrationStatusValidator < ActiveModel::Validator
7
7
  VALID_STATUS_TRANSITIONS = {
8
8
  # enqueued -> running occurs when the migration starts performing.
9
- "enqueued" => ["running"],
9
+ "enqueued" => ["running", "cancelled"],
10
10
  # running -> succeeded occurs when the migration completes successfully.
11
11
  # running -> failed occurs when the migration raises an exception when running and retry attempts exceeded.
12
- "running" => ["succeeded", "failed"],
12
+ "running" => ["succeeded", "failed", "cancelled"],
13
13
  # failed -> enqueued occurs when the failed migration is enqueued to be retried.
14
14
  # failed -> running occurs when the failed migration is retried.
15
- "failed" => ["enqueued", "running"],
15
+ "failed" => ["enqueued", "running", "cancelled"],
16
16
  }
17
17
 
18
18
  def validate(record)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OnlineMigrations
4
- VERSION = "0.18.0"
4
+ VERSION = "0.19.0"
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.18.0
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - fatkodima
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-07 00:00:00.000000000 Z
11
+ date: 2024-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord