Skip to content

Commit 2cfa43d

Browse files
committed
Test to ensure lock is released after failed query
1 parent 97605a0 commit 2cfa43d

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
@@ -4131,7 +4131,7 @@ def up
41314131
)
41324132
end
41334133

4134-
it "releases the lock (even after an exception)" do
4134+
it "releases the lock even after an exception" do
41354135
begin
41364136
migration.safely_acquire_lock_for_table(table_name) do
41374137
raise "bogus error"
@@ -4142,6 +4142,32 @@ def up
41424142
expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty
41434143
end
41444144

4145+
it "releases the lock even after a swallowed postgres exception" do
4146+
migration.safely_acquire_lock_for_table(table_name) do
4147+
expect(locks_for_table(table_name, connection: alternate_connection)).to contain_exactly(
4148+
having_attributes(
4149+
table: "bogus_table",
4150+
lock_type: "AccessExclusiveLock",
4151+
granted: true,
4152+
pid: kind_of(Integer),
4153+
),
4154+
)
4155+
4156+
begin
4157+
migration.connection.execute("SELECT * FROM garbage")
4158+
rescue
4159+
end
4160+
4161+
expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty
4162+
4163+
expect do
4164+
migration.connection.execute("SELECT * FROM bogus_table")
4165+
end.to raise_error(ActiveRecord::StatementInvalid, /PG::InFailedSqlTransaction/)
4166+
end
4167+
4168+
expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty
4169+
end
4170+
41454171
it "waits to acquire a lock if the table is already blocked" do
41464172
block_call_count = 0
41474173
expect(PgHaMigrations::BlockingDatabaseTransactions).to receive(:find_blocking_transactions).exactly(3).times do |*args|

0 commit comments

Comments
 (0)