Sha256: 792a79b43073633c1d767620398bbc7ba89bd2873e4a3d4ac29de4125f9d9469

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module DataTaster
  # Selects and processes tables from the source_db
  # to insert (or query) into the working_db
  class Sample
    def initialize(table_name)
      @table_name = table_name
      @include_insert = DataTaster.config.include_insert
      @collection = DataTaster::Collection.new(
        table_name
      ).assemble
    end

    def serve!
      # Any table that does not return SQL is considered deprecated and we should fully skip it
      if collection.empty? && include_insert
        DataTaster.safe_execute("DROP TABLE IF EXISTS #{table_name}")
      else
        ensure_empty_table
        process_select(collection[:select])
        DataTaster::Sanitizer.new(table_name, collection[:sanitize]).clean!
      end
    end

  private

    attr_reader :table_name, :include_insert, :collection

    def ensure_empty_table
      DataTaster.safe_execute("TRUNCATE TABLE #{working_db}.#{table_name}")
    end

    def process_select(sql)
      DataTaster.safe_execute(sql)
    rescue => e
      e.message << " executing SQL statement for #{table_name}: #{sql}"
      raise e
    end

    def working_db
      @working_db ||= DataTaster.config.working_client.query_options[:database]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
data_taster-0.4.0 lib/data_taster/sample.rb
data_taster-0.3.0 lib/data_taster/sample.rb
data_taster-0.2.2 lib/data_taster/sample.rb