diff --git a/lib/ruby-pg-extras.rb b/lib/ruby-pg-extras.rb index f3a151b..17fa80a 100644 --- a/lib/ruby-pg-extras.rb +++ b/lib/ruby-pg-extras.rb @@ -247,6 +247,8 @@ def self.connection end def self.database_url=(value) + @_connection&.close + @_connection = nil @@database_url = value end diff --git a/spec/smoke_spec.rb b/spec/smoke_spec.rb index a0599ff..2f8853f 100644 --- a/spec/smoke_spec.rb +++ b/spec/smoke_spec.rb @@ -65,5 +65,17 @@ RubyPgExtras.bloat(in_format: :hash) end.not_to raise_error end + + it "resets the connection when setting database URL" do + old_connection = RubyPgExtras.connection + expect(old_connection).not_to be_finished + + RubyPgExtras.database_url = ENV.fetch("DATABASE_URL") + + expect(old_connection).to be_finished + new_connection = RubyPgExtras.connection + expect(new_connection).not_to eq(old_connection) + expect(new_connection).not_to be_finished + end end end