Module: ActiveRecordUpsert::Arel::TableExtensions

Defined in:
lib/active_record_upsert/arel/table_extensions.rb

Instance Method Summary collapse

Instance Method Details

#compile_upsert(upsert_keys, upsert_options, upsert_values, insert_values, wheres) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/active_record_upsert/arel/table_extensions.rb', line 4

def compile_upsert(upsert_keys, upsert_options, upsert_values, insert_values, wheres)
  # Support non-attribute key (like `md5(my_attribute)``)
  target = self[upsert_options.key?(:literal) ? ::Arel::Nodes::SqlLiteral.new(upsert_options[:literal]) : upsert_keys.join(',')]
  on_conflict_do_update = ::Arel::OnConflictDoUpdateManager.new

  on_conflict_do_update.target = target
  on_conflict_do_update.target_condition = upsert_options[:where]
  on_conflict_do_update.wheres = wheres
  on_conflict_do_update.set(upsert_values)

  insert_manager = ::Arel::InsertManager.new
  insert_manager.on_conflict = on_conflict_do_update.to_node
  insert_manager.into insert_values.first.first.relation
  insert_manager.insert(insert_values)
  insert_manager
end