debian-mirror-gitlab/config/initializers/mysql_set_length_for_binary_indexes.rb

31 lines
1.1 KiB
Ruby
Raw Normal View History

2017-09-10 17:25:29 +05:30
# This patches ActiveRecord so indexes for binary columns created using the
# MySQL adapter apply a length of 20. Otherwise MySQL can't create an index on
# binary columns.
2019-02-15 15:39:39 +05:30
module MysqlSetLengthForBinaryIndexAndIgnorePostgresOptionsForSchema
# This method is used in Rails 5 schema loading as t.index
def index(column_names, options = {})
# Ignore indexes that use opclasses,
# also see config/initializers/mysql_ignore_postgresql_options.rb
if options[:opclasses]
warn "WARNING: index on columns #{column_names} uses unsupported option, skipping."
return
end
2018-11-20 20:47:30 +05:30
options[:length] ||= {}
2017-09-10 17:25:29 +05:30
Array(column_names).each do |column_name|
2019-02-15 15:39:39 +05:30
column = columns.find { |c| c.name == column_name }
2017-09-10 17:25:29 +05:30
if column&.type == :binary
2018-11-20 20:47:30 +05:30
options[:length][column_name] = 20
2017-09-10 17:25:29 +05:30
end
end
2019-02-15 15:39:39 +05:30
super(column_names, options)
2017-09-10 17:25:29 +05:30
end
end
2019-02-15 15:39:39 +05:30
if defined?(ActiveRecord::ConnectionAdapters::MySQL::TableDefinition)
ActiveRecord::ConnectionAdapters::MySQL::TableDefinition.send(:prepend, MysqlSetLengthForBinaryIndexAndIgnorePostgresOptionsForSchema)
2018-11-20 20:47:30 +05:30
end