Class: Flydata::SourcePostgresql::GenerateSourceDump

Inherits:
Flydata::Source::GenerateSourceDump show all
Includes:
PostgresqlComponent
Defined in:
lib/flydata/source_postgresql/generate_source_dump.rb

Constant Summary collapse

DUMP_SIZE_QUERY =
<<EOS
SELECT sum(pg_total_relation_size(c.oid)) AS "total_size"
  FROM pg_class c
  LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
  WHERE nspname = %{schema} AND relname in (%{tables})
EOS

Instance Attribute Summary

Attributes inherited from Flydata::Source::GenerateSourceDump

#dp

Instance Method Summary collapse

Methods included from PostgresqlComponent

#de_prefs

Methods inherited from Flydata::Source::GenerateSourceDump

inherited, #initialize

Methods inherited from Flydata::Source::Component

#initialize

Constructor Details

This class inherits a constructor from Flydata::Source::GenerateSourceDump

Instance Method Details

#confirmation_itemsObject


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/flydata/source_postgresql/generate_source_dump.rb', line 25

def confirmation_items
  items = {
    "host"     => de_prefs['host'],
    "port"     => de_prefs['port'],
    "username" => de_prefs['username'],
    "database" => de_prefs['database'],
    "schema"   => de_prefs['schema'],
  }

  items
end

#dump(tables, file_path = nil, &src_pos_callback) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/flydata/source_postgresql/generate_source_dump.rb', line 62

def dump(tables, file_path = nil, &src_pos_callback)
  io = nil
  if file_path
    io = File.open(file_path, "w")
  else
    raise "dump via pipe has not been implemented yet"
  end

  table_meta = Flydata::SourcePostgresql::TableMeta.new(de_prefs, tables)
  cli = FlydataCore::Postgresql::PGClient.new(de_prefs)
  table_meta.reload(cli)

  tables_missing_meta = tables.select{|t| tm = table_meta[t]; tm.nil? || tm.empty?}
  unless tables_missing_meta.empty?
    raise "Tables are not available.  Check if the following table(s) exist and are visible: #{tables_missing_meta.join(",")}"
  end

  context = source.sync_generate_table_ddl(dp, nil)
  source_pos = get_source_pos(table_meta.current_snapshot, &src_pos_callback)

  options = de_prefs.merge(table_meta: table_meta)
  missing_tables = context.each_source_tabledef(tables, options) do |tabledef, error|
    dump_table(tabledef, source_pos, io, cli) if tabledef
  end

  nil
ensure
  cli.close if cli
  io.close if io
end

#dump_size(tables) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/flydata/source_postgresql/generate_source_dump.rb', line 44

def dump_size(tables)
  cli = FlydataCore::Postgresql::PGClient.new(de_prefs)

  base_query = DUMP_SIZE_QUERY % {
    schema: FlydataCore::Postgresql::QueryHelper.schema_as_value(de_prefs['schema']),
    tables: '%s'   # Use binding parameters for tables
  }
  query = FlydataCore::Postgresql::PGQuery.new(base_query,
            placeholder_size: tables.size,
            placeholder_start_num: 1)
  res = cli.query(query, tables)

  res.first['total_size'].to_i

ensure
  cli.close if cli
end

#run_compatibility_check(dump_dir, backup_dir) ⇒ Object


16
17
18
19
20
21
22
23
# File 'lib/flydata/source_postgresql/generate_source_dump.rb', line 16

def run_compatibility_check(dump_dir, backup_dir)
  %w(host username database).each do |k|
    if de_prefs[k].to_s.empty?
      raise "'#{k}' is required. Set the value in the conf file " +
            "-> #{Flydata::Preference::DataEntryPreference.conf_path(de)}"
    end
  end
end