Skip to content

Commit

Permalink
ensure add_schema_to_search_path returns the block value (#33)
Browse files Browse the repository at this point in the history
whether or not the requested schema is already in the path
  • Loading branch information
jstanley0 authored Aug 2, 2024
1 parent 9436ecc commit 7354283
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/active_record/pg_extensions/postgresql_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ def add_schema_to_search_path(schema)
else
old_search_path = schema_search_path
manual_rollback = false
result = nil
transaction(requires_new: true) do
self.schema_search_path += ",#{schema}"
yield
result = yield
self.schema_search_path = old_search_path
rescue ActiveRecord::StatementInvalid, ActiveRecord::Rollback => e
# the transaction rolling back will revert the search path change;
Expand All @@ -126,6 +127,8 @@ def add_schema_to_search_path(schema)
# the transaction call will swallow ActiveRecord::Rollback,
# but we want it this method to be transparent
raise manual_rollback if manual_rollback

result
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/postgresql_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
end.to raise_error(ActiveRecord::StatementInvalid)
expect(connection.schema_search_path).to eq "public"
end

it "returns the value from the block whether or not the schema is added" do
expect(connection.add_schema_to_search_path("public") { 1 }).to eq 1
expect(connection.add_schema_to_search_path("postgis") { 2 }).to eq 2
end
end

describe "#vacuum" do
Expand Down

0 comments on commit 7354283

Please sign in to comment.