Skip to content

Commit 9541a0a

Browse files
committed
Test to ensure lock is released after failed query
1 parent 4af852d commit 9541a0a

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

spec/safe_statements_spec.rb

+27-1
Original file line numberDiff line numberDiff line change
@@ -3896,7 +3896,7 @@ def up
38963896
)
38973897
end
38983898

3899-
it "releases the lock (even after an exception)" do
3899+
it "releases the lock even after an exception" do
39003900
begin
39013901
migration.safely_acquire_lock_for_table(table_name) do
39023902
raise "bogus error"
@@ -3907,6 +3907,32 @@ def up
39073907
expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty
39083908
end
39093909

3910+
it "releases the lock even after a swallowed postgres exception" do
3911+
migration.safely_acquire_lock_for_table(table_name) do
3912+
expect(locks_for_table(table_name, connection: alternate_connection)).to contain_exactly(
3913+
having_attributes(
3914+
table: "bogus_table",
3915+
lock_type: "AccessExclusiveLock",
3916+
granted: true,
3917+
pid: kind_of(Integer),
3918+
),
3919+
)
3920+
3921+
begin
3922+
migration.connection.execute("SELECT * FROM garbage")
3923+
rescue
3924+
end
3925+
3926+
expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty
3927+
3928+
expect do
3929+
migration.connection.execute("SELECT * FROM bogus_table")
3930+
end.to raise_error(ActiveRecord::StatementInvalid, /PG::InFailedSqlTransaction/)
3931+
end
3932+
3933+
expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty
3934+
end
3935+
39103936
it "waits to acquire a lock if the table is already blocked" do
39113937
block_call_count = 0
39123938
expect(PgHaMigrations::BlockingDatabaseTransactions).to receive(:find_blocking_transactions).exactly(3).times do |*args|

0 commit comments

Comments
 (0)