class ROM::SQL::Postgres::Commands::Upsert
Upsert
command
The command being called attempts to insert a record and if the inserted row would violate a unique constraint updates the conflicting row (or silently does nothing). A very important implementation detail is that the whole operation is serializable, i.e. aware of concurrent transactions, and doesn't raise exceptions and doesn't issue missing updates once used properly.
See PG's docs in INSERT statement for details www.postgresql.org/docs/current/static/sql-insert.html
Normally, the command should be configured via class level settings. By default, that is without any setting provided, the command uses the ON CONFLICT DO NOTHING clause.
This implementation uses Sequel's API underneath, the docs are available at sequel.jeremyevans.net/rdoc-adapters/classes/Sequel/Postgres/DatasetMethods.html#method-i-insert_conflict
@api public
Public Instance Methods
Tries to insert provided tuples and do an update (or nothing) when the inserted record violates a unique constraint and hence cannot be appended to the table
@return [Array<Hash>]
@api public
# File lib/rom/sql/extensions/postgres/commands.rb, line 117 def execute(tuples) inserted_tuples = with_input_tuples(tuples) do |tuple| upsert(input[tuple], upsert_options) end inserted_tuples.flatten(1) end
@api private
# File lib/rom/sql/extensions/postgres/commands.rb, line 126 def upsert_options @upsert_options ||= { constraint: constraint, target: conflict_target, conflict_where: conflict_where, update_where: update_where, update: update_statement } end