Class: ROM::SQL::Commands::Postgres::Upsert
- Defined in:
- lib/rom/sql/extensions/postgres/commands.rb
Overview
Upsert command
Uses a feature of PostgreSQL 9.5 commonly called an “upsert”. The command been called attempts to perform an insert and can make an update (or silently do nothing) in case of the insertion was unsuccessful due to a violation of a unique constraint. Very important implementation detail is that the whole operation is atomic, i.e. aware of concurrent transactions, and doesn’t raise exceptions if used properly.
See PG’s docs in INSERT statement for details www.postgresql.org/docs/current/static/sql-insert.html
Normally, the command should configured via class level settings. By default, that is without any settings provided, the command uses 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
-
#conflict_target ⇒ Object
readonly
The column or expression to handle a violation on.
-
#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.
76 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 76 option :conflict_target, reader: true, default: -> c { c.class.conflict_target } |
#constraint ⇒ Symbol (readonly)
Returns the name of the constraint expected to be violated.
72 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 72 option :constraint, reader: true, default: -> c { c.class.constraint } |
#update_statement ⇒ Object (readonly)
Returns the update statement which will be executed in case of a violation.
80 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 80 option :update_statement, reader: true, default: -> c { c.class.update_statement } |
#update_where ⇒ Object (readonly)
Returns the WHERE clause to be added to the update.
84 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 84 option :update_where, reader: true, default: -> c { c.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
93 94 95 96 97 98 99 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 93 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.
102 103 104 105 106 107 108 109 |
# File 'lib/rom/sql/extensions/postgres/commands.rb', line 102 def @upsert_options ||= { constraint: constraint, target: conflict_target, update_where: update_where, update: update_statement } end |