@@ -49,33 +49,22 @@ def change
49
49
refute @connection . table_exists? ( :users )
50
50
end
51
51
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
70
54
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
78
65
66
+ def test_statement_retry
67
+ @migration = simulate_blocking_transaction_from_another_connection
79
68
calls = record_calls ( @migration , :write ) { run_migration } . map ( &:first )
80
69
assert @connection . column_exists? ( :users , :email , :string )
81
70
assert_equal [
@@ -597,4 +586,33 @@ def up
597
586
assert_match ( "SET lock_timeout TO '70s'" , logs [ 4 ] )
598
587
end
599
588
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
600
618
end
0 commit comments