Class: MultitenancyTools::SchemaDumper

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

Overview

SchemaDumper can be used to generate SQL dumps of the structure of a PostgreSQL schema. It requires pg_dump.

The generated dump DOES NOT contain:

  • privilege statements (GRANT/REVOKE)

  • tablespace assigments

  • ownership information

  • any table data

SchemaDumper is suitable to create SQL templates for SchemaCreator.

Examples:

dumper = MultitenancyTools::SchemaDumper.new('my_db', 'my_schema')
dumper.dump_to('path/to/file.sql')

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(database, schema) ⇒ SchemaDumper

Returns a new instance of SchemaDumper.

Parameters:

  • database (String)

    database name

  • schema (String)

    schema name



23
24
25
26
# File 'lib/multitenancy_tools/schema_dumper.rb', line 23

def initialize(database, schema)
  @database = database
  @schema = schema
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:



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

def dump_to(file, mode: 'w')
  stdout, stderr, status = Open3.capture3(
    'pg_dump',
    '--schema', @schema,
    '--schema-only',
    '--no-privileges',
    '--no-tablespaces',
    '--no-owner',
    '--dbname', @database
  )

  fail(PgDumpError, stderr) unless status.success?

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