Module: InventoryRefresh::SaveCollection::Saver::SqlHelperUpsert

Includes:
Logging
Defined in:
lib/inventory_refresh/save_collection/saver/sql_helper_upsert.rb

Instance Method Summary collapse

Methods included from Logging

#logger

Instance Method Details

#build_insert_query(all_attribute_keys, hashes, on_conflict: nil, mode:, column_name: nil) ⇒ Object

Parameters:

  • all_attribute_keys (Array<Symbol>)

    Array of all columns we will be saving into each table row

  • hashes (Array<Hash>)

    data used for building a batch insert sql query

  • mode (Symbol)

    Mode for saving, allowed values are [:full, :partial], :full is when we save all columns of a row, :partial is when we save only few columns, so a partial row.

  • on_conflict (Symbol, NilClass) (defaults to: nil)

    defines behavior on conflict with unique index constraint, allowed values are :do_update, :do_nothing, nil



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/inventory_refresh/save_collection/saver/sql_helper_upsert.rb', line 22

def build_insert_query(all_attribute_keys, hashes, on_conflict: nil, mode:, column_name: nil)
  logger.debug("Building insert query for #{inventory_collection} of size #{inventory_collection.size}...")

  # Cache the connection for the batch
  connection = get_connection
  # Ignore versioning columns that are set separately
  ignore_cols = mode == :partial ? [:resource_timestamp, :resource_counter] : []
  # Make sure we don't send a primary_key for INSERT in any form, it could break PG sequencer
  all_attribute_keys_array = all_attribute_keys.to_a - [primary_key.to_s, primary_key.to_sym] - ignore_cols

  insert_query = insert_query_insert_values(hashes, all_attribute_keys_array, connection)
  insert_query += insert_query_on_conflict_behavior(all_attribute_keys, on_conflict, mode, ignore_cols, column_name)
  insert_query += insert_query_returning

  logger.debug("Building insert query for #{inventory_collection} of size #{inventory_collection.size}...Complete")

  insert_query
end

#build_insert_set_cols(key) ⇒ String

Builds ON CONFLICT UPDATE updating branch for one column identified by the passed key

Parameters:

  • key (Symbol)

    key that is column name

Returns:

  • (String)

    SQL clause for upserting one column



12
13
14
# File 'lib/inventory_refresh/save_collection/saver/sql_helper_upsert.rb', line 12

def build_insert_set_cols(key)
  "#{quote_column_name(key)} = EXCLUDED.#{quote_column_name(key)}"
end