Skip to content

Commit adab2ba

Browse files
Fix broken tests
1 parent fe242fa commit adab2ba

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

lib/safe-pg-migrations/configuration.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def initialize
3333
def lock_timeout=(value)
3434
raise 'Setting lock timeout to 0 disables the lock timeout and is dangerous' if value == 0.seconds
3535

36-
unless value.nil? || (value < safe_timeout && value < max_lock_timeout_for_retry)
36+
unless value.nil? || (value < safe_timeout && value <= max_lock_timeout_for_retry)
3737
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})"
3838
end
3939

@@ -43,15 +43,15 @@ def lock_timeout=(value)
4343
def safe_timeout=(value)
4444
raise 'Setting safe timeout to 0 or nil disables the safe timeout and is dangerous' unless value && value > 0.seconds
4545

46-
unless lock_timeout.nil? || (value > lock_timeout && value > max_lock_timeout_for_retry)
46+
unless lock_timeout.nil? || (value > lock_timeout && value >= max_lock_timeout_for_retry)
4747
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})"
4848
end
4949

5050
@safe_timeout = value
5151
end
5252

53-
def max_lock_timeout_for_retry(value)
54-
unless lock_timeout.nil? || (value > lock_timeout && value < safe_timeout)
53+
def max_lock_timeout_for_retry=(value)
54+
unless lock_timeout.nil? || (value >= lock_timeout && value <= safe_timeout)
5555
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})"
5656
end
5757

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ module StatementRetrier
1313
private
1414

1515
def retry_if_lock_timeout
16-
lock_timeout_step = get_lock_timeout_step(SafePgMigrations.config)
16+
initial_lock_timeout = SafePgMigrations.config.lock_timeout
1717
number_of_retries = 0
1818
begin
1919
number_of_retries += 1
2020
yield
2121
rescue ActiveRecord::LockWaitTimeout
22-
raise if transaction_open? # Retrying is useless if we're inside a transaction.
23-
raise if number_of_retries >= max_tries
22+
# Retrying is useless if we're inside a transaction.
23+
if transaction_open? || number_of_retries >= SafePgMigrations.config.max_tries
24+
SafePgMigrations.config.lock_timeout = initial_lock_timeout
25+
raise
26+
end
2427

25-
increase_lock_timeout(lock_timeout_step) unless SafePgMigrations.config.lock_timeout.nil?
28+
increase_lock_timeout unless SafePgMigrations.config.lock_timeout.nil?
2629

2730
retry_delay = SafePgMigrations.config.retry_delay
2831
Helpers::Logger.say "Retrying in #{retry_delay} seconds...", sub_item: true
@@ -32,15 +35,15 @@ def retry_if_lock_timeout
3235
end
3336
end
3437

35-
def increase_lock_timeout(lock_timeout_step)
38+
def increase_lock_timeout
3639
SafePgMigrations.config.lock_timeout += lock_timeout_step
3740
unless SafePgMigrations.config.lock_timeout < SafePgMigrations.config.max_lock_timeout_for_retry
3841
SafePgMigrations.config.lock_timeout = SafePgMigrations.config.max_lock_timeout_for_retry
3942
end
4043
end
4144

42-
def get_lock_timeout_step(config)
43-
(config.max_lock_timeout_for_retry - config.lock_timeout) / config.max_tries
45+
def lock_timeout_step
46+
(SafePgMigrations.config.max_lock_timeout_for_retry - SafePgMigrations.config.lock_timeout) / SafePgMigrations.config.max_tries
4447
end
4548
end
4649
end

test/statement_retrier_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ def test_statement_retry
4848
' -> Retrying now.',
4949
], calls[7..9]
5050
end
51+
52+
def lock_timout_increase_on_retry
53+
54+
end
5155
end

0 commit comments

Comments
 (0)