Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure add_schema_to_search_path returns the block value #33

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading