Class: ActiveRecord::Base::PromiscuousUpdateOperation

Inherits:
PromiscousOperation show all
Defined in:
lib/promiscuous/publisher/operation/active_record.rb

Instance Attribute Summary

Attributes inherited from Promiscuous::Publisher::Operation::NonPersistent

#instances

Attributes inherited from Promiscuous::Publisher::Operation::Base

#operation

Instance Method Summary collapse

Methods inherited from PromiscousOperation

#ensure_transaction!, #model, #operation_payloads, #transaction_context

Methods inherited from Promiscuous::Publisher::Operation::NonPersistent

#execute_instrumented, #operation_payloads, #query_dependencies

Methods inherited from Promiscuous::Publisher::Operation::Base

_acquire_lock, #acquire_op_lock, #dependencies_for, #dependency_for_op_lock, #ensure_op_still_locked, #execute_instrumented, #explain_operation, #generate_payload, #get_new_op_lock, #increment_dependencies, lock_options, #on_rabbitmq_confirm, #operation_payloads, #payload_for, #publish_payload_in_rabbitmq_async, #publish_payload_in_redis, #query_dependencies, rabbitmq_staging_set_key, #record_timestamp, #recover_db_operation, recover_locks, recover_operation, recover_operation_from_lock, recover_payloads_for_rabbitmq, #recovering?, #recovery_payload, register_recovery_mechanism, #release_op_lock, run_recovery_mechanisms, #should_instrument_query?, #trace_operation, #write_dependencies

Constructor Details

#initialize(arel, name, binds, options = {}) ⇒ PromiscuousUpdateOperation

Returns a new instance of PromiscuousUpdateOperation.



209
210
211
212
213
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 209

def initialize(arel, name, binds, options={})
  super
  @operation = :update
  raise unless @arel.is_a?(Arel::UpdateManager)
end

Instance Method Details

#any_published_field_changed?Boolean

Returns:

  • (Boolean)


229
230
231
232
233
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 229

def any_published_field_changed?
  updates = updated_fields_in_query
  return true if updates.nil? # Couldn't parse query
  (updated_fields_in_query.keys & model.published_db_fields).present?
end

#db_operation_and_selectObject



235
236
237
238
239
240
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 235

def db_operation_and_select
  # TODO this should be in the postgres driver (to also leverage the cache)
  @connection.exec_query("#{@connection.to_sql(@arel, @binds)} RETURNING *", @operation_name, @binds).tap do |result|
    @instances = result.map { |row| model.instantiate(row) }
  end.rows.size
end

#execute(&db_operation) ⇒ Object



242
243
244
245
246
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 242

def execute(&db_operation)
  return db_operation.call unless model
  return db_operation.call unless any_published_field_changed?
  super
end

#updated_fields_in_queryObject



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/promiscuous/publisher/operation/active_record.rb', line 215

def updated_fields_in_query
  Hash[@arel.ast.values.map do |v|
    case v
    when Arel::Nodes::Assignment
      [v.left.name.to_sym, v.right]
    when Arel::Nodes::SqlLiteral
      # Not parsing SQL, no thanks. It's an optimization anyway
      return nil
    else
      return nil
    end
  end]
end