Module: MigrationComments::ActiveRecord::SchemaDumper

Includes:
SchemaFormatter
Defined in:
lib/migration_comments/active_record/schema_dumper.rb

Instance Method Summary collapse

Methods included from SchemaFormatter

#render_comment, #render_kv_pair, #render_value

Instance Method Details

#append_comments(table, stream) ⇒ Object



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
# File 'lib/migration_comments/active_record/schema_dumper.rb', line 15

def append_comments(table, stream)
  table_name = table.inspect.gsub('"', '')
  table_comment = @connection.retrieve_table_comment(table_name)
  column_comments = @connection.retrieve_column_comments(table_name)
  comment_stream = StringIO.new
  lines = []
  table_line = 0
  col_names = {}
  error = false
  while (stream_line = stream.gets)
    content = stream_line.chomp
    if content =~ /^# Could not dump table "#{table_name}"/
      error = true
    elsif content =~ /create_table\s/
      table_line = lines.size
    elsif content =~ /t\.\w+\s+"(\w+)"/
      col_names[lines.size] = $1.to_sym
    end
    lines << content
  end
  len = col_names.keys.map{|index| lines[index]}.map(&:length).max + 2 unless col_names.empty?
  lines.each_with_index do |line, index|
    if error
      # do nothing
    elsif table_line == index && table_comment.present?
      block_init = " do |t|"
      line.chomp!(block_init) << ", " << render_comment(table_comment) << block_init
    elsif col_names[index] && ::ActiveRecord::VERSION::MAJOR < 5
      comment = column_comments[col_names[index]]
      line << ',' << ' ' * (len - line.length) << render_comment(comment) unless comment.blank?
    end
    comment_stream.puts line
  end
  comment_stream.rewind
  comment_stream
end

#table(table, stream) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/migration_comments/active_record/schema_dumper.rb', line 5

def table(table, stream)
  return super if ::ActiveRecord::VERSION::MAJOR >= 5 && @connection.class.name !~ /SQLite/
  tbl_stream = StringIO.new
  super(table, tbl_stream)
  tbl_stream.rewind
  commented_stream = append_comments(table, tbl_stream)
  tbl_stream.close
  stream.print commented_stream.read
end