Class: BeetleETL::Load

Inherits:
Step
  • Object
show all
Defined in:
lib/beetle_etl/steps/load.rb

Constant Summary collapse

IMPORTER_COLUMNS =
%i[
  external_source
  transition
]

Instance Attribute Summary

Attributes inherited from Step

#table_name

Instance Method Summary collapse

Methods inherited from Step

#database, #external_source, #name, #stage_table_name, step_name, #target_schema

Constructor Details

#initialize(config, table_name, relations) ⇒ Load

Returns a new instance of Load.



9
10
11
12
# File 'lib/beetle_etl/steps/load.rb', line 9

def initialize(config, table_name, relations)
  super(config, table_name)
  @relations = relations
end

Instance Method Details

#dependenciesObject



20
21
22
# File 'lib/beetle_etl/steps/load.rb', line 20

def dependencies
  @relations.values.map { |d| Load.step_name(d) }.to_set
end

#load_createObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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_deleteObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# 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_updateObject



40
41
42
43
44
45
46
47
48
49
50
51
# 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

#runObject



14
15
16
17
18
# File 'lib/beetle_etl/steps/load.rb', line 14

def run
  %w(create update delete).each do |transition|
    public_send(:"load_#{transition}")
  end
end