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

🍀 Four kinds of init error #54

Merged
merged 1 commit into from
Sep 29, 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
19 changes: 10 additions & 9 deletions lib/dfe/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,7 @@ 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

ActiveRecord::Base.connection # cause an exception early if we can't connect
DfE::Analytics::Fields.check!

entities_for_analytics.each do |entity|
Expand All @@ -96,6 +90,10 @@ def self.initialize!
model.include(DfE::Analytics::Entities)
end
end
rescue ActiveRecord::PendingMigrationError
Rails.logger.info('Database requires migration; DfE Analytics not initialized')
rescue ActiveRecord::ActiveRecordError
Rails.logger.info('No database connection; DfE Analytics not initialized')
end

def self.enabled?
Expand Down Expand Up @@ -177,9 +175,12 @@ def self.entity_model_mapping

Rails.application.eager_load!

rails_tables = %w[ar_internal_metadata schema_migrations]

ActiveRecord::Base.descendants
.reject(&:abstract_class?)
.index_by(&:table_name)
.reject(&:abstract_class?)
.index_by(&:table_name)
.except(*rails_tables)
end
end

Expand Down
9 changes: 9 additions & 0 deletions spec/dfe/analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
end
end

describe 'when migrations are pending' do
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 { 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