Class: Ridgepole::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/ridgepole/dumper.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Dumper

Returns a new instance of Dumper.



5
6
7
8
# File 'lib/ridgepole/dumper.rb', line 5

def initialize(options = {})
  @options = options
  @logger = Ridgepole::Logger.instance
end

Instance Method Details

#dumpObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ridgepole/dumper.rb', line 10

def dump
  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|
      ActiveRecord::SchemaDumper.ignore_tables << tbl if ignore_tables.any? { |i| i =~ tbl } && !(target_tables && target_tables.include?(tbl))
    end
  end

  stream = dump_from(conn)

  ActiveRecord::SchemaDumper.ignore_tables.clear if target_tables || ignore_tables

  stream.string.lines.each_cons(2) do |first_line, second_line|
    if first_line.start_with?('# Could not dump')
      @logger.warn("[WARNING] #{first_line.sub(/\A# /, '').chomp}")
      @logger.warn(second_line.sub(/\A#/, '').chomp)
    end
  end

  dsl = stream.string.lines.select do |line|
    line !~ /\A#/ &&
      line !~ /\AActiveRecord::Schema(\[\d\.\d\])?\.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