Sha256: 55409044d76bb10141dd7d97334edd6006d097ad5b96c00b567f50066c73af0a

Contents?: true

Size: 1.65 KB

Versions: 37

Compression:

Stored size: 1.65 KB

Contents

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

  def dump
    stream = StringIO.new
    conn = ActiveRecord::Base.connection
    target_tables = @options[:tables]
    ignore_tables = @options[:ignore_tables]

    if target_tables
      conn.tables.each do |tbl|
        next if target_tables.include?(tbl)
        ActiveRecord::SchemaDumper.ignore_tables << tbl
      end
    end

    if ignore_tables
      conn.tables.each do |tbl|
        if ignore_tables.any? {|i| i =~ tbl }
          ActiveRecord::SchemaDumper.ignore_tables << tbl
        end
      end
    end

    ActiveRecord::SchemaDumper.dump(conn, stream)

    if target_tables or ignore_tables
      ActiveRecord::SchemaDumper.ignore_tables.clear
    end

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

    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

37 entries across 37 versions & 1 rubygems

Version Path
ridgepole-0.5.1 lib/ridgepole/dumper.rb
ridgepole-0.5.1.beta3 lib/ridgepole/dumper.rb
ridgepole-0.5.1.beta2 lib/ridgepole/dumper.rb
ridgepole-0.5.1.beta lib/ridgepole/dumper.rb
ridgepole-0.5.0 lib/ridgepole/dumper.rb
ridgepole-0.5.0.beta lib/ridgepole/dumper.rb
ridgepole-0.4.12 lib/ridgepole/dumper.rb
ridgepole-0.4.11.beta lib/ridgepole/dumper.rb
ridgepole-0.4.10 lib/ridgepole/dumper.rb
ridgepole-0.4.10.beta lib/ridgepole/dumper.rb
ridgepole-0.4.9 lib/ridgepole/dumper.rb
ridgepole-0.4.8 lib/ridgepole/dumper.rb
ridgepole-0.4.8.rc2 lib/ridgepole/dumper.rb
ridgepole-0.4.8.rc1 lib/ridgepole/dumper.rb
ridgepole-0.4.7 lib/ridgepole/dumper.rb
ridgepole-0.4.6 lib/ridgepole/dumper.rb
ridgepole-0.4.5 lib/ridgepole/dumper.rb
ridgepole-0.4.4 lib/ridgepole/dumper.rb
ridgepole-0.4.3 lib/ridgepole/dumper.rb
ridgepole-0.4.2 lib/ridgepole/dumper.rb