Class: RailsErrorDashboard::Commands::BackfillEnvironments

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_error_dashboard/commands/backfill_environments.rb

Overview

Command: fill in environment on error logs captured before the column existed, using the rails_env recorded in environment_info at capture time.

Opt-in (rails_error_dashboard:backfill_environments). Live errors do not need it — FindOrIncrementError adopts a NULL row on its next occurrence — but history that never recurs would otherwise stay unbadged and unfilterable forever.

Runs in batches, one UPDATE per row: the value is inside a JSON text column, so it has to be parsed in Ruby and no portable single UPDATE can do it across PostgreSQL, MySQL and SQLite.

Constant Summary collapse

BATCH_SIZE =
500
COLUMN_LIMIT =
64

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(batch_size: BATCH_SIZE) ⇒ BackfillEnvironments

Returns a new instance of BackfillEnvironments.



24
25
26
# File 'lib/rails_error_dashboard/commands/backfill_environments.rb', line 24

def initialize(batch_size: BATCH_SIZE)
  @batch_size = batch_size
end

Class Method Details

.call(batch_size: BATCH_SIZE) ⇒ Object



20
21
22
# File 'lib/rails_error_dashboard/commands/backfill_environments.rb', line 20

def self.call(batch_size: BATCH_SIZE)
  new(batch_size: batch_size).call
end

Instance Method Details

#callInteger

Returns rows updated.

Returns:

  • (Integer)

    rows updated



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rails_error_dashboard/commands/backfill_environments.rb', line 29

def call
  return 0 unless ErrorLog.column_names.include?("environment")

  updated = 0
  ErrorLog.where(environment: nil).where.not(environment_info: nil)
          .in_batches(of: @batch_size) do |batch|
    batch.pluck(:id, :environment_info).each do |id, raw|
      env = rails_env_from(raw)
      next if env.nil?

      updated += ErrorLog.where(id: id, environment: nil).update_all(environment: env)
    end
  end
  updated
end