module PGTrunk::SchemaDumper

@private Overloads methods defined in ActiveRecord::SchemaDumper to redefine how various objects must be dumped.

Public Class Methods

operations() click to toggle source
# File lib/pg_trunk/core/railtie/schema_dumper.rb, line 9
def operations
  @operations ||= []
end
register(operation) click to toggle source
# File lib/pg_trunk/core/railtie/schema_dumper.rb, line 13
def register(operation)
  operations << operation unless operations.include?(operation)
end

Public Instance Methods

dump(stream) click to toggle source

Here we totally redefining the way a schema is dumped.

In Rails every table definition is dumped as an ‘add_table` creator including all its columns, indexes, type casts and foreign keys.

In some circumstances, these objects can have inter-dependencies with others (like functions, custom types and constraints). For example, we could define a function getting table raw as an argument, and then use this function to define check constraint for the table. In this case we must insert the definition of the function between the table’s and constraint’s ones.

That’s why we can neither rely on the method, defined in ActiveRecord nor reuse it through fallback to ‘super` like both Scenic and F(x) do. Instead of it, we fetch object definitions from the database, and then resolve their inter-dependencies.

# File lib/pg_trunk/core/railtie/schema_dumper.rb, line 34
def dump(stream)
  pg_trunk_register_custom_types
  header(stream)
  extensions(stream)
  pg_trunk_objects(stream)
  trailer(stream)
  stream
end

Private Instance Methods

check_constraints_in_create(_table, _stream) click to toggle source
# File lib/pg_trunk/core/railtie/schema_dumper.rb, line 73
def check_constraints_in_create(_table, _stream); end
indexes_in_create(_table, _stream) click to toggle source

Prevent indexes and check constraints from being added to the table

# File lib/pg_trunk/core/railtie/schema_dumper.rb, line 72
def indexes_in_create(_table, _stream); end
pg_trunk_objects(stream) click to toggle source
# File lib/pg_trunk/core/railtie/schema_dumper.rb, line 51
def pg_trunk_objects(stream)
  # Fetch operation definitions from the database.
  #
  # Operations of different kind are fetched
  # in the order of their definitions (see `lib/pg_trunk/definitions.rb`).
  # Operations of the same kind are sorted in a kind-specific order.
  operations = SchemaDumper.operations.flat_map(&:to_a)
  # Limit operations by oids known in `pg_trunk`
  oids = PGTrunk::Registry.pluck(:oid)
  operations = operations.select { |op| oids.include?(op.oid) }
  # Resolve dependencies between fetched commands.
  operations = PGTrunk::DependenciesResolver.resolve(operations)
  # provide the content of the schema.
  operations.each do |cmd|
    cmd.dump(stream)
    stream.puts
    stream.puts
  end
end
pg_trunk_register_custom_types() click to toggle source

Before dumping the schema extract from SQL all custom types to enable their usage in table columns in the schema.

# File lib/pg_trunk/core/railtie/schema_dumper.rb, line 47
def pg_trunk_register_custom_types
  @connection.enable_pg_trunk_types
end