Class: RedshiftConnector::Importer::RebuildRename

Inherits:
Object
  • Object
show all
Defined in:
lib/redshift_connector/importer/rebuild_rename.rb

Instance Method Summary collapse

Constructor Details

#initialize(dao:, columns:, logger: RedshiftConnector.logger) ⇒ RebuildRename

Returns a new instance of RebuildRename.



6
7
8
9
10
# File 'lib/redshift_connector/importer/rebuild_rename.rb', line 6

def initialize(dao:, columns:, logger: RedshiftConnector.logger)
  @dao = dao
  @columns = columns
  @logger = logger
end

Instance Method Details

#exec_update(query) ⇒ Object



28
29
30
31
# File 'lib/redshift_connector/importer/rebuild_rename.rb', line 28

def exec_update(query)
  @logger.info query
  @dao.connection.execute(query)
end

#execute(bundle) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/redshift_connector/importer/rebuild_rename.rb', line 12

def execute(bundle)
  dest_table = @dao.table_name
  tmp_table = "#{dest_table}_new"
  old_table = "#{dest_table}_old"

  tmp_dao = @dao.dup
  tmp_dao.table_name = tmp_table

  exec_update "drop table if exists #{tmp_table}"
  exec_update "create table #{tmp_table} like #{dest_table}"
  import(tmp_dao, bundle)
  exec_update "drop table if exists #{old_table}"
  # Atomic table exchange
  exec_update "rename table #{dest_table} to #{old_table}, #{tmp_table} to #{dest_table}"
end

#import(dao, bundle) ⇒ Object



33
34
35
36
37
38
# File 'lib/redshift_connector/importer/rebuild_rename.rb', line 33

def import(dao, bundle)
  @logger.info "IMPORT #{bundle.url}* -> #{dao.table_name} (#{@columns.join(', ')})"
  bundle.each_batch do |rows|
    dao.import(@columns, rows)
  end
end