Sha256: 4dfeace9af4cf3e7129d4576143a927b3afc7f4cd68c37598afa71ab0cb2628b

Contents?: true

Size: 885 Bytes

Versions: 2

Compression:

Stored size: 885 Bytes

Contents

# frozen_string_literal: true

module Ductr
  module Sequel
    #
    # A destination control that accumulates rows in a buffer to upsert them by batch.
    #
    class BufferedUpsertDestination < Ductr::ETL::BufferedDestination
      #
      # Open the database if needed and call the job's method to run the query.
      #
      # @return [void]
      #
      def on_flush
        call_method(adapter.db, excluded, buffer)
      end

      private

      #
      # Generate the excluded keys hash e.g.
      #
      # ```ruby
      # {a: Sequel[:excluded][:a]}
      # ```
      #
      # @return [Hash<Symbol, Sequel::SQL::QualifiedIdentifier>] The excluded keys hash
      #
      def excluded
        keys = buffer.first.keys

        excluded_keys = keys.map do |key|
          Sequel[:excluded][key]
        end

        keys.zip(excluded_keys).to_h
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ductr-0.2.1 lib/ductr/sequel/buffered_upsert_destination.rb
ductr-0.2.0 lib/ductr/sequel/buffered_upsert_destination.rb