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

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

Constant Summary collapse

TABLE_COLUMN_MATCHES =
[
    [ # first match expression index case
        %r{
          ^
          t\.index \s*
            "(?<index_cols>(?:[^"\\]|\\.)*?)" \s*
            , \s*
            name\: \s* [:'"](?<name>[^"\s]+)[,"]? \s*
            ,? \s*
            (?<options>.*)
          $
          }x,
        ->(m) {
          index_cols = m[:index_cols].gsub('\\"', '"')
          SchemaDump::Table::Index.new name: m[:name], columns: index_cols, options: eval("{" + m[:options] + "}")
        }
    ],
    [ # general matching of columns
        %r{
          ^
          t\.(?<type>\S+) \s*
            [:'"](?<name>[^"\s]+)[,"]? \s*
            ,? \s*
            (?<options>.*)
          $
          }x,
        ->(m) {
          SchemaDump::Table::Column.new name: m[:name], type: m[:type], options: eval("{" + m[:options] + "}"), comments: []
        }
    ],
    [ # index definitions with multiple columns
        %r{
          ^
          t\.index \s*
            \[(?<index_cols>.*?)\] \s*
            , \s*
            name\: \s* [:'"](?<name>[^"\s]+)[,"]? \s*
            ,? \s*
            (?<options>.*)
          $
          }x,
        ->(m) {
          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]}}")
        }
    ]
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



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

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

Instance Method Details

#dump(stream) ⇒ Object



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

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

#extensions(_) ⇒ Object



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

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



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

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

#table(table, _) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/schema_plus/core/active_record/schema_dumper.rb', line 97

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
        result = nil
        # find the first regex that matches and grab the column definition
        TABLE_COLUMN_MATCHES.find do |(r, l)|
          m = cs.match r
          result = m.nil? ? nil : l.call(m)
        end
        result
      }.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



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

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

#trailer(_) ⇒ Object



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

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