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, enable_cast: false, logger: RedshiftConnector.logger) ⇒ ActiveRecordExporter

Returns a new instance of ActiveRecordExporter.

Raises:

  • (ArgumentError)


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

def initialize(ds:, query:, bundle_params:, enable_sort: false, enable_cast: false, logger: RedshiftConnector.logger)
  raise ArgumentError, "ActiveRecordExporter does not support type cast" if enable_cast
  @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.



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

def bundle
  @bundle
end

#bundle_paramsObject (readonly)

Returns the value of attribute bundle_params.



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

def bundle_params
  @bundle_params
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#queryObject (readonly)

Returns the value of attribute query.



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

def query
  @query
end

Instance Method Details

#batch_job_labelObject



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

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



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

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