Sha256: d089502812c287c8068e14b25b2c1df148a9f40d85ee0c01dc44eb711df41770

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require 'table_saw/queries/table_columns'

module TableSaw
  class CreateDumpFile
    attr_reader :records, :file

    def initialize(records, file = 'psql.dump')
      @records = records
      @file = file
    end

    # rubocop:disable Metrics/MethodLength
    def call
      write_to_file <<~SQL
        BEGIN;

        SET session_replication_role = replica;
        SET statement_timeout = 0;
        SET lock_timeout = 0;
        SET client_encoding = 'UTF8';
        SET standard_conforming_strings = on;
        SET check_function_bodies = false;
        SET client_min_messages = warning;

        SET search_path = public, pg_catalog;
      SQL

      records.each do |name, ids|
        write_to_file <<~COMMENT
          --
          -- Data for Name: #{name}; Type: TABLE DATA
          --

        COMMENT

        write_to_file <<~SQL
          COPY #{name} (#{quoted_columns(name)}) FROM STDIN;
        SQL

        TableSaw::Connection.with do |conn|
          conn.copy_data "COPY (select * from #{name} where id in (#{ids.join(',')})) TO STDOUT" do
            while (row = conn.get_copy_data)
              write_to_file row
            end
          end
        end

        write_to_file '\.'
        write_to_file "\n"
      end

      write_to_file 'COMMIT;'
    end
    # rubocop:enable Metrics/MethodLength

    private

    def write_to_file(data)
      File.open(file, 'ab') { |f| f.puts(data) }
    end

    def quoted_columns(table)
      TableSaw::Queries::TableColumns.new(table).call.map { |c| "\"#{c}\"" }.join(', ')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
table_saw-0.2.1 lib/table_saw/create_dump_file.rb
table_saw-0.2.0 lib/table_saw/create_dump_file.rb