Sha256: 9cc370e4c3cf2d1b9b23586a9f1f48b9ba5ccf4214fca9904661d5ede0cb1c08
Contents?: true
Size: 1.6 KB
Versions: 1
Compression:
Stored size: 1.6 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,Metrics/AbcSize 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.to_a.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,Metrics/AbcSize 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
table_saw-0.1.0 | lib/table_saw/create_dump_file.rb |