class PGTrunk::Operations::Tables::CreateTable

@private

When dealing with tables we’re only interested in dumping tables one-by-one to enable other operations in between tables.

We doesn’t overload the method ‘create_table`, but keep the original implementation unchanged. That’s why neither ‘to_sql`, `invert` or `generates_object` are necessary.

While we rely on the original implementation, there are some differences in a way we fetching tables and dumping them to the schema:

Public Instance Methods

to_ruby() click to toggle source

Instead of defining ruby_snippet, we overload the to_ruby to rely on the original implementation.

We overloaded the ActiveRecord::SchemaDumper methods indexes_in_create and check_constraints_in_create so that they do nothing to exclude indexes and constraints from a table definition.

@see PGTrunk::SchemaDumper module (in ‘core/railtie`).

# File lib/pg_trunk/operations/tables/create_table.rb, line 52
def to_ruby
  stream = StringIO.new
  PGTrunk.dumper.send(:table, name.lean, stream)
  unindent(stream.string)
end

Private Instance Methods

unindent(snippet) click to toggle source

ActiveRecord builds the dump indented by 2 space chars. Because the to_ruby method is used in error messages, we do indentation separately in the PGTrunk::SchemaDumper.

That’s why we have to unindent the original snippet provided by the +ActiveRecord::Dumper##table+ method call back by 2 space characters.

The ‘.strip << “n”` is added for the compatibility with the RubyBuilder which returns snippets having one trailing newline only.

# File lib/pg_trunk/operations/tables/create_table.rb, line 71
def unindent(snippet)
  snippet.lines.map { |line| line.sub(/^ {1,2}/, "") }.join.strip << "\n"
end