Skip to content

Commit 3574eee

Browse files
author
Baptiste Perbos
committed
feat: add a test
1 parent 1ea0225 commit 3574eee

File tree

2 files changed

+52
-33
lines changed

2 files changed

+52
-33
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ def log_blocking_queries
7979
if queries.empty?
8080
SafePgMigrations.say 'Could not find any blocking query.', true
8181
else
82-
SafePgMigrations.say(
83-
"Statement was being blocked by the following #{'query'.pluralize(queries.size)}:", true
84-
)
85-
SafePgMigrations.say '', true
82+
8683
output_blocking_queries(queries)
8784
SafePgMigrations.say(
8885
'Beware, some of those queries might run in a transaction. In this case the locking query might be '\
@@ -97,16 +94,20 @@ def log_blocking_queries
9794

9895
def output_blocking_queries(queries)
9996
if SafePgMigrations.config.blocking_activity_logger_verbose
97+
SafePgMigrations.say(
98+
"Statement was being blocked by the following #{'query'.pluralize(queries.size)}:", true
99+
)
100+
SafePgMigrations.say '', true
100101
queries.each do |query, start_time|
101102
SafePgMigrations.say "#{format_start_time start_time}: #{query}", true
102103
end
103104
else
104105
queries.each do |start_time, locktype, mode, pid, transactionid|
105106
SafePgMigrations.say format_start_time(start_time), true
106-
SafePgMigrations.say "lock type: #{locktype || 'null'}"
107-
SafePgMigrations.say "lock mode: #{mode || 'null'}"
108-
SafePgMigrations.say "lock pid: #{pid || 'null'}"
109-
SafePgMigrations.say "lock transactionid: #{transactionid || 'null'}"
107+
SafePgMigrations.say "lock type: #{locktype || 'null'}", true
108+
SafePgMigrations.say "lock mode: #{mode || 'null'}", true
109+
SafePgMigrations.say "lock pid: #{pid || 'null'}", true
110+
SafePgMigrations.say "lock transactionid: #{transactionid || 'null'}", true
110111
end
111112
end
112113
end

test/safe_pg_migrations_test.rb

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,22 @@ def change
4949
refute @connection.table_exists?(:users)
5050
end
5151

52-
def test_statement_retry
53-
@connection.create_table(:users)
54-
@migration =
55-
Class.new(ActiveRecord::Migration::Current) do
56-
def up
57-
# Simulate a blocking transaction from another connection.
58-
thread_lock = Concurrent::CountDownLatch.new
59-
thread =
60-
Thread.new do
61-
ActiveRecord::Base.connection.execute('BEGIN; SELECT 1 FROM users')
62-
thread_lock.count_down
63-
sleep 1
64-
ActiveRecord::Base.connection.commit_db_transaction
65-
end
66-
67-
thread_lock.wait # Wait for the above transaction to start.
68-
69-
add_column :users, :email, :string
52+
def test_blocking_activity_logger_filtered
53+
SafePgMigrations.config.blocking_activity_logger_verbose = false
7054

71-
thread.join
72-
end
73-
end.new
74-
75-
SafePgMigrations.config.retry_delay = 1.second
76-
SafePgMigrations.config.safe_timeout = 0.5.second
77-
SafePgMigrations.config.blocking_activity_logger_margin = 0.1.seconds
55+
@connection.create_table(:users)
56+
@migration = simulate_blocking_transaction_from_another_connection
57+
58+
calls = record_calls(@migration, :write) { run_migration }.join
59+
assert_includes calls, 'lock type: relation'
60+
assert_includes calls, 'lock mode: AccessExclusiveLock'
61+
assert_includes calls, 'lock pid:'
62+
assert_includes calls, 'lock transactionid: null'
63+
refute_includes calls, 'Statement was being blocked by the following query'
64+
end
7865

66+
def test_statement_retry
67+
@migration = simulate_blocking_transaction_from_another_connection
7968
calls = record_calls(@migration, :write) { run_migration }.map(&:first)
8069
assert @connection.column_exists?(:users, :email, :string)
8170
assert_equal [
@@ -597,4 +586,33 @@ def up
597586
assert_match("SET lock_timeout TO '70s'", logs[4])
598587
end
599588
end
589+
590+
private
591+
592+
def simulate_blocking_transaction_from_another_connection
593+
SafePgMigrations.config.retry_delay = 1.second
594+
SafePgMigrations.config.safe_timeout = 0.5.second
595+
SafePgMigrations.config.blocking_activity_logger_margin = 0.1.seconds
596+
597+
@connection.create_table(:users)
598+
599+
Class.new(ActiveRecord::Migration::Current) do
600+
def up
601+
thread_lock = Concurrent::CountDownLatch.new
602+
thread =
603+
Thread.new do
604+
ActiveRecord::Base.connection.execute('BEGIN; SELECT 1 FROM users')
605+
thread_lock.count_down
606+
sleep 1
607+
ActiveRecord::Base.connection.commit_db_transaction
608+
end
609+
610+
thread_lock.wait # Wait for the above transaction to start.
611+
612+
add_column :users, :email, :string
613+
614+
thread.join
615+
end
616+
end.new
617+
end
600618
end

0 commit comments

Comments
 (0)