Sha256: 8d04f9c32c58c556a6fca64cbf83db7298df6654025445741c2ec5bd6e8c335b

Contents?: true

Size: 1.89 KB

Versions: 16

Compression:

Stored size: 1.89 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 } and not (target_tables and target_tables.include?(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/
    }.map {|line|
      if @options[:dump_without_table_options] and line =~ /\A  create_table /
        line.gsub(/, options: ("(?:[^"]|\")*")/, '')
      else
        line
      end
    }.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

16 entries across 16 versions & 1 rubygems

Version Path
ridgepole-0.6.5.beta lib/ridgepole/dumper.rb
ridgepole-0.6.4 lib/ridgepole/dumper.rb
ridgepole-0.6.4.beta9 lib/ridgepole/dumper.rb
ridgepole-0.6.4.beta8 lib/ridgepole/dumper.rb
ridgepole-0.6.4.beta7 lib/ridgepole/dumper.rb
ridgepole-0.6.4.beta6 lib/ridgepole/dumper.rb
ridgepole-0.6.4.beta5 lib/ridgepole/dumper.rb
ridgepole-0.6.4.beta4 lib/ridgepole/dumper.rb
ridgepole-0.6.4.beta3 lib/ridgepole/dumper.rb
ridgepole-0.6.4.beta2 lib/ridgepole/dumper.rb
ridgepole-0.6.4.beta lib/ridgepole/dumper.rb
ridgepole-0.6.3 lib/ridgepole/dumper.rb
ridgepole-0.6.3.beta5 lib/ridgepole/dumper.rb
ridgepole-0.6.3.beta4 lib/ridgepole/dumper.rb
ridgepole-0.6.3.beta3 lib/ridgepole/dumper.rb
ridgepole-0.6.3.beta2 lib/ridgepole/dumper.rb