diff --git a/lib/active_record/pg_extensions/postgresql_adapter.rb b/lib/active_record/pg_extensions/postgresql_adapter.rb index 17651ae..c0e4548 100644 --- a/lib/active_record/pg_extensions/postgresql_adapter.rb +++ b/lib/active_record/pg_extensions/postgresql_adapter.rb @@ -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; @@ -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 diff --git a/spec/postgresql_adapter_spec.rb b/spec/postgresql_adapter_spec.rb index f1952ba..0279e17 100644 --- a/spec/postgresql_adapter_spec.rb +++ b/spec/postgresql_adapter_spec.rb @@ -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