Class: TableSaw::CreateDumpFile

Inherits:
Object
  • Object
show all
Defined in:
lib/table_saw/create_dump_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, file = 'psql.dump') ⇒ CreateDumpFile

Returns a new instance of CreateDumpFile.



7
8
9
10
# File 'lib/table_saw/create_dump_file.rb', line 7

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

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/table_saw/create_dump_file.rb', line 5

def file
  @file
end

#recordsObject (readonly)

Returns the value of attribute records.



5
6
7
# File 'lib/table_saw/create_dump_file.rb', line 5

def records
  @records
end

Instance Method Details

#callObject

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/table_saw/create_dump_file.rb', line 13

def call
  File.delete(file) if File.exist?(file)

  alter_constraints_to_deferrable

  write_to_file <<~SQL
    BEGIN;

    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, table|
    defer_constraints(name)

    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 (#{table.copy_statement}) TO STDOUT" do
        while (row = conn.get_copy_data)
          write_to_file row
        end
      end
    end

    write_to_file '\.'
    write_to_file "\n"
  end

  refresh_materialized_views
  restart_sequences

  write_to_file 'COMMIT;'
end