Class: DbFuel::Library::ActiveRecord::Insert

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

Overview

This job can take the objects in a register and insert them into a database table.

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:, keys_register: nil, name: '', attributes: [], debug: false, primary_keyed_column: nil, register: Burner::DEFAULT_REGISTER, separator: '', timestamps: true) ⇒ Insert

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 insertion.

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: If primary_keyed_column is present then it will be used to
                  set the object's property to the returned primary key
                  from the INSERT statement.

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 both created_at
            and updated_at columns will automatically have their values set
            to the current UTC timestamp.


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

def initialize(
  table_name:,
  keys_register: nil,
  name: '',
  attributes: [],
  debug: false,
  primary_keyed_column: nil,
  register: Burner::DEFAULT_REGISTER,
  separator: '',
  timestamps: true
)
  attributes = Burner::Modeling::Attribute.array(attributes)

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

Instance Method Details

#perform(output, payload) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/db_fuel/library/active_record/insert.rb', line 72

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

  output.detail("Inserting #{payload[register].count} record(s)")

  payload[register].each { |row| insert_record(output, row, payload.time, keys) }
end