Module: SchemaMonkey::ActiveRecord::SchemaDumper

Defined in:
lib/schema_monkey/active_record/schema_dumper.rb

Defined Under Namespace

Classes: Dump

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 101

def self.included(base)
  base.class_eval do
    alias_method_chain :dump, :schema_monkey
    alias_method_chain :extensions, :schema_monkey
    alias_method_chain :tables, :schema_monkey
    alias_method_chain :table, :schema_monkey
    alias_method_chain :foreign_keys, :schema_monkey
    alias_method_chain :trailer, :schema_monkey
    public :ignored?
  end
end

Instance Method Details

#dump_with_schema_monkey(stream) ⇒ Object



113
114
115
116
117
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 113

def dump_with_schema_monkey(stream)
  @dump = Dump.new(self)
  dump_without_schema_monkey(stream)
  @dump.assemble(stream)
end

#extensions_with_schema_monkey(_) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 131

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

#foreign_keys_with_schema_monkey(table, _) ⇒ Object



119
120
121
122
123
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 119

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

#table_with_schema_monkey(table, _) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 145

def table_with_schema_monkey(table, _)
  Middleware::Dumper::Table.start dumper: self, connection: @connection, dump: @dump, table: @dump.tables[table] = Dump::Table.new(name: table) do |env|
    stream = StringIO.new
    table_without_schema_monkey(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
      Dump::Table::Column.new(name: m[:name], type: m[:type], options: m[:options])
    }
  end
end

#tables_with_schema_monkey(_) ⇒ Object



139
140
141
142
143
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 139

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

#trailer_with_schema_monkey(_) ⇒ Object



125
126
127
128
129
# File 'lib/schema_monkey/active_record/schema_dumper.rb', line 125

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