class DbFuel::Library::ActiveRecord::Update

This job can take the unique 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 to find the records to update. The primary_keyed_column is used to update the unique record. Only one record will be updated per statement.

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

Public Class Methods

new( table_name:, name: '', attributes: [], debug: false, primary_keyed_column: nil, keys_register: nil, register: Burner::DEFAULT_REGISTER, separator: '', timestamps: true, unique_attributes: [] ) click to toggle source

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.

primary_keyed_column [required]: Primary key column for the corresponding table.
                        Used as the WHERE clause for the UPDATE statement.
                        Only one record will be updated at a time
                        using the primary key specified.

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 find specific
                   records. The UPDATE statement's WHERE
                   clause will use the primary key specified.
Calls superclass method
# File lib/db_fuel/library/active_record/update.rb, line 54
def initialize(
  table_name:,
  name: '',
  attributes: [],
  debug: false,
  primary_keyed_column: nil,
  keys_register: nil,
  register: Burner::DEFAULT_REGISTER,
  separator: '',
  timestamps: true,
  unique_attributes: []
)

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

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

  freeze
end

Public Instance Methods

perform(output, payload) click to toggle source
# File lib/db_fuel/library/active_record/update.rb, line 85
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|
    first_record  = update_record(output, row, payload.time, keys)
    rows_affected = first_record ? 1 : 0

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

    memo + rows_affected
  end

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