Class: RedshiftConnector::ActiveRecordExporter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ds:, query:, bundle_params:, enable_sort: false, logger: RedshiftConnector.logger) ⇒ ActiveRecordExporter

Returns a new instance of ActiveRecordExporter.



7
8
9
10
11
12
13
14
15
# File 'lib/redshift_connector/active_record_exporter.rb', line 7

def initialize(ds:, query:, bundle_params:, enable_sort: false, logger: RedshiftConnector.logger)
  @ds = ds
  @query = query
  @bundle_params = bundle_params
  @enable_sort = enable_sort
  @logger = logger

  @bundle = S3DataFileBundle.for_params(bundle_params)
end

Instance Attribute Details

#bundleObject (readonly)

Returns the value of attribute bundle.



19
20
21
# File 'lib/redshift_connector/active_record_exporter.rb', line 19

def bundle
  @bundle
end

#bundle_paramsObject (readonly)

Returns the value of attribute bundle_params.



18
19
20
# File 'lib/redshift_connector/active_record_exporter.rb', line 18

def bundle_params
  @bundle_params
end

#loggerObject (readonly)

Returns the value of attribute logger.



20
21
22
# File 'lib/redshift_connector/active_record_exporter.rb', line 20

def logger
  @logger
end

#queryObject (readonly)

Returns the value of attribute query.



17
18
19
# File 'lib/redshift_connector/active_record_exporter.rb', line 17

def query
  @query
end

Instance Method Details

#batch_job_labelObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/redshift_connector/active_record_exporter.rb', line 32

def batch_job_label
  @batch_job_label ||= begin
    components = Dir.getwd.split('/')
    app = if components.last == 'current'
        # is Capistrano environment
        components[-2]
      else
        components[-1]
      end
    batch_file = caller.detect {|c| /redshift_connector|active_record/ !~ c }
    path = batch_file ? batch_file.split(':').first : '?'
    "/* Job: #{app}:#{path} */ "
  end
end

#executeObject



22
23
24
25
26
27
28
29
30
# File 'lib/redshift_connector/active_record_exporter.rb', line 22

def execute
  @bundle.clear
  unload_query = UnloadQuery.new(query: @query, bundle: @bundle, enable_sort: @enable_sort)
  @logger.info "EXPORT #{unload_query.description} -> #{@bundle.url}*"
  stmt = unload_query.to_sql
  @logger.info "[SQL/Redshift] #{batch_job_label}#{stmt.strip}"
  @ds.execute_query(batch_job_label + stmt)
  @bundle
end