Skip to content

Commit 8a32ee8

Browse files
Merge pull request #160 from doctolib/Final_fix_for_nil_start_date
Fix format_start_time implementation
2 parents 70a17d4 + 223c0df commit 8a32ee8

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/safe-pg-migrations/helpers/blocking_activity_formatter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def output_confidentially_blocking_queries(queries)
4141
queries.each do |start_time, locktype, mode, pid, transactionid|
4242
Logger.say <<~MESSAGE.squish, sub_item: true, sensitive: true
4343
Query with pid #{pid || 'null'}
44-
started #{start_time ? format_start_time(start_time) : 'unknown'}:
44+
started #{format_start_time(start_time)}:
4545
lock type: #{locktype || 'null'},
4646
lock mode: #{mode || 'null'},
4747
lock transactionid: #{transactionid || 'null'}",
@@ -50,6 +50,8 @@ def output_confidentially_blocking_queries(queries)
5050
end
5151

5252
def format_start_time(start_time, reference_time = Time.now)
53+
return '(unknown start time)' unless start_time
54+
5355
start_time = Time.parse(start_time) unless start_time.is_a? Time
5456

5557
duration = (reference_time - start_time).round

test/blocking_activity_logger_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ def test_blocking_activity_logger_filtered
1717
refute_includes calls, 'BEGIN; SELECT 1 FROM users'
1818
end
1919

20+
def test_doenot_fail_with_unknown_start_time
21+
@migration = simulate_blocking_transaction_from_another_connection
22+
23+
alternate_connection = SafePgMigrations.alternate_connection
24+
SafePgMigrations.stubs(:alternate_connection).returns(alternate_connection)
25+
alternate_connection.stubs(:query).returns([[1234, 'SELECT something FROM a_table', nil]])
26+
27+
calls = record_calls(@migration, :write) do
28+
run_migration
29+
end.join
30+
31+
assert_match(/Query with pid 1234 started \(unknown start time\)/, calls)
32+
end
33+
2034
def test_logger_unfiltered
2135
@migration = simulate_blocking_transaction_from_another_connection
2236
calls = record_calls(@migration, :write) { run_migration }.join

0 commit comments

Comments
 (0)