Class: ROM::SQL::Postgres::Commands::Upsert

Inherits:
Commands::Create show all
Defined in:
lib/rom/sql/extensions/postgres/commands.rb

Overview

Upsert command

The command beign 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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands::ErrorWrapper

#call

Instance Attribute Details

#conflict_targetObject (readonly)

Returns the column or expression to handle a violation on.

Returns:

  • (Object)

    the column or expression to handle a violation on



77
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 77

option :conflict_target, default: -> { self.class.conflict_target }

#constraintSymbol (readonly)

Returns the name of the constraint expected to be violated.

Returns:

  • (Symbol)

    the name of the constraint expected to be violated



73
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 73

option :constraint, default: -> { self.class.constraint }

#update_statementObject (readonly)

Returns the update statement which will be executed in case of a violation.

Returns:

  • (Object)

    the update statement which will be executed in case of a violation



81
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 81

option :update_statement, default: -> { self.class.update_statement }

#update_whereObject (readonly)

Returns the WHERE clause to be added to the update.

Returns:

  • (Object)

    the WHERE clause to be added to the update



85
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 85

option :update_where, default: -> { self.class.update_where }

Instance Method Details

#execute(tuples) ⇒ Array<Hash>

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

Returns:

  • (Array<Hash>)


94
95
96
97
98
99
100
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 94

def execute(tuples)
  inserted_tuples = with_input_tuples(tuples) do |tuple|
    upsert(input[tuple], upsert_options)
  end

  inserted_tuples.flatten(1)
end

#upsert_optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
106
107
108
109
110
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 103

def upsert_options
  @upsert_options ||= {
    constraint: constraint,
    target: conflict_target,
    update_where: update_where,
    update: update_statement
  }
end