Class: MultitenancyTools::TableDumper

Inherits:
Object
  • Object
show all
Defined in:
lib/multitenancy_tools/table_dumper.rb

Overview

TableDumper can be used to generate SQL dumps of the structure and, unlike SchemaDumper, the data of a PostgreSQL table. It requires pg_dump.

The generated dump DOES NOT contain:

  • privilege statements (GRANT/REVOKE)

  • tablespace assigments

  • ownership information

The dump will use INSERTs instead of COPY statements.

Examples:

dumper = MultitenancyTools::TableDumper.new('db name', 'schema name', 'table name')
dumper.dump_to('path/to/file.sql')

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(database, schema, table, options = {}) ⇒ TableDumper

Returns a new instance of TableDumper.

Parameters:

  • database (String)

    database name

  • schema (String)

    schema name

  • table (String)

    table name



23
24
25
26
27
28
29
# File 'lib/multitenancy_tools/table_dumper.rb', line 23

def initialize(database, schema, table, options = {})
  @database = database
  @schema = schema
  @table = table
  @host = options.fetch(:host, '')
  @username = options.fetch(:username, '')
end

Instance Method Details

#dump_to(file, mode: 'w') ⇒ Object

Generates a dump an writes it into a file. Please see IO.new for open modes.

Parameters:

  • file (String)

    file path

  • mode (String) (defaults to: 'w')

    IO open mode

See Also:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/multitenancy_tools/table_dumper.rb', line 38

def dump_to(file, mode: 'w')
  stdout, stderr, status = Dump::DataOnly.new(
    table: @table,
    schema: @schema,
    database: @database,
    host: @host,
    username: @username
  ).dump

  fail(PgDumpError, stderr) unless status.success?

  File.open(file, mode) do |f|
    f.write DumpCleaner.new(stdout, @schema).clean
  end
end