Skip to content

Commit

Permalink
Merge pull request #51 from DFE-Digital/recover-init-when-no-database
Browse files Browse the repository at this point in the history
Quietly fail init if no database is present
  • Loading branch information
duncanjbrown authored Sep 27, 2022
2 parents a64cf26 + 03e2d00 commit 7fbf9cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/dfe/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def self.configure
end

def self.initialize!
begin
ActiveRecord::Base.connection
rescue ActiveRecord::ActiveRecordError
Rails.logger.info('No database connection; DfE Analytics not initialized')
return
end

DfE::Analytics::Fields.check!

entities_for_analytics.each do |entity|
Expand Down
8 changes: 8 additions & 0 deletions spec/dfe/analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
end
end

describe 'when no database connection is available' do
it 'recovers and logs' do
allow(ActiveRecord::Base).to receive(:connection).and_raise(ActiveRecord::ConnectionNotEstablished)
allow(Rails.logger).to receive(:info).with(/No database connection/)
expect { DfE::Analytics.initialize! }.not_to raise_error
end
end

describe 'field checks on initialization' do
# field validity is computed from allowlist, blocklist and database. See
# Analytics::Fields for more details
Expand Down

0 comments on commit 7fbf9cf

Please sign in to comment.