Skip to content

Commit 3afcb87

Browse files
use pg_duration to avoid errors
1 parent 93c722f commit 3afcb87

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

lib/safe-pg-migrations/configuration.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# frozen_string_literal: true
22

33
require 'active_support/core_ext/numeric/time'
4+
require 'safe-pg-migrations/helpers/pg_helper'
45

56
module SafePgMigrations
67
class Configuration
8+
include Helpers::PgHelper
9+
710
attr_accessor(*%i[
811
backfill_batch_size
912
backfill_pause
@@ -77,12 +80,5 @@ def pg_lock_timeout
7780
# By reducing the lock timeout by a very small margin, we ensure that the lock timeout is raised in priority
7881
pg_duration safe_timeout * 0.99
7982
end
80-
81-
private
82-
83-
def pg_duration(duration)
84-
value, unit = duration.integer? ? [duration, 's'] : [(duration * 1000).to_i, 'ms']
85-
"#{value}#{unit}"
86-
end
8783
end
8884
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module SafePgMigrations
4+
module Helpers
5+
module PgHelper
6+
def pg_duration(duration)
7+
value, unit = duration.integer? ? [duration, 's'] : [(duration * 1000).to_i, 'ms']
8+
"#{value}#{unit}"
9+
end
10+
end
11+
end
12+
end

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module SafePgMigrations
44
module StatementRetrier
55
include Helpers::StatementsHelper
6+
include Helpers::PgHelper
67

78
RETRIABLE_SCHEMA_STATEMENTS.each do |method|
89
define_method method do |*args, **options, &block|
@@ -36,14 +37,14 @@ def retry_if_lock_timeout
3637
end
3738

3839
def increase_lock_timeout
39-
Helpers::Logger.say " Increasing the lock timeout... Currently set to #{@lock_timeout} seconds",
40+
Helpers::Logger.say " Increasing the lock timeout... Currently set to #{pg_duration(@lock_timeout)}",
4041
sub_item: true
4142
@lock_timeout += lock_timeout_step
4243
unless @lock_timeout < SafePgMigrations.config.max_lock_timeout_for_retry
4344
@lock_timeout = SafePgMigrations.config.max_lock_timeout_for_retry
4445
end
45-
execute("SET lock_timeout TO '#{@lock_timeout}s'")
46-
Helpers::Logger.say " Lock timeout is now set to #{@lock_timeout} seconds", sub_item: true
46+
execute("SET lock_timeout TO '#{pg_duration(@lock_timeout)}'")
47+
Helpers::Logger.say " Lock timeout is now set to #{pg_duration(@lock_timeout)}", sub_item: true
4748
end
4849

4950
def lock_timeout_step

test/statement_retrier_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ def test_lock_timeout_increase_on_retry
1212

1313
assert_equal [
1414
' -> Retrying in 60 seconds...',
15-
' -> Increasing the lock timeout... Currently set to 0.1 seconds',
16-
' -> Lock timeout is now set to 0.325 seconds',
15+
' -> Increasing the lock timeout... Currently set to 100ms',
16+
' -> Lock timeout is now set to 325ms',
1717
' -> Retrying now.',
1818
' -> Retrying in 60 seconds...',
19-
' -> Increasing the lock timeout... Currently set to 0.325 seconds',
20-
' -> Lock timeout is now set to 0.55 seconds',
19+
' -> Increasing the lock timeout... Currently set to 325ms',
20+
' -> Lock timeout is now set to 550ms',
2121
' -> Retrying now.',
2222
' -> Retrying in 60 seconds...',
23-
' -> Increasing the lock timeout... Currently set to 0.55 seconds',
24-
' -> Lock timeout is now set to 0.775 seconds',
23+
' -> Increasing the lock timeout... Currently set to 550ms',
24+
' -> Lock timeout is now set to 775ms',
2525
' -> Retrying now.',
2626
' -> Retrying in 60 seconds...',
27-
' -> Increasing the lock timeout... Currently set to 0.775 seconds',
28-
' -> Lock timeout is now set to 1 seconds',
27+
' -> Increasing the lock timeout... Currently set to 775ms',
28+
' -> Lock timeout is now set to 1s',
2929
' -> Retrying now.',
3030
], calls[1..].map(&:first)
3131
end

0 commit comments

Comments
 (0)