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

#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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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
    if m.nil?
      env.table.alt = stream.string
    else
      env.table.pname = m[:name]
      env.table.options = m[:options].strip
      env.table.trailer = m[:trailer].split("\n").map(&:strip).reject{|s| s.blank?}
      table_objects = m[:columns].strip.split("\n").map { |col|
        cs = col.strip
        m = cs.match %r{
        ^
        t\.(?<type>\S+) \s*
          [:'"](?<name>[^"\s]+)[,"]? \s*
          ,? \s*
          (?<options>.*)
        $
        }x
        if !m.nil?
          SchemaDump::Table::Column.new name: m[:name], type: m[:type], options: eval("{" + m[:options] + "}"), comments: []
        else
          m = cs.match %r{
          ^
          t\.index \s*
            \[(?<index_cols>.*?)\] \s*
            , \s*
            name\: \s* [:'"](?<name>[^"\s]+)[,"]? \s*
            ,? \s*
            (?<options>.*)
          $
          }x
          if m.nil?
            nil
          else
            index_cols = m[:index_cols].tr(%q{'":}, '').strip.split(/\s*,\s*/)
            SchemaDump::Table::Index.new name: m[:name], columns: index_cols, options: eval("{#{m[:options]}}")
          end
        end
      }.reject { |o| o.nil? }
      env.table.columns = table_objects.select { |o| o.is_a? SchemaDump::Table::Column }
      env.table.indexes = table_objects.select { |o| o.is_a? SchemaDump::Table::Index }
    end
  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