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



10
11
12
13
# File 'lib/table_saw/create_dump_file.rb', line 10

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

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#recordsObject (readonly)

Returns the value of attribute records.



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

def records
  @records
end

Instance Method Details

#callObject

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



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
# File 'lib/table_saw/create_dump_file.rb', line 16

def call
  write_to_file "    BEGIN;\n\n    SET session_replication_role = replica;\n    SET statement_timeout = 0;\n    SET lock_timeout = 0;\n    SET client_encoding = 'UTF8';\n    SET standard_conforming_strings = on;\n    SET check_function_bodies = false;\n    SET client_min_messages = warning;\n\n    SET search_path = public, pg_catalog;\n  SQL\n\n  records.each do |name, ids|\n    write_to_file <<~COMMENT\n      --\n      -- Data for Name: \#{name}; Type: TABLE DATA\n      --\n\n    COMMENT\n\n    write_to_file <<~SQL\n      COPY \#{name} (\#{quoted_columns(name)}) FROM STDIN;\n    SQL\n\n    TableSaw::Connection.with do |conn|\n      conn.copy_data \"COPY (select * from \#{name} where id in (\#{ids.join(',')})) TO STDOUT\" do\n        while (row = conn.get_copy_data)\n          write_to_file row\n        end\n      end\n    end\n\n    write_to_file '\\.'\n    write_to_file \"\\n\"\n  end\n\n  refresh_materialized_views\n\n  write_to_file 'COMMIT;'\nend\n"