Method: Webhookdb::Replicator::Base#_upsert_webhook

Defined in:
lib/webhookdb/replicator/base.rb

#_upsert_webhook(request, upsert: true) ⇒ Object

Hook to be overridden, while still retaining top-level upsert_webhook functionality like error handling.

Parameters:

Raises:



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/webhookdb/replicator/base.rb', line 663

def _upsert_webhook(request, upsert: true)
  resource, event = self._resource_and_event(request)
  return nil if resource.nil?
  enrichment = self._fetch_enrichment(resource, event, request)
  prepared = self._prepare_for_insert(resource, event, request, enrichment)
  raise Webhookdb::InvalidPostcondition if prepared.key?(:data)
  inserting = {}
  data_col_val = self._resource_to_data(resource, event, request, enrichment)
  inserting[:data] = self._to_json(data_col_val)
  inserting[:enrichment] = self._to_json(enrichment) if self._store_enrichment_body?
  inserting.merge!(prepared)
  return inserting unless upsert
  remote_key_col = self._remote_key_column
  updating = self._upsert_update_expr(inserting, enrichment:)
  update_where = self._update_where_expr
  upserted_rows = self.admin_dataset(timeout: :fast) do |ds|
    ds.insert_conflict(
      target: remote_key_col.name,
      update: updating,
      update_where:,
    ).insert(inserting)
  end
  row_changed = upserted_rows.present?
  self._notify_dependents(inserting, row_changed)
  self._publish_rowupsert(inserting) if row_changed
  return inserting
end