Module: MigrationComments::ActiveRecord::ConnectionAdapters::MysqlAdapter

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
# File 'lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb', line 3

def self.included(base)
  base.class_eval do
    alias_method_chain :create_table, :migration_comments
    alias_method_chain :change_column, :migration_comments
  end
end

Instance Method Details

#add_column_options!(sql, options) ⇒ Object



61
62
63
64
65
66
# File 'lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb', line 61

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

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



54
55
56
57
58
59
# File 'lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb', line 54

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

#comment_sql(comment_definition) ⇒ Object



68
69
70
# File 'lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb', line 68

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

#comments_supported?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb', line 10

def comments_supported?
  true
end

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



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb', line 40

def create_table_with_migration_comments(table_name, options={})
  local_table_definition = nil
  create_table_without_migration_comments(table_name, options) do |td|
    local_table_definition = td
    local_table_definition.base = self
    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



72
73
74
75
76
77
78
# File 'lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb', line 72

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



34
35
36
37
38
# File 'lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb', line 34

def retrieve_column_comments(table_name, *column_names)
  result = select_rows(column_comment_sql(table_name, *column_names))
  return {} if result.nil?
  result.inject({}){|m, row| m[row[0].to_sym] = (row[1].blank? ? nil : row[1]); m}
end

#retrieve_table_comment(table_name) ⇒ Object



29
30
31
32
# File 'lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb', line 29

def retrieve_table_comment(table_name)
  result = select_rows(table_comment_sql(table_name))
  result[0].nil? || result[0][0].blank? ? nil : result[0][0]
end

#set_column_comment(table_name, column_name, comment_text) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb', line 18

def set_column_comment(table_name, column_name, comment_text)
  pk_info = pk_and_sequence_for(table_name)
  if pk_info && pk_info.first == column_name.to_s             # change_column broken for :primary_key
    primary_col_def = native_database_types[:primary_key].sub(/ PRIMARY KEY/i, '')
    execute "ALTER TABLE #{quote_table_name table_name} CHANGE #{quote_column_name column_name} #{quote_column_name column_name} #{primary_col_def} COMMENT #{escaped_comment(comment_text)};"
  else
    column = column_for(table_name, column_name)
    change_column table_name, column_name, column.sql_type, :comment => comment_text
  end
end

#set_table_comment(table_name, comment_text) ⇒ Object



14
15
16
# File 'lib/migration_comments/active_record/connection_adapters/mysql_adapter.rb', line 14

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