class BeetleETL::Load

Constants

IMPORTER_COLUMNS

Public Class Methods

new(config, table_name, relations) click to toggle source
Calls superclass method
# File lib/beetle_etl/steps/load.rb, line 9
def initialize(config, table_name, relations)
  super(config, table_name)
  @relations = relations
end

Public Instance Methods

dependencies() click to toggle source
# File lib/beetle_etl/steps/load.rb, line 20
def dependencies
  @relations.values.map { |d| Load.step_name(d) }.to_set
end
load_create() click to toggle source
# File lib/beetle_etl/steps/load.rb, line 24
    def load_create
      just_now = now

      database.execute <<-SQL
        INSERT INTO "#{target_schema}"."#{table_name}"
          (#{data_columns.join(', ')}, external_source, created_at, updated_at)
        SELECT
          #{data_columns.join(', ')},
          '#{external_source}',
          '#{just_now}',
          '#{just_now}'
        FROM "#{target_schema}"."#{stage_table_name}"
        WHERE transition = 'CREATE'
      SQL
    end
load_delete() click to toggle source
# File lib/beetle_etl/steps/load.rb, line 53
    def load_delete
      just_now = now

      database.execute <<-SQL
        UPDATE "#{target_schema}"."#{table_name}" target
        SET
          updated_at = '#{just_now}',
          deleted_at = '#{just_now}'
        FROM "#{target_schema}"."#{stage_table_name}" stage
        WHERE stage.id = target.id
          AND stage.transition = 'DELETE'
      SQL
    end
load_update() click to toggle source
# File lib/beetle_etl/steps/load.rb, line 40
    def load_update
      database.execute <<-SQL
        UPDATE "#{target_schema}"."#{table_name}" target
        SET
          #{updatable_columns.map { |c| %Q("#{c}" = stage."#{c}") }.join(',')},
          "updated_at" = '#{now}',
          deleted_at = NULL
        FROM "#{target_schema}"."#{stage_table_name}" stage
        WHERE stage.id = target.id
          AND stage.transition IN ('UPDATE', 'REINSTATE')
      SQL
    end
run() click to toggle source
# File lib/beetle_etl/steps/load.rb, line 14
def run
  %w(create update delete).each do |transition|
    public_send(:"load_#{transition}")
  end
end

Private Instance Methods

data_columns() click to toggle source
# File lib/beetle_etl/steps/load.rb, line 69
def data_columns
  table_columns - ignored_columns
end
ignored_columns() click to toggle source
# File lib/beetle_etl/steps/load.rb, line 77
def ignored_columns
  IMPORTER_COLUMNS + table_columns.select do |column_name|
    column_name.to_s.index(/^external_.+_id$/)
  end
end
now() click to toggle source
# File lib/beetle_etl/steps/load.rb, line 87
def now
  Time.now
end
table_columns() click to toggle source
# File lib/beetle_etl/steps/load.rb, line 73
def table_columns
  @table_columns ||= database.column_names(target_schema, stage_table_name)
end
updatable_columns() click to toggle source
# File lib/beetle_etl/steps/load.rb, line 83
def updatable_columns
  data_columns - [:id, :external_source, :external_id]
end