Class: RecordVersionBase

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/record_version_base.rb

Overview

Abstract base class for versions database connection

Required because connects_to can only be called on ActiveRecord::Base
or abstract classes, not on concrete model classes like RecordVersion.

This enables Rails 7 multi-database features:
rails db:migrate:versions # Run versions migrations only
rails db:rollback:versions # Rollback versions only
rails db:migrate # Runs both primary AND versions

Direct Known Subclasses

RecordVersion

Constant Summary collapse

CONNECTION_ERRORS =

The heatwave_versions DB is a SEPARATE FDW-backed cluster holding the PaperTrail
audit trail. A brief outage (failover / restart / maintenance) must NOT 500 pages
that render audit-trail data as a supplementary widget. Wrap any versions-DB read
in RecordVersionBase.safe_read(fallback) { ... }: on a connection failure it logs
and returns fallback, so the caller (view/helper/model) degrades gracefully instead
of raising. Closed AppSignal incidents #4929 (RMA dates tracker) and #1745 (CRM audit
creator name) were exactly this — a versions blip 500ing the entire page.

Only connection failures are swallowed; real query errors (other StatementInvalid
subclasses) still propagate so genuine bugs aren't hidden.

[
  ActiveRecord::ConnectionNotEstablished, # pool / initial connect failure (incl. DatabaseConnectionError)
  ActiveRecord::ConnectionFailed,         # connection lost mid-statement (AppSignal #5993)
  ActiveRecord::NoDatabaseError,          # "database ... is not currently accepting connections" / not found
  PG::ConnectionBad,                      # raw libpq connection failure
].freeze

Class Method Summary collapse

Class Method Details

.safe_read(fallback = nil) ⇒ Object

Run a block that reads from the versions DB, degrading to fallback if that DB is
momentarily unavailable. See CONNECTION_ERRORS above.



39
40
41
42
43
44
# File 'app/models/record_version_base.rb', line 39

def self.safe_read(fallback = nil)
  yield
rescue *CONNECTION_ERRORS => e
  Rails.logger.warn("[versions-db unavailable] degrading gracefully (#{e.class}): #{e.message.to_s.lines.first&.strip}")
  fallback
end