Module: SchemaPlus::Core::ActiveRecord::SchemaDumper

Defined in:
lib/schema_plus/core/active_record/schema_dumper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



9
10
11
12
13
# File 'lib/schema_plus/core/active_record/schema_dumper.rb', line 9

def self.prepended(base)
  base.class_eval do
    public :ignored?
  end
end

Instance Method Details

#dump(stream) ⇒ Object



15
16
17
18
19
# File 'lib/schema_plus/core/active_record/schema_dumper.rb', line 15

def dump(stream)
  @dump = SchemaDump.new(self)
  super stream
  @dump.assemble(stream)
end

#extensions(_) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/schema_plus/core/active_record/schema_dumper.rb', line 33

def extensions(_)
  SchemaMonkey::Middleware::Dumper::Initial.start(dumper: self, connection: @connection, dump: @dump, initial: @dump.initial) do |env|
    stream = StringIO.new
    super stream
    env.dump.initial << stream.string unless stream.string.blank?
  end
end

#foreign_keys(table, _) ⇒ Object



21
22
23
24
25
# File 'lib/schema_plus/core/active_record/schema_dumper.rb', line 21

def foreign_keys(table, _)
  stream = StringIO.new
  super table, stream
  @dump.final += stream.string.split("\n").map(&:strip)
end

#indexes(table, _) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/schema_plus/core/active_record/schema_dumper.rb', line 80

def indexes(table, _)
  SchemaMonkey::Middleware::Dumper::Indexes.start(dumper: self, connection: @connection, dump: @dump, table: @dump.tables[table]) do |env|
    stream = StringIO.new
    super env.table.name, stream
    env.table.indexes += stream.string.split("\n").map { |string|
      m = string.strip.match %r{
      ^
      add_index \s*
        [:'"](?<table>[^'"\s]+)['"]? \s* , \s*
        (?<columns>.*) \s*
        name: \s* [:'"](?<name>[^'"\s]+)['"]? \s*
        (, \s* (?<options>.*))?
        $
      }x
      columns = m[:columns].tr(%q{[]'":}, '').strip.split(/\s*,\s*/)
      SchemaDump::Table::Index.new name: m[:name], columns: columns, options: m[:options]
    }
  end
end

#table(table, _) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/schema_plus/core/active_record/schema_dumper.rb', line 47

def table(table, _)
  SchemaMonkey::Middleware::Dumper::Table.start(dumper: self, connection: @connection, dump: @dump, table: @dump.tables[table] = SchemaDump::Table.new(name: table)) do |env|
    stream = StringIO.new
    super env.table.name, stream
    m = stream.string.match %r{
    \A \s*
      create_table \s*
      [:'"](?<name>[^'"\s]+)['"]? \s*
      ,? \s*
      (?<options>.*) \s+
      do \s* \|t\| \s* $
    (?<columns>.*)
    ^\s*end\s*$
    (?<trailer>.*)
    \Z
    }xm
    env.table.pname = m[:name]
    env.table.options = m[:options].strip
    env.table.trailer = m[:trailer].split("\n").map(&:strip).reject{|s| s.blank?}
    env.table.columns = m[:columns].strip.split("\n").map { |col|
      m = col.strip.match %r{
      ^
      t\.(?<type>\S+) \s*
        [:'"](?<name>[^"\s]+)[,"]? \s*
        ,? \s*
        (?<options>.*)
      $
      }x
      SchemaDump::Table::Column.new(name: m[:name], type: m[:type], options: m[:options])
    }
  end
end

#tables(_) ⇒ Object



41
42
43
44
45
# File 'lib/schema_plus/core/active_record/schema_dumper.rb', line 41

def tables(_)
  SchemaMonkey::Middleware::Dumper::Tables.start(dumper: self, connection: @connection, dump: @dump) do |env|
    super nil
  end
end

#trailer(_) ⇒ Object



27
28
29
30
31
# File 'lib/schema_plus/core/active_record/schema_dumper.rb', line 27

def trailer(_)
  stream = StringIO.new
  super stream
  @dump.trailer = stream.string
end