module PgPower::SchemaDumper::SchemaMethods

Extends ActiveRecord::SchemaDumper class to dump schemas other than “public” and tables from those schemas.

Public Instance Methods

header_with_schemas(stream) click to toggle source

Dump create schema statements

# File lib/pg_power/schema_dumper/schema_methods.rb, line 5
def header_with_schemas(stream)
  header_without_schemas(stream)
  schemas(stream)
  stream
end
tables_with_schemas(stream) click to toggle source
  • Dumps schemas.

  • Dumps tables from public schema using native tables method.

  • Dumps tables from schemas other than public.

# File lib/pg_power/schema_dumper/schema_methods.rb, line 14
def tables_with_schemas(stream)
  tables_without_schemas(stream)
  non_public_schema_tables(stream)
end

Private Instance Methods

get_non_public_schema_table_names() click to toggle source

Returns a sorted list of non-public schema tables Usage:

get_non_public_schema_table_names # => ['demography.cities','demography.countries','politics.members']
# File lib/pg_power/schema_dumper/schema_methods.rb, line 51
  def get_non_public_schema_table_names
    result = @connection.query(<<-SQL, 'SCHEMA')
      SELECT schemaname || '.' || tablename
      FROM pg_tables
      WHERE schemaname NOT IN ('pg_catalog', 'information_schema', 'public')
      ORDER BY schemaname, tablename
    SQL
    result.flatten
  end
non_public_schema_tables(stream) click to toggle source

Dumps tables from schemas other than public

# File lib/pg_power/schema_dumper/schema_methods.rb, line 37
def non_public_schema_tables(stream)
  get_non_public_schema_table_names.each do |name|
    begin
      table(name, stream)
    rescue ::ActiveRecord::InsufficientPrivilege => exc
      with_warnings(false) { warn("#{exc.class.name}: #{exc.message}. Skipping #{name.inspect}...") }
    end
  end
end
schema(schema_name, stream) click to toggle source

Generates code to create schema.

# File lib/pg_power/schema_dumper/schema_methods.rb, line 31
def schema(schema_name, stream)
  stream << "  create_schema \"#{schema_name}\"\n"
end
schemas(stream) click to toggle source

Generates code to create schemas.

# File lib/pg_power/schema_dumper/schema_methods.rb, line 20
def schemas(stream)
  # Don't create "public" schema since it exists by default.
  schema_names = PgPower::Tools.schemas - ["public", "information_schema"]
  schema_names.each do |schema_name|
    schema(schema_name, stream)
  end
  stream << "\n"
end