Skip to content

Commit

Permalink
Quietly fail init if no database is present
Browse files Browse the repository at this point in the history
Failing loudly under these circs context isn't helpful because it
doesn't reflect a problem with the analytics setup; apps in CI often run
the asset precompilation step without a database present.
  • Loading branch information
duncanjbrown committed Sep 26, 2022
1 parent a64cf26 commit 03e2d00
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 03e2d00

Please sign in to comment.