Module: MigrationComments::ActiveRecord::ConnectionAdapters::Mysql2Adapter

Defined in:
lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb

Instance Method Summary collapse

Instance Method Details

#add_column_options!(sql, options) ⇒ Object



57
58
59
60
61
62
# File 'lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb', line 57

def add_column_options!(sql, options)
  super(sql, options)
  if options.keys.include?(:comment)
    sql << comment_sql(CommentDefinition.new(nil, nil, options[:comment]))
  end
end

#add_sql_comment!(sql, comment) ⇒ Object



22
23
24
25
# File 'lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb', line 22

def add_sql_comment!(sql, comment)
  comment_text = comment.respond_to?(:comment_text) ? comment.comment_text : comment
  super(sql, comment_text)
end

#change_column(table_name, column_name, type, options = {}) ⇒ Object



50
51
52
53
54
55
# File 'lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb', line 50

def change_column(table_name, column_name, type, options={})
  unless options.keys.include?(:comment)
    options.merge!(:comment => retrieve_column_comment(table_name, column_name))
  end
  super(table_name, column_name, type, options)
end

#comment_sql(comment_definition) ⇒ Object



64
65
66
# File 'lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb', line 64

def comment_sql(comment_definition)
  " COMMENT #{escaped_comment(comment_definition.comment_text)}"
end

#comments_supported?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb', line 4

def comments_supported?
  true
end

#create_table(table_name, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb', line 37

def create_table(table_name, options={})
  local_table_definition = nil
  super(table_name, options) do |td|
    local_table_definition = td
    local_table_definition.comment = options[:comment] if options.has_key?(:comment)
    yield td if block_given?
  end
  comments = local_table_definition.collect_comments(table_name)
  comments.each do |comment_definition|
    execute_comment comment_definition
  end
end

#execute_comment(comment_definition) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb', line 68

def execute_comment(comment_definition)
  if comment_definition.table_comment?
    set_table_comment comment_definition.table_name, comment_definition.comment_text
  else
    set_column_comment comment_definition.table_name, comment_definition.column_name, comment_definition.comment_text
  end
end

#retrieve_column_comments(table_name, *column_names) ⇒ Object



32
33
34
35
# File 'lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb', line 32

def retrieve_column_comments(table_name, *column_names)
  result = select_rows(column_comment_sql(table_name, *column_names)) || []
  Hash[result.map{|row| [row[0].to_sym, row[1].presence]}]
end

#retrieve_table_comment(table_name) ⇒ Object



28
29
30
# File 'lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb', line 28

def retrieve_table_comment(table_name)
  select_value(table_comment_sql(table_name)).presence
end

#set_column_comment(table_name, column_name, comment_text) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb', line 12

def set_column_comment(table_name, column_name, comment_text)
  column = column_for(table_name, column_name)
  options = {
    comment: comment_text,
    auto_increment: !!(column.extra =~ /auto_increment/)      # MySQL adapter drops this on calls to change_column
  }
  change_column table_name, column_name, column.sql_type, options
end

#set_table_comment(table_name, comment_text) ⇒ Object



8
9
10
# File 'lib/migration_comments/active_record/connection_adapters/mysql2_adapter.rb', line 8

def set_table_comment(table_name, comment_text)
  execute "ALTER TABLE #{quote_table_name table_name} COMMENT #{escaped_comment(comment_text)}"
end