Module: ActiveRecord::ConnectionAdapters::MySQL::SchemaStatements

Included in:
AbstractMysqlAdapter
Defined in:
lib/active_record/connection_adapters/mysql/schema_statements.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#create_schema_dumper(options) ⇒ Object



66
67
68
# File 'lib/active_record/connection_adapters/mysql/schema_statements.rb', line 66

def create_schema_dumper(options)
  MySQL::SchemaDumper.create(self, options)
end

#indexes(table_name) ⇒ Object

Returns an array of indexes for the given table.



8
9
10
11
12
13
14
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
# File 'lib/active_record/connection_adapters/mysql/schema_statements.rb', line 8

def indexes(table_name)
  indexes = []
  current_index = nil
  execute_and_free("SHOW KEYS FROM #{quote_table_name(table_name)}", "SCHEMA") do |result|
    each_hash(result) do |row|
      if current_index != row[:Key_name]
        next if row[:Key_name] == "PRIMARY" # skip the primary key
        current_index = row[:Key_name]

        mysql_index_type = row[:Index_type].downcase.to_sym
        case mysql_index_type
        when :fulltext, :spatial
          index_type = mysql_index_type
        when :btree, :hash
          index_using = mysql_index_type
        end

        indexes << [
          row[:Table],
          row[:Key_name],
          row[:Non_unique].to_i == 0,
          [],
          lengths: {},
          orders: {},
          type: index_type,
          using: index_using,
          comment: row[:Index_comment].presence
        ]
      end

      indexes.last[-2] << row[:Column_name]
      indexes.last[-1][:lengths].merge!(row[:Column_name] => row[:Sub_part].to_i) if row[:Sub_part]
      indexes.last[-1][:orders].merge!(row[:Column_name] => :desc) if row[:Collation] == "D"
    end
  end

  indexes.map { |index| IndexDefinition.new(*index) }
end

#internal_string_options_for_primary_keyObject



54
55
56
57
58
59
60
# File 'lib/active_record/connection_adapters/mysql/schema_statements.rb', line 54

def internal_string_options_for_primary_key
  super.tap do |options|
    if CHARSETS_OF_4BYTES_MAXLEN.include?(charset) && (mariadb? || version < "8.0.0")
      options[:collation] = collation.sub(/\A[^_]+/, "utf8")
    end
  end
end

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



47
48
49
50
51
52
# File 'lib/active_record/connection_adapters/mysql/schema_statements.rb', line 47

def remove_column(table_name, column_name, type = nil, options = {})
  if foreign_key_exists?(table_name, column: column_name)
    remove_foreign_key(table_name, column: column_name)
  end
  super
end

#update_table_definition(table_name, base) ⇒ Object



62
63
64
# File 'lib/active_record/connection_adapters/mysql/schema_statements.rb', line 62

def update_table_definition(table_name, base)
  MySQL::Table.new(table_name, base)
end