Skip to content

Commit 2a625c4

Browse files
authored
Merge pull request #195 from hrmtl/make-activerecord-optional
Make the gem work without ActiveRecord
2 parents a1a6d20 + 75b719c commit 2a625c4

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

lib/dfe/analytics.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
require 'i18n'
55
require 'httparty'
66
require 'google/cloud/bigquery'
7+
require 'dfe/analytics/activerecord' if defined?(ActiveRecord)
78
require 'dfe/analytics/event_schema'
89
require 'dfe/analytics/fields'
910
require 'dfe/analytics/entities'
10-
require 'dfe/analytics/services/entity_table_checks'
11-
require 'dfe/analytics/services/checksum_calculator'
12-
require 'dfe/analytics/services/generic_checksum_calculator'
13-
require 'dfe/analytics/services/postgres_checksum_calculator'
1411
require 'dfe/analytics/shared/service_pattern'
1512
require 'dfe/analytics/concerns/requestable'
1613
require 'dfe/analytics/event'
@@ -29,7 +26,6 @@
2926
require 'dfe/analytics/big_query_api'
3027
require 'dfe/analytics/big_query_legacy_api'
3128
require 'dfe/analytics/azure_federated_auth'
32-
require 'dfe/analytics/transaction_changes'
3329
require 'dfe/analytics/api_requests'
3430

3531
module DfE
@@ -98,19 +94,21 @@ def self.configure
9894
end
9995

10096
def self.initialize!
101-
unless defined?(ActiveRecord)
102-
# bail if we don't have AR at all
103-
Rails.logger.error('ActiveRecord not loaded; DfE Analytics not initialized')
104-
return
105-
end
106-
10797
unless Rails.env.production? || File.exist?(Rails.root.join('config/initializers/dfe_analytics.rb'))
10898
message = "Warning: DfE Analytics is not set up. Run: 'bundle exec rails generate dfe:analytics:install'"
10999
Rails.logger.error(message)
110100
puts message
111101
return
112102
end
113103

104+
if defined?(ActiveRecord)
105+
setup_entities
106+
else
107+
Rails.logger.info('ActiveRecord not loaded; DfE Analytics will only track non-database requests.')
108+
end
109+
end
110+
111+
def self.setup_entities
114112
if Rails.version.to_f > 7.1
115113
ActiveRecord::Base.with_connection do |connection|
116114
raise ActiveRecord::PendingMigrationError if connection.pool.migration_context.needs_migration?

lib/dfe/analytics/activerecord.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
require 'dfe/analytics/services/entity_table_checks'
4+
require 'dfe/analytics/services/checksum_calculator'
5+
require 'dfe/analytics/services/generic_checksum_calculator'
6+
require 'dfe/analytics/services/postgres_checksum_calculator'
7+
require 'dfe/analytics/transaction_changes'

spec/dfe/analytics_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
it 'recovers and logs' do
5656
hide_const('ActiveRecord')
5757

58-
expect(Rails.logger).to receive(:error).with(/ActiveRecord not loaded/)
58+
expect(Rails.logger).to receive(:info).with(/ActiveRecord not loaded; DfE Analytics will only track non-database requests./)
5959
expect { DfE::Analytics.initialize! }.not_to raise_error
6060
end
6161
end
@@ -330,4 +330,14 @@
330330
expect(DfE::Analytics.all_entities_in_application.first.to_s).to match(/with_model_candidates/)
331331
end
332332
end
333+
334+
describe 'when ActiveRecord is not available' do
335+
before do
336+
hide_const('ActiveRecord')
337+
end
338+
339+
it 'initializes without errors' do
340+
expect { DfE::Analytics.initialize! }.not_to raise_error
341+
end
342+
end
333343
end

0 commit comments

Comments
 (0)