Skip to content

Commit 57261fa

Browse files
fix rubocop rules
1 parent 94fa595 commit 57261fa

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

lib/safe-pg-migrations/configuration.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,30 @@ def lock_timeout=(value)
3636
raise 'Setting lock timeout to 0 disables the lock timeout and is dangerous' if value == 0.seconds
3737

3838
unless value.nil? || (value < safe_timeout && value <= max_lock_timeout_for_retry)
39-
raise ArgumentError, "Lock timeout (#{value}) cannot be greater than the safe timeout (#{safe_timeout}) or the max lock timeout for retry (#{max_lock_timeout_for_retry})"
39+
raise ArgumentError, "Lock timeout (#{value}) cannot be greater than the safe timeout (#{safe_timeout}) or the
40+
max lock timeout for retry (#{max_lock_timeout_for_retry})"
4041
end
4142

4243
@lock_timeout = value
4344
end
4445

4546
def safe_timeout=(value)
46-
raise 'Setting safe timeout to 0 or nil disables the safe timeout and is dangerous' unless value && value > 0.seconds
47+
unless value && value > 0.seconds
48+
raise 'Setting safe timeout to 0 or nil disables the safe timeout and is dangerous'
49+
end
4750

4851
unless lock_timeout.nil? || (value > lock_timeout && value >= max_lock_timeout_for_retry)
49-
raise ArgumentError, "Safe timeout (#{value}) cannot be lower than the lock timeout (#{lock_timeout}) or the max lock timeout for retry (#{max_lock_timeout_for_retry})"
52+
raise ArgumentError, "Safe timeout (#{value}) cannot be lower than the lock timeout (#{lock_timeout}) or the
53+
max lock timeout for retry (#{max_lock_timeout_for_retry})"
5054
end
5155

5256
@safe_timeout = value
5357
end
5458

5559
def max_lock_timeout_for_retry=(value)
5660
unless lock_timeout.nil? || (value >= lock_timeout && value <= safe_timeout)
57-
raise ArgumentError, "Max lock timeout for retry (#{value}) cannot be lower than the lock timeout (#{lock_timeout}) and greater than the safe timeout (#{safe_timeout})"
61+
raise ArgumentError, "Max lock timeout for retry (#{value}) cannot be lower than the lock timeout
62+
(#{lock_timeout}) and greater than the safe timeout (#{safe_timeout})"
5863
end
5964

6065
@max_lock_timeout_for_retry = value

lib/safe-pg-migrations/plugins/statement_retrier.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ def retry_if_lock_timeout
2828
retry_delay = SafePgMigrations.config.retry_delay
2929
Helpers::Logger.say "Retrying in #{retry_delay} seconds...", sub_item: true
3030

31-
increase_lock_timeout if SafePgMigrations.config.increase_lock_timeout_on_retry && !SafePgMigrations.config.lock_timeout.nil?
31+
if SafePgMigrations.config.increase_lock_timeout_on_retry && !SafePgMigrations.config.lock_timeout.nil?
32+
increase_lock_timeout
33+
end
3234

3335
sleep retry_delay
3436
Helpers::Logger.say 'Retrying now.', sub_item: true
@@ -37,7 +39,8 @@ def retry_if_lock_timeout
3739
end
3840

3941
def increase_lock_timeout
40-
Helpers::Logger.say " Increasing the lock timeout... Currently set to #{SafePgMigrations.config.lock_timeout}", sub_item: true
42+
Helpers::Logger.say " Increasing the lock timeout... Currently set to #{SafePgMigrations.config.lock_timeout}",
43+
sub_item: true
4144
SafePgMigrations.config.lock_timeout = (SafePgMigrations.config.lock_timeout + lock_timeout_step)
4245
unless SafePgMigrations.config.lock_timeout < SafePgMigrations.config.max_lock_timeout_for_retry
4346
SafePgMigrations.config.lock_timeout = SafePgMigrations.config.max_lock_timeout_for_retry
@@ -46,7 +49,10 @@ def increase_lock_timeout
4649
end
4750

4851
def lock_timeout_step
49-
@lock_timeout_step ||= (SafePgMigrations.config.max_lock_timeout_for_retry - SafePgMigrations.config.lock_timeout) / (SafePgMigrations.config.max_tries - 1)
52+
max_lock_timeout_for_retry = SafePgMigrations.config.max_lock_timeout_for_retry
53+
lock_timeout = SafePgMigrations.config.lock_timeout
54+
max_tries = SafePgMigrations.config.max_tries
55+
@lock_timeout_step ||= (max_lock_timeout_for_retry - lock_timeout) / (max_tries - 1)
5056
end
5157
end
5258
end

test/statement_retrier_test.rb

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,30 @@
33
require 'test_helper'
44

55
class StatementRetrierTest < Minitest::Test
6-
76
def test_lock_timeout_increase_on_retry
87
SafePgMigrations.config.lock_timeout = 0.1.seconds
98
SafePgMigrations.config.increase_lock_timeout_on_retry = true
109

1110
calls = calls_for_lock_timeout_migration
1211

1312
assert_equal [
14-
' -> Retrying in 60 seconds...',
15-
' -> Increasing the lock timeout... Currently set to 0.1',
16-
' -> Lock timeout is now set to 0.325',
17-
' -> Retrying now.',
18-
' -> Retrying in 60 seconds...',
19-
' -> Increasing the lock timeout... Currently set to 0.325',
20-
' -> Lock timeout is now set to 0.55',
21-
' -> Retrying now.',
22-
' -> Retrying in 60 seconds...',
23-
' -> Increasing the lock timeout... Currently set to 0.55',
24-
' -> Lock timeout is now set to 0.775',
25-
' -> Retrying now.',
26-
' -> Retrying in 60 seconds...',
27-
' -> Increasing the lock timeout... Currently set to 0.775',
28-
' -> Lock timeout is now set to 1',
29-
' -> Retrying now.',
30-
], calls[1..-1].map(&:first)
13+
' -> Retrying in 60 seconds...',
14+
' -> Increasing the lock timeout... Currently set to 0.1',
15+
' -> Lock timeout is now set to 0.325',
16+
' -> Retrying now.',
17+
' -> Retrying in 60 seconds...',
18+
' -> Increasing the lock timeout... Currently set to 0.325',
19+
' -> Lock timeout is now set to 0.55',
20+
' -> Retrying now.',
21+
' -> Retrying in 60 seconds...',
22+
' -> Increasing the lock timeout... Currently set to 0.55',
23+
' -> Lock timeout is now set to 0.775',
24+
' -> Retrying now.',
25+
' -> Retrying in 60 seconds...',
26+
' -> Increasing the lock timeout... Currently set to 0.775',
27+
' -> Lock timeout is now set to 1',
28+
' -> Retrying now.',
29+
], calls[1..].map(&:first)
3130
end
3231

3332
def test_no_lock_timeout_increase_on_retry_if_disabled
@@ -37,15 +36,15 @@ def test_no_lock_timeout_increase_on_retry_if_disabled
3736
calls = calls_for_lock_timeout_migration
3837

3938
assert_equal [
40-
' -> Retrying in 60 seconds...',
41-
' -> Retrying now.',
42-
' -> Retrying in 60 seconds...',
43-
' -> Retrying now.',
44-
' -> Retrying in 60 seconds...',
45-
' -> Retrying now.',
46-
' -> Retrying in 60 seconds...',
47-
' -> Retrying now.',
48-
], calls[1..-1].map(&:first)
39+
' -> Retrying in 60 seconds...',
40+
' -> Retrying now.',
41+
' -> Retrying in 60 seconds...',
42+
' -> Retrying now.',
43+
' -> Retrying in 60 seconds...',
44+
' -> Retrying now.',
45+
' -> Retrying in 60 seconds...',
46+
' -> Retrying now.',
47+
], calls[1..].map(&:first)
4948
end
5049

5150
def test_retry_if_lock_timeout
@@ -91,7 +90,6 @@ def up
9190

9291
@connection.expects(:sleep).times(4)
9392

94-
9593
record_calls(@migration, :write) do
9694
run_migration
9795
flunk 'run_migration should raise'

0 commit comments

Comments
 (0)