Module: ActiveRecord::ConnectionAdapters::MySQL::SchemaStatements
Do not use. This module is for internal use only.
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| Defined in: | activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb |
Constant Summary
-
CHARSETS_OF_4BYTES_MAXLEN =
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 181["utf8mb4", "utf16", "utf16le", "utf32"].freeze
Instance Attribute Summary
- #row_format_dynamic_by_default? ⇒ Boolean readonly private
Instance Method Summary
- #add_index_options(table_name, column_name, name: nil, if_not_exists: false, internal: false, **options)
- #create_index_definition(table_name, name, unique, columns, **options)
- #create_schema_dumper(options)
- #create_table(table_name, options: default_row_format)
-
#indexes(table_name)
Returns an array of indexes for the given table.
- #internal_string_options_for_primary_key
- #remove_foreign_key(from_table, to_table = nil, **options)
- #schema_creation
- #table_alias_length
-
#type_to_sql(type, limit: nil, precision: nil, scale: nil, size: limit_to_size(limit, type), unsigned: nil)
Maps logical
::Railstypes to MySQL-specific data types. - #update_table_definition(table_name, base)
- #add_index_length(quoted_columns, **options) private
- #add_options_for_index_columns(quoted_columns, **options) private
- #bare_table_name(table) private
- #build_indexes(rows) private
- #create_alter_table(name) private
- #create_table_definition(name, **options) private
- #data_source_sql(name = nil, type: nil) private
- #default_row_format private
- #extract_foreign_key_action(specifier) private
- #extract_schema_qualified_name(string) private
- #fetch_indexes(tables) private
- #fetch_type_metadata(sql_type, extra = "") private
- #integer_to_sql(limit) private
- #limit_to_size(limit, type) private
- #new_column_from_field(table_name, field, _definitions) private
- #quoted_scope(name = nil, type: nil) private
- #type_with_size_to_sql(type, size) private
- #valid_index_options private
- #valid_primary_key_options private
Instance Attribute Details
#row_format_dynamic_by_default? ⇒ Boolean (readonly, private)
[ GitHub ]
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 183
def row_format_dynamic_by_default? if mariadb? database_version >= "10.2.2" else database_version >= "5.7.9" end end
Instance Method Details
#add_index_length(quoted_columns, **options) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 257
def add_index_length(quoted_columns, **) lengths = ([:length]) quoted_columns.each do |name, column| column << "(#{lengths[name]})" if lengths[name].present? end end
#add_index_options(table_name, column_name, name: nil, if_not_exists: false, internal: false, **options)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 17
def (table_name, column_name, name: nil, if_not_exists: false, internal: false, **) # :nodoc: index, algorithm, if_not_exists = super index.enabled = [:enabled] unless [:enabled].nil? [index, algorithm, if_not_exists] end
#add_options_for_index_columns(quoted_columns, **options) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 271
def (quoted_columns, **) quoted_columns = add_index_length(quoted_columns, **) super end
#bare_table_name(table) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 300
def (table) unquote_identifier(extract_schema_qualified_name(table).last) end
#build_indexes(rows) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 114
def build_indexes(rows) indexes = [] current_index = nil rows.each do |row| if current_index != row["Key_name"] current_index = row["Key_name"] mysql_index_type = row["Index_type"].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] = row["Enabled"] == "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| = index.pop if expressions = .delete(:expressions) orders = .delete(:orders) lengths = .delete(:lengths) columns = index[-1].to_h { |name| [ name.to_sym, expressions[name] || +quote_column_name(name) ] } index[-1] = ( columns, order: orders, length: lengths ).values.join(", ") end MySQL::IndexDefinition.new(*index, **) end end
#create_alter_table(name) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 213
def create_alter_table(name) MySQL::AlterTable.new create_table_definition(name) end
#create_index_definition(table_name, name, unique, columns, **options)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 13
def create_index_definition(table_name, name, unique, columns, **) MySQL::IndexDefinition.new(table_name, name, unique, columns, **) end
#create_schema_dumper(options)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 46
def create_schema_dumper() MySQL::SchemaDumper.create(self, ) end
#create_table(table_name, options: default_row_format)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 23
def create_table(table_name, options: default_row_format, **) super end
#create_table_definition(name, **options) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 209
def create_table_definition(name, **) MySQL::TableDefinition.new(self, name, **) end
#data_source_sql(name = nil, type: nil) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 276
def data_source_sql(name = nil, type: nil) scope = quoted_scope(name, type: type) sql = +"SELECT table_name FROM information_schema.tables" sql << " WHERE table_schema = #{scope[:schema]}" if scope[:name] sql << " AND table_name = #{scope[:name]}" sql << " AND table_name IN (SELECT table_name FROM information_schema.tables WHERE table_schema = #{scope[:schema]})" end sql << " AND table_type = #{scope[:type]}" if scope[:type] sql end
#default_row_format (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 191
def default_row_format return if row_format_dynamic_by_default? unless defined?(@default_row_format) if query_value("SELECT @@innodb_file_per_table = 1 AND @@innodb_file_format = 'Barracuda'") == 1 @default_row_format = "ROW_FORMAT=DYNAMIC" else @default_row_format = nil end end @default_row_format end
#extract_foreign_key_action(specifier) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 253
def extract_foreign_key_action(specifier) super unless specifier == "RESTRICT" end
#extract_schema_qualified_name(string) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 304
def extract_schema_qualified_name(string) schema, name = string.to_s.scan(/[^`.\s]+|`[^`]*`/) schema, name = nil, schema unless name [schema, name] end
#fetch_indexes(tables) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 83
def fetch_indexes(tables) # MariaDB has no EXPRESSION column, and neither it nor MySQL before # 8.0.13 reports index visibility the same way, so ask for what the # server has. optional_columns = +"" optional_columns << ", expression AS 'Expression'" if supports_expression_index? if supports_disabling_indexes? optional_columns << (mariadb? ? ", IF(ignored = 'NO', 'YES', 'NO') AS 'Enabled'" : ", is_visible AS 'Enabled'") end fetch_by_schema(tables) do |schema, group| by_name = query_all(<<~SQL).group_by { |row| row["Table"] } SELECT table_name AS 'Table', index_name AS 'Key_name', non_unique AS 'Non_unique', column_name AS 'Column_name', collation AS 'Collation', sub_part AS 'Sub_part', LOWER(index_type) AS 'Index_type', index_comment AS 'Index_comment'#{optional_columns} FROM information_schema.statistics WHERE table_schema = #{schema} AND table_name IN (#{quoted_table_names(group)}) AND index_name != 'PRIMARY' ORDER BY table_name, index_name, seq_in_index SQL group.index_with { |table| build_indexes(rows_for(by_name, table)) } end end
#fetch_type_metadata(sql_type, extra = "") (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 249
def (sql_type, extra = "") MySQL::TypeMetadata.new(super(sql_type), extra: extra) end
#indexes(table_name)
Returns an array of indexes for the given table.
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 8
def indexes(table_name) result = fetch_indexes(Array(table_name).map(&:to_s)) table_name.is_a?(Array) ? result : result[table_name.to_s] end
#integer_to_sql(limit) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 333
def integer_to_sql(limit) case limit when 1; "tinyint" when 2; "smallint" when 3; "mediumint" when nil, 4; "int" when 5..8; "bigint" else raise ArgumentError, "No integer type has byte size #{limit}. Use a decimal with scale 0 instead." end end
#internal_string_options_for_primary_key
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 34
def super.tap do || if !row_format_dynamic_by_default? && CHARSETS_OF_4BYTES_MAXLEN.include?(charset) [:collation] = collation.sub(/\A[^_]+/, "utf8") end end end
#limit_to_size(limit, type) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 320
def limit_to_size(limit, type) case type.to_s when "text", "blob", "binary" case limit when 0..0xff; "tiny" when nil, 0x100..0xffff; nil when 0x10000..0xffffff; "medium" when 0x1000000..0xffffffff; "long" else raise ArgumentError, "No #{type} type has byte size #{limit}" end end end
#new_column_from_field(table_name, field, _definitions) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 217
def new_column_from_field(table_name, field, _definitions) = (field["Type"], field["Extra"]) default, default_function = field["Default"], nil if .type == :datetime && /\ACURRENT_TIMESTAMP(?:\([0-6]?\))?\z/i.match?(default) default = "#{default} ON UPDATE #{default}" if /on update CURRENT_TIMESTAMP/i.match?(field["Extra"]) default, default_function = nil, default elsif .extra == "DEFAULT_GENERATED" if mariadb? default, default_function = nil, default else default = "(#{default})" unless default.start_with?("(") default = default.gsub("\\'", "'") default, default_function = nil, default end elsif .type == :text && default&.start_with?("'") # strip and unescape quotes default = default[1...-1].gsub("\\'", "'") end MySQL::Column.new( field["Field"], lookup_cast_type(.sql_type), default, , field["Null"] == "YES", default_function, collation: field["Collation"], comment: field["Comment"].presence ) end
#quoted_scope(name = nil, type: nil) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 291
def quoted_scope(name = nil, type: nil) schema, name = extract_schema_qualified_name(name) scope = {} scope[:schema] = schema ? quote(schema) : "database()" scope[:name] = quote(name) if name scope[:type] = quote(type) if type scope end
#remove_foreign_key(from_table, to_table = nil, **options)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 27
def remove_foreign_key(from_table, to_table = nil, **) # RESTRICT is by default in MySQL. .delete(:on_update) if [:on_update] == :restrict .delete(:on_delete) if [:on_delete] == :restrict super end
#schema_creation
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 78
def schema_creation # :nodoc: MySQL::SchemaCreation.new(self) end
#table_alias_length
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 74
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)
Maps logical ::Rails types to MySQL-specific data types.
# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 51
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
#type_with_size_to_sql(type, size) (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 310
def type_with_size_to_sql(type, size) case size&.to_s when nil, "tiny", "medium", "long" "#{size}#{type}" else raise ArgumentError, "#{size.inspect} is invalid :size value. Only :tiny, :medium, and :long are allowed." end end
#update_table_definition(table_name, base)
[ GitHub ]#valid_index_options (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 264
def = super << :enabled if supports_disabling_indexes? << :lock end
#valid_primary_key_options (private)
[ GitHub ]# File 'activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb', line 205
def super + [:unsigned, :auto_increment] end