Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover when the database hasn’t been set up yet #58

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/dfe/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ def self.configure
end

def self.initialize!
ActiveRecord::Base.connection # cause an exception early if we can't connect
unless defined?(ActiveRecord)
# bail if we don't have AR at all
Rails.logger.info('ActiveRecord not loaded; DfE Analytics not initialized')
return
end

raise ActiveRecord::PendingMigrationError if ActiveRecord::Base.connection.migration_context.needs_migration?

DfE::Analytics::Fields.check!

entities_for_analytics.each do |entity|
Expand Down
13 changes: 11 additions & 2 deletions spec/dfe/analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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(Rails.logger).to receive(:info).with(/No database connection/)
expect { DfE::Analytics.initialize! }.not_to raise_error
end
end
Expand All @@ -26,7 +26,16 @@
it 'recovers and logs' do
allow(DfE::Analytics::Fields).to receive(:check!).and_raise(ActiveRecord::PendingMigrationError)

allow(Rails.logger).to receive(:info).with(/Database requires migration/)
expect(Rails.logger).to receive(:info).with(/Database requires migration/)
expect { DfE::Analytics.initialize! }.not_to raise_error
end
end

describe 'when ActiveRecord is not loaded' do
it 'recovers and logs' do
hide_const('ActiveRecord')

expect(Rails.logger).to receive(:info).with(/ActiveRecord not loaded/)
expect { DfE::Analytics.initialize! }.not_to raise_error
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ def name; end
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!

config.around do |example|
ActiveRecord::Base.connection.migration_context.migrate
example.run
end

config.define_derived_metadata do |metadata|
metadata[:skip_analytics_init] = true unless metadata[:skip_analytics_init] == false
end
Expand Down