Class: DbFuel::Library::ActiveRecord::UpdateAll

Inherits:
Upsert
  • Object
show all
Defined in:
lib/db_fuel/library/active_record/update_all.rb

Overview

This job can take the objects in a register and updates them within database table. The attributes translate to SQL SET clauses and the unique_keys translate to WHERE clauses. One or more records may be updated at a time.

Expected Payload input: array of objects Payload output: array of objects.

Constant Summary

Constants inherited from Base

Base::CREATED_AT, Base::NOW_TYPE, Base::UPDATED_AT

Instance Attribute Summary

Attributes inherited from Upsert

#primary_keyed_column, #timestamps, #unique_record_transformer

Attributes inherited from Base

#db_provider, #debug, #keys_register, #record_transformer, #resolver

Instance Method Summary collapse

Constructor Details

#initialize(table_name:, name: '', attributes: [], debug: false, register: Burner::DEFAULT_REGISTER, keys_register: nil, separator: '', timestamps: true, unique_attributes: []) ⇒ UpdateAll

Arguments:

name: name of the job within the Burner::Pipeline.

table_name [required]: name of the table to use for the INSERT statements.

attributes:  Used to specify which object properties to put into the
             SQL statement and also allows for one last custom transformation
             pipeline, in case the data calls for SQL-specific transformers
             before mutation.

debug: If debug is set to true (defaults to false) then the SQL statements and
       returned objects will be printed in the output.  Only use this option while
       debugging issues as it will fill up the output with (potentially too much) data.

separator: Just like other jobs with a 'separator' option, if the objects require
           key-path notation or nested object support, you can set the separator
           to something non-blank (like a period for notation in the
           form of: name.first).

timestamps: If timestamps is true (default behavior) then the updated_at column will
            automatically have its value set to the current UTC timestamp.

unique_attributes: Each key will become a WHERE clause in order to only update specific
                   records.


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/db_fuel/library/active_record/update_all.rb', line 47

def initialize(
  table_name:,
  name: '',
  attributes: [],
  debug: false,
  register: Burner::DEFAULT_REGISTER,
  keys_register: nil,
  separator: '',
  timestamps: true,
  unique_attributes: []
)

  attributes = Burner::Modeling::Attribute.array(attributes)

  super(
    name: name,
    table_name: table_name,
    attributes: attributes,
    debug: debug,
    primary_keyed_column: nil,
    keys_register: keys_register,
    register: register,
    separator: separator,
    timestamps: timestamps,
    unique_attributes: unique_attributes
  )

  freeze
end

Instance Method Details

#perform(output, payload) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/db_fuel/library/active_record/update_all.rb', line 77

def perform(output, payload)
  payload[register] = array(payload[register])
  keys              = resolve_key_set(output, payload)

  total_rows_affected = payload[register].inject(0) do |memo, row|
    where_object  = unique_record_transformer.transform(row, payload.time)
    rows_affected = update(output, row, payload.time, where_object, keys)

    debug_detail(output, "Individual Rows Affected: #{rows_affected}")

    memo + rows_affected
  end

  output.detail("Total Rows Affected: #{total_rows_affected}")
end