Sha256: 67206dabf107c3d58465212d53ca6c07dc2cb8858c233771b7aafa9dcadb17b8

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module ActiveRecordDataLoader
  class FileOutputAdapter
    def initialize(options)
      @filename = options.fetch(:filename, "active_record_data_loader_script.sql")
      @file_basename = File.basename(@filename, File.extname(@filename))
      @path = File.expand_path(File.dirname(@filename))
    end

    def needs_timeout_output?
      true
    end

    def copy(connection:, table:, columns:, data:, row_numbers:)
      data_filename = data_filename(table, row_numbers)
      File.open(data_filename, "w") { |f| f.puts(data) }
      File.open(@filename, "a") do |file|
        file.puts("\\COPY #{table} (#{columns}) FROM '#{data_filename}' WITH (FORMAT CSV);")
      end
    end

    def insert(connection:, command:)
      execute(command)
    end

    def execute(command)
      File.open(@filename, "a") { |f| f.puts("#{command.gsub("\n", ' ')};") }
    end

    private

    def data_filename(table, row_numbers)
      File.join(
        @path,
        "#{@file_basename}_#{table.gsub(/"/, '')}_rows_#{row_numbers[0]}_to_#{row_numbers[-1]}.csv"
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_data_loader-1.2.0 lib/active_record_data_loader/file_output_adapter.rb