delayed 2.0.1 → 2.0.2
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 +4 -4
- data/lib/delayed/helpers/migration.rb +13 -18
- data/lib/delayed/version.rb +1 -1
- data/spec/delayed/helpers/migration_spec.rb +33 -18
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 16d1a1a8e3f41bee282a4d725afdf3be6419fceba8c5cef58a3eb35a7a406823
|
|
4
|
+
data.tar.gz: 9ad241b6880c13997ea7b58ba2bcfba02a1beb6692b050698fac30301cc9c90c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a63a4f0a56f26c9b5bb3a987b6c2877068016823b21efb059744effc0eeb68255b4b193e7f0e0292f6d790e12138ad8c57bab032a7f1f3c1a89e2c533db45df6
|
|
7
|
+
data.tar.gz: 7912ee6d5d633293a2f1707d890c2fd5a60cbdb1c44ec215a8d2414db35902a272e4caad4ece1947c6475f4e90a7b5c1fe54ef320549681c04c948d7786cd4a8
|
|
@@ -20,13 +20,17 @@ module Delayed
|
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
def upsert_index(*args, **opts)
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
with_retry_loop(**opts) do
|
|
24
|
+
dir(:up) { _add_or_replace_index(*args, **opts) }
|
|
25
|
+
dir(:down) { _drop_index_if_exists(*args, **opts) }
|
|
26
|
+
end
|
|
25
27
|
end
|
|
26
28
|
|
|
27
29
|
def remove_index_if_exists(*args, **opts)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
with_retry_loop(**opts) do
|
|
31
|
+
dir(:up) { _drop_index_if_exists(*args, **opts) }
|
|
32
|
+
dir(:down) { _add_or_replace_index(*args, **opts) }
|
|
33
|
+
end
|
|
30
34
|
end
|
|
31
35
|
|
|
32
36
|
RETRY_EXCEPTIONS = [
|
|
@@ -48,7 +52,7 @@ module Delayed
|
|
|
48
52
|
end
|
|
49
53
|
end
|
|
50
54
|
|
|
51
|
-
def with_timeouts(statement_timeout: 1.minute, lock_timeout: 5.seconds)
|
|
55
|
+
def with_timeouts(statement_timeout: 1.minute, lock_timeout: 5.seconds, **_opts)
|
|
52
56
|
dir(:both) { set_timeouts!(statement_timeout: statement_timeout, lock_timeout: lock_timeout) }
|
|
53
57
|
yield
|
|
54
58
|
ensure
|
|
@@ -61,24 +65,15 @@ module Delayed
|
|
|
61
65
|
index = _lookup_index(table, columns, **opts)
|
|
62
66
|
if index && !_index_matches?(index, **opts)
|
|
63
67
|
Delayed.logger.warn("Recreating index #{index.name} (is invalid or does not match desired options)")
|
|
64
|
-
|
|
68
|
+
remove_index(table, name: index.name)
|
|
65
69
|
end
|
|
66
|
-
|
|
70
|
+
opts = opts.except(:wait_timeout, :statement_timeout, :lock_timeout)
|
|
71
|
+
add_index(table, columns, **opts) if !index || !_index_matches?(index, **opts)
|
|
67
72
|
end
|
|
68
73
|
|
|
69
74
|
def _drop_index_if_exists(table, columns, **opts)
|
|
70
75
|
index = _lookup_index(table, columns, **opts)
|
|
71
|
-
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def _add_index(*args, **opts)
|
|
75
|
-
index_opts = opts.slice!(:wait_timeout, :statement_timeout, :lock_timeout)
|
|
76
|
-
with_retry_loop(**opts) { add_index(*args, **index_opts) }
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
def _drop_index(table, name:, **opts)
|
|
80
|
-
opts.slice!(:wait_timeout, :statement_timeout, :lock_timeout)
|
|
81
|
-
with_retry_loop(**opts) { remove_index(table, name: name) }
|
|
76
|
+
remove_index(table, name: index.name) if index
|
|
82
77
|
end
|
|
83
78
|
|
|
84
79
|
def _lookup_index(table, columns, **opts)
|
data/lib/delayed/version.rb
CHANGED
|
@@ -33,35 +33,50 @@ describe Delayed::Helpers::Migration do
|
|
|
33
33
|
migration.migration_start = Delayed::Job.db_time_now
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
describe '#
|
|
36
|
+
describe '#upsert_index retry behavior' do
|
|
37
37
|
it 'raises exception when wait_timeout is exceeded based on @migration_start' do
|
|
38
|
-
# Simulate migration that started 6 minutes ago
|
|
39
38
|
migration.migration_start = Delayed::Job.db_time_now - 6.minutes
|
|
40
39
|
|
|
40
|
+
allow(migration).to receive(:add_index).and_raise(ActiveRecord::LockWaitTimeout)
|
|
41
|
+
allow(migration.connection).to receive(:indexes).and_return([])
|
|
42
|
+
|
|
41
43
|
expect {
|
|
42
|
-
migration.
|
|
43
|
-
raise ActiveRecord::LockWaitTimeout
|
|
44
|
-
end
|
|
44
|
+
migration.upsert_index(:delayed_jobs, :name, wait_timeout: 5.minutes)
|
|
45
45
|
}.to raise_error(ActiveRecord::LockWaitTimeout)
|
|
46
46
|
end
|
|
47
47
|
|
|
48
|
-
it '
|
|
49
|
-
|
|
48
|
+
it 're-checks for invalid index and drops it before retrying after timeout' do
|
|
49
|
+
add_index_calls = 0
|
|
50
|
+
remove_index_calls = 0
|
|
51
|
+
lookup_calls = 0
|
|
50
52
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
invalid_opts = ActiveRecord.version >= Gem::Version.new('7.1.0') ? { valid?: false } : { unique: true }
|
|
54
|
+
invalid_index = instance_double(
|
|
55
|
+
ActiveRecord::ConnectionAdapters::IndexDefinition,
|
|
56
|
+
name: 'test_idx',
|
|
57
|
+
columns: ['name'],
|
|
58
|
+
**invalid_opts,
|
|
55
59
|
)
|
|
56
60
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
allow(migration.connection).to receive(:indexes) do
|
|
62
|
+
lookup_calls += 1
|
|
63
|
+
lookup_calls == 2 ? [invalid_index] : []
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
allow(migration).to receive(:add_index) do |*_args|
|
|
67
|
+
add_index_calls += 1
|
|
68
|
+
raise ActiveRecord::StatementTimeout, 'timeout' if add_index_calls == 1
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
allow(migration).to receive(:remove_index) do |*_args|
|
|
72
|
+
remove_index_calls += 1
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
migration.upsert_index(:delayed_jobs, :name, name: 'test_idx', wait_timeout: 5.minutes)
|
|
63
76
|
|
|
64
|
-
expect(
|
|
77
|
+
expect(lookup_calls).to eq(3)
|
|
78
|
+
expect(remove_index_calls).to eq(1)
|
|
79
|
+
expect(add_index_calls).to eq(2)
|
|
65
80
|
end
|
|
66
81
|
end
|
|
67
82
|
end
|