class RedshiftConnector::ActiveRecordExporter

Attributes

bundle[R]
bundle_params[R]
logger[R]
query[R]

Public Class Methods

new(ds:, query:, bundle_params:, enable_sort: false, logger: RedshiftConnector.logger) click to toggle source
# 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

Public Instance Methods

batch_job_label() click to toggle source
# 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
execute() click to toggle source
# 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