Sha256: 242fb2a1d292d5c8b1fc2beed9d6de139a09e755cb9f7856a8d2405a57c647bc

Contents?: true

Size: 1.74 KB

Versions: 21

Compression:

Stored size: 1.74 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.data_sources.each do |tbl|
        next if target_tables.include?(tbl)
        ActiveRecord::SchemaDumper.ignore_tables << tbl
      end
    end

    if ignore_tables
      conn.data_sources.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 do |line|
      line !~ /\A#/ &&
      line !~ /\AActiveRecord::Schema\.define/ &&
      line !~ /\Aend/
    end

    dsl = dsl.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

21 entries across 21 versions & 1 rubygems

Version Path
ridgepole-0.7.0.beta18 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta17 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta16 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta15 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta14 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta13 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta12 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta11 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta10 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta9 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta8 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta7 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta6 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta5 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta4 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta3 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta2 lib/ridgepole/dumper.rb
ridgepole-0.7.0.beta lib/ridgepole/dumper.rb
ridgepole-0.7.0.alpha3 lib/ridgepole/dumper.rb
ridgepole-0.7.0.alpha2 lib/ridgepole/dumper.rb