Class: BeetleETL::Load
- Inherits:
-
Step
- Object
- Step
- BeetleETL::Load
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, step_name
Methods included from Naming
#stage_table_name, #stage_table_name_sql, #target_table_name, #target_table_name_sql
Constructor Details
#initialize(table_name, relations) ⇒ Load
9
10
11
12
|
# File 'lib/beetle_etl/steps/load.rb', line 9
def initialize(table_name, relations)
super(table_name)
@relations = relations
end
|
Instance Method Details
#dependencies ⇒ Object
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_create ⇒ Object
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 " INSERT INTO \#{target_table_name_sql}\n (\#{data_columns.join(', ')}, external_source, created_at, updated_at)\n SELECT\n \#{data_columns.join(', ')},\n '\#{external_source}',\n '\#{just_now}',\n '\#{just_now}'\n FROM \#{stage_table_name_sql}\n WHERE transition = 'CREATE'\n SQL\nend\n"
|
#load_delete ⇒ Object
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 " UPDATE \#{target_table_name_sql} target\n SET\n updated_at = '\#{just_now}',\n deleted_at = '\#{just_now}'\n FROM \#{stage_table_name_sql} stage\n WHERE stage.id = target.id\n AND stage.transition = 'DELETE'\n SQL\nend\n"
|
#load_update ⇒ Object
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 " UPDATE \#{target_table_name_sql} target\n SET\n \#{updatable_columns.map { |c| %Q(\"\#{c}\" = stage.\"\#{c}\") }.join(',')},\n \"updated_at\" = '\#{now}',\n deleted_at = NULL\n FROM \#{stage_table_name_sql} stage\n WHERE stage.id = target.id\n AND stage.transition IN ('UPDATE', 'REINSTATE')\n SQL\nend\n"
|
#run ⇒ Object
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
|