Class: ROM::SQL::Postgres::Commands::Upsert
- Inherits:
-
Commands::Create
- Object
- Commands::Create
- Commands::Create
- ROM::SQL::Postgres::Commands::Upsert
- Defined in:
- lib/rom/sql/extensions/postgres/commands.rb
Overview
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 https://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 http://sequel.jeremyevans.net/rdoc-adapters/classes/Sequel/Postgres/DatasetMethods.html#method-i-insert_conflict
Instance Attribute Summary collapse
-
#conflict_target ⇒ Object
readonly
The column or expression to handle a violation on.
-
#conflict_where ⇒ Object
readonly
The index filter, when using a partial index to determine uniqueness.
-
#constraint ⇒ Symbol
readonly
The name of the constraint expected to be violated.
-
#update_statement ⇒ Object
readonly
The update statement which will be executed in case of a violation.
-
#update_where ⇒ Object
readonly
The WHERE clause to be added to the update.
Instance Method Summary collapse
-
#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.
- #upsert_options ⇒ Object private
Instance Attribute Details
#conflict_target ⇒ Object (readonly)
Returns the column or expression to handle a violation on.
100 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 100 option :conflict_target, default: -> { config.conflict_target } |
#conflict_where ⇒ Object (readonly)
Returns the index filter, when using a partial index to determine uniqueness.
104 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 104 option :conflict_where, default: -> { config.conflict_where } |
#constraint ⇒ Symbol (readonly)
Returns the name of the constraint expected to be violated.
96 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 96 option :constraint, default: -> { config.constraint } |
#update_statement ⇒ Object (readonly)
Returns the update statement which will be executed in case of a violation.
108 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 108 option :update_statement, default: -> { config.update_statement } |
#update_where ⇒ Object (readonly)
Returns the WHERE clause to be added to the update.
112 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 112 option :update_where, default: -> { config.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
121 122 123 124 125 126 127 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 121 def execute(tuples) inserted_tuples = with_input_tuples(tuples) do |tuple| upsert(input[tuple], ) end inserted_tuples.flatten(1) end |
#upsert_options ⇒ Object
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.
130 131 132 133 134 135 136 137 138 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 130 def @upsert_options ||= { constraint: constraint, target: conflict_target, conflict_where: conflict_where, update_where: update_where, update: update_statement } end |