Class: DbFuel::Library::ActiveRecord::Update
Overview
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.
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
-
#initialize(table_name:, name: '', attributes: [], debug: false, primary_keyed_column: nil, keys_register: nil, register: Burner::DEFAULT_REGISTER, separator: '', timestamps: true, unique_attributes: []) ⇒ Update
constructor
Arguments: name: name of the job within the Burner::Pipeline.
- #perform(output, payload) ⇒ Object
Constructor Details
#initialize(table_name:, name: '', attributes: [], debug: false, primary_keyed_column: nil, keys_register: nil, register: Burner::DEFAULT_REGISTER, separator: '', timestamps: true, unique_attributes: []) ⇒ Update
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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# 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: , unique_attributes: unique_attributes ) freeze end |
Instance Method Details
#perform(output, payload) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# 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 |