Sha256: 53fc5cb9968f056566f4d64973f23c1f1ab41896901a2316e0ed1b6c689649a4

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

class Ridgepole::Dumper
  def initialize(options = {})
    @options = options
  end

  def dump
    stream = StringIO.new
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)

    dsl = stream.string.lines.select {|line|
      line !~ /\A#/ &&
      line !~ /\AActiveRecord::Schema\.define/ &&
      line !~ /\Aend/
    }.join.undent.strip

    definitions = []

    each_table(dsl) do |name, definition|
      if target?(name)
        definitions << definition
        yield(name, definition) if block_given?
      end
    end

    definitions.join("\n\n")
  end

  private

  def each_table(dsl)
    name = nil
    definition = []

    pass = proc do
      if name
        yield(name, definition.join.strip)
        name = nil
        definition = []
      end
    end

    dsl.lines.each do |line|
      if line =~ /\Acreate_table/
        pass.call
        name = line.split(/[\s,'"]+/)[1]
        definition << line
      elsif name
        definition << line
      end
    end

    pass.call
  end

  def target?(table_name)
    not @options[:tables] or @options[:tables].include?(table_name)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ridgepole-0.1.1 lib/ridgepole/dumper.rb
ridgepole-0.1.0 lib/ridgepole/dumper.rb