Skip to content

Commit

Permalink
Merge pull request #30 from bdurand/suppress-connection-attempt
Browse files Browse the repository at this point in the history
Ensure there are connections to the database before attempting to checkout a connection
  • Loading branch information
bdurand authored Dec 3, 2024
2 parents f8f09bc + 5f8e983 commit 191385b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 2.1.2

### Fixed

- Fixed ActiveRecord code ensure there are connections to the database before attempting to checkout a connection to avoid errors when the database is not available.

## 2.1.1

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.1
2.1.2
14 changes: 11 additions & 3 deletions lib/super_settings/storage/active_record_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,23 @@ def create_history(key:, changed_by:, created_at:, value: nil, deleted: false)
end

def with_connection(&block)
Model.connection_pool.with_connection(&block)
if Model.available?
Model.connection_pool.with_connection(&block)
else
yield
end
end

def transaction(&block)
Model.transaction(&block)
if Model.available?
Model.transaction(&block)
else
yield
end
end

def destroy_all
ApplicationRecord.transaction do
Model.transaction do
Model.delete_all
HistoryModel.delete_all
end
Expand Down
41 changes: 41 additions & 0 deletions spec/super_settings/storage/active_record_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,46 @@
expect(setting.history(limit: 1, offset: 2).collect(&:value)).to eq ["1"]
end
end

context "when there is no database connection" do
before do
SuperSettings::Storage::ActiveRecordStorage.new(key: "setting_1", raw_value: "1").save!
allow(SuperSettings::Storage::ActiveRecordStorage::Model).to receive(:available?).and_return(false)
end

it "should not fetch a connection" do
value = nil
expect(SuperSettings::Storage::ActiveRecordStorage::Model.connection_pool).to_not receive(:with_connection)
SuperSettings::Storage::ActiveRecordStorage.with_connection { value = true }
expect(value).to be true
end

it "should not open a transaction" do
value = nil
expect(SuperSettings::Storage::ActiveRecordStorage::Model).to_not receive(:transaction)
SuperSettings::Storage::ActiveRecordStorage.transaction { value = true }
expect(value).to be true
end

it "should return an empty array for all" do
expect(SuperSettings::Storage::ActiveRecordStorage.all).to eq []
end

it "should return an empty array for active" do
expect(SuperSettings::Storage::ActiveRecordStorage.active).to eq []
end

it "should return an empty array for updated_since" do
expect(SuperSettings::Storage::ActiveRecordStorage.updated_since(Time.now)).to eq []
end

it "should return nil for find_by_key" do
expect(SuperSettings::Storage::ActiveRecordStorage.find_by_key("setting_1")).to eq nil
end

it "should return nil for last_updated_at" do
expect(SuperSettings::Storage::ActiveRecordStorage.last_updated_at).to eq nil
end
end
end
end

0 comments on commit 191385b

Please sign in to comment.