online_migrations 0.18.0 → 0.19.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/docs/background_data_migrations.md +10 -0
- data/docs/background_schema_migrations.md +10 -0
- data/lib/online_migrations/background_migrations/migration.rb +12 -0
- data/lib/online_migrations/background_migrations/migration_job.rb +1 -0
- data/lib/online_migrations/background_migrations/migration_job_status_validator.rb +3 -3
- data/lib/online_migrations/background_migrations/migration_runner.rb +3 -2
- data/lib/online_migrations/background_migrations/migration_status_validator.rb +5 -4
- data/lib/online_migrations/background_schema_migrations/migration.rb +12 -0
- data/lib/online_migrations/background_schema_migrations/migration_helpers.rb +26 -17
- data/lib/online_migrations/background_schema_migrations/migration_runner.rb +2 -0
- data/lib/online_migrations/background_schema_migrations/migration_status_validator.rb +3 -3
- data/lib/online_migrations/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 259343358d51032a296c3745f92aa0e7ab5fc8f2d5dc6cd124873876e523d525
|
4
|
+
data.tar.gz: d07240d06faac048145ae8f54da1a947d197f9ff40bb2adf276625eb76bcafb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c67adbf061095fdcbb5fcaa5305f671d81cf434dc182bfe9e43d66dae41f87d24cf5a79bb4aca160a955c74dff20de5e217b20731fe022df1dece6246e0239c0
|
7
|
+
data.tar.gz: 705397632e3afe3628776e854f74ff0ee70ada9d73d0d9e84c113289623d6240fc79b7823ca0dc05d3dab8dfd90c421c667b60ee394dbfb03c49afabd31ddc95
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
## master (unreleased)
|
2
2
|
|
3
|
+
## 0.19.1 (2024-05-24)
|
4
|
+
|
5
|
+
- Fix `add_index_in_background` to be idempotent
|
6
|
+
|
7
|
+
## 0.19.0 (2024-05-21)
|
8
|
+
|
9
|
+
- Add ability to cancel background migrations
|
10
|
+
|
3
11
|
## 0.18.0 (2024-05-07)
|
4
12
|
|
5
13
|
- 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
|
@@ -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
|
@@ -6,14 +6,24 @@ module OnlineMigrations
|
|
6
6
|
def add_index_in_background(table_name, column_name, **options)
|
7
7
|
migration_options = options.extract!(:max_attempts, :statement_timeout, :connection_class_name)
|
8
8
|
|
9
|
+
options[:algorithm] = :concurrently
|
10
|
+
index, algorithm, if_not_exists = add_index_options(table_name, column_name, **options)
|
11
|
+
|
12
|
+
# Need to check this first, because `index_exists?` does not check for `:where`s.
|
13
|
+
if index_name_exists?(table_name, index.name)
|
14
|
+
Utils.raise_or_say(<<~MSG)
|
15
|
+
Index creation was not enqueued because the index with name '#{index.name}' already exists.
|
16
|
+
This can be due to an aborted migration or you need to explicitly provide another name
|
17
|
+
via `:name` option.
|
18
|
+
MSG
|
19
|
+
return
|
20
|
+
end
|
21
|
+
|
9
22
|
if index_exists?(table_name, column_name, **options)
|
10
23
|
Utils.raise_or_say("Index creation was not enqueued because the index already exists.")
|
11
24
|
return
|
12
25
|
end
|
13
26
|
|
14
|
-
options[:algorithm] = :concurrently
|
15
|
-
index, algorithm, if_not_exists = add_index_options(table_name, column_name, **options)
|
16
|
-
|
17
27
|
create_index = ActiveRecord::ConnectionAdapters::CreateIndexDefinition.new(index, algorithm, if_not_exists)
|
18
28
|
schema_creation = ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaCreation.new(self)
|
19
29
|
definition = schema_creation.accept(create_index)
|
@@ -77,22 +87,21 @@ module OnlineMigrations
|
|
77
87
|
# @private
|
78
88
|
def create_background_schema_migration(migration_name, table_name, **options)
|
79
89
|
options.assert_valid_keys(:definition, :max_attempts, :statement_timeout, :connection_class_name)
|
80
|
-
migration = Migration.new(migration_name: migration_name, table_name: table_name, **options)
|
81
|
-
|
82
|
-
shards = Utils.shard_names(migration.connection_class)
|
83
|
-
if shards.size > 1
|
84
|
-
migration.children = shards.map do |shard|
|
85
|
-
child = migration.dup
|
86
|
-
child.shard = shard
|
87
|
-
child
|
88
|
-
end
|
89
90
|
|
90
|
-
|
91
|
-
|
91
|
+
Migration.find_or_create_by!(migration_name: migration_name, shard: nil) do |migration|
|
92
|
+
migration.assign_attributes(**options, table_name: table_name)
|
92
93
|
|
93
|
-
|
94
|
-
|
95
|
-
|
94
|
+
shards = Utils.shard_names(migration.connection_class)
|
95
|
+
if shards.size > 1
|
96
|
+
migration.children = shards.map do |shard|
|
97
|
+
child = migration.dup
|
98
|
+
child.shard = shard
|
99
|
+
child
|
100
|
+
end
|
101
|
+
|
102
|
+
migration.composite = true
|
103
|
+
end
|
104
|
+
end
|
96
105
|
end
|
97
106
|
end
|
98
107
|
end
|
@@ -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)
|
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.
|
4
|
+
version: 0.19.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-05-
|
11
|
+
date: 2024-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|