diff --git a/spec/safe_statements_spec.rb b/spec/safe_statements_spec.rb index d93a694..ecfecc8 100644 --- a/spec/safe_statements_spec.rb +++ b/spec/safe_statements_spec.rb @@ -4167,7 +4167,7 @@ def up ) end - it "releases the lock (even after an exception)" do + it "releases the lock even after an exception" do begin migration.safely_acquire_lock_for_table(table_name) do raise "bogus error" @@ -4178,6 +4178,32 @@ def up expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty end + it "releases the lock even after a swallowed postgres exception" do + migration.safely_acquire_lock_for_table(table_name) do + expect(locks_for_table(table_name, connection: alternate_connection)).to contain_exactly( + having_attributes( + table: "bogus_table", + lock_type: "AccessExclusiveLock", + granted: true, + pid: kind_of(Integer), + ), + ) + + begin + migration.connection.execute("SELECT * FROM garbage") + rescue + end + + expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty + + expect do + migration.connection.execute("SELECT * FROM bogus_table") + end.to raise_error(ActiveRecord::StatementInvalid, /PG::InFailedSqlTransaction/) + end + + expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty + end + it "waits to acquire a lock if the table is already blocked" do block_call_count = 0 expect(PgHaMigrations::BlockingDatabaseTransactions).to receive(:find_blocking_transactions).exactly(3).times do |*args|