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

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_index_options(table_name, column_name, name: nil, if_not_exists: false, internal: false, **options) ⇒ Object

:nodoc:



86
87
88
89
90
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 86

def add_index_options(table_name, column_name, name: nil, if_not_exists: false, internal: false, **options) # :nodoc:
  index, algorithm, if_not_exists = super
  index.enabled = options[:enabled] unless options[:enabled].nil?
  [index, algorithm, if_not_exists]
end

#create_index_definition(table_name, name, unique, columns, **options) ⇒ Object



82
83
84
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 82

def create_index_definition(table_name, name, unique, columns, **options)
  MySQL::IndexDefinition.new(table_name, name, unique, columns, **options)
end

#create_schema_dumper(options) ⇒ Object



122
123
124
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 122

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

#create_table(table_name, options: default_row_format) ⇒ Object



99
100
101
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 99

def create_table(table_name, options: default_row_format, **)
  super
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 8

def indexes(table_name)
  indexes = []
  current_index = nil
  internal_exec_query("SHOW KEYS FROM #{quote_table_name(table_name)}", "SCHEMA").each 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

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

      if supports_disabling_indexes?
        index[-1][:enabled] = mariadb? ? row["Ignored"] == "NO" : row["Visible"] == "YES"
      end

      indexes << index
    end

    if expression = row["Expression"]
      expression = expression.gsub("\\'", "'")
      expression = +"(#{expression})" unless expression.start_with?("(")
      indexes.last[-2] << expression
      indexes.last[-1][:expressions] ||= {}
      indexes.last[-1][:expressions][expression] = expression
      indexes.last[-1][:orders][expression] = :desc if row["Collation"] == "D"
    else
      indexes.last[-2] << row["Column_name"]
      indexes.last[-1][:lengths][row["Column_name"]] = row["Sub_part"].to_i if row["Sub_part"]
      indexes.last[-1][:orders][row["Column_name"]] = :desc if row["Collation"] == "D"
    end
  end

  indexes.map do |index|
    options = index.pop

    if expressions = options.delete(:expressions)
      orders = options.delete(:orders)
      lengths = options.delete(:lengths)

      columns = index[-1].to_h { |name|
        [ name.to_sym, expressions[name] || +quote_column_name(name) ]
      }

      index[-1] = add_options_for_index_columns(
        columns, order: orders, length: lengths
      ).values.join(", ")
    end
    MySQL::IndexDefinition.new(*index, **options)
  end
rescue StatementInvalid => e
  if e.message.match?(/Table '.+' doesn't exist/)
    []
  else
    raise
  end
end

#internal_string_options_for_primary_keyObject



110
111
112
113
114
115
116
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 110

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

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



92
93
94
95
96
97
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 92

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

#remove_foreign_key(from_table, to_table = nil, **options) ⇒ Object



103
104
105
106
107
108
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 103

def remove_foreign_key(from_table, to_table = nil, **options)
  # RESTRICT is by default in MySQL.
  options.delete(:on_update) if options[:on_update] == :restrict
  options.delete(:on_delete) if options[:on_delete] == :restrict
  super
end

#schema_creationObject

:nodoc:



154
155
156
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 154

def schema_creation # :nodoc:
  MySQL::SchemaCreation.new(self)
end

#table_alias_lengthObject



150
151
152
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 150

def table_alias_length
  256 # https://dev.mysql.com/doc/refman/en/identifiers.html
end

#type_to_sql(type, limit: nil, precision: nil, scale: nil, size: limit_to_size(limit, type), unsigned: nil) ⇒ Object

Maps logical Rails types to MySQL-specific data types.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 127

def type_to_sql(type, limit: nil, precision: nil, scale: nil, size: limit_to_size(limit, type), unsigned: nil, **)
  sql =
    case type.to_s
    when "integer"
      integer_to_sql(limit)
    when "text"
      type_with_size_to_sql("text", size)
    when "blob"
      type_with_size_to_sql("blob", size)
    when "binary"
      if (0..0xfff) === limit
        "varbinary(#{limit})"
      else
        type_with_size_to_sql("blob", size)
      end
    else
      super
    end

  sql = "#{sql} unsigned" if unsigned && type != :primary_key
  sql
end

#update_table_definition(table_name, base) ⇒ Object



118
119
120
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 118

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