Class: MultitenancyTools::TableDumper
- Inherits:
-
Object
- Object
- MultitenancyTools::TableDumper
- 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.
Instance Method Summary collapse
-
#dump_to(file, mode: 'w') ⇒ Object
Generates a dump an writes it into a file.
-
#initialize(database, schema, table) ⇒ TableDumper
constructor
A new instance of TableDumper.
Constructor Details
#initialize(database, schema, table) ⇒ TableDumper
Returns a new instance of TableDumper.
23 24 25 26 27 |
# File 'lib/multitenancy_tools/table_dumper.rb', line 23 def initialize(database, schema, table) @database = database @schema = schema @table = table 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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/multitenancy_tools/table_dumper.rb', line 36 def dump_to(file, mode: 'w') stdout, stderr, status = Open3.capture3( 'pg_dump', '--table', "#{@schema}.#{@table}", '--data-only', '--no-privileges', '--no-tablespaces', '--no-owner', '--inserts', '--dbname', @database ) fail(PgDumpError, stderr) unless status.success? File.open(file, mode) do |f| f.write DumpCleaner.new(stdout).clean end end |