2017-07-25 08:02:13 +00:00
|
|
|
# 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.
|
|
|
|
|
2018-09-03 16:28:17 +00:00
|
|
|
# This module can be removed once a Rails 5 schema is used.
|
|
|
|
# It can't be wrapped in a check that checks Gitlab.rails5? because
|
|
|
|
# the old Rails 4 schema layout is still used
|
2017-07-25 14:45:13 +00:00
|
|
|
module MysqlSetLengthForBinaryIndex
|
|
|
|
def add_index(table_name, column_names, options = {})
|
2018-09-04 22:52:03 +00:00
|
|
|
options[:length] ||= {}
|
2017-07-25 14:45:13 +00:00
|
|
|
Array(column_names).each do |column_name|
|
|
|
|
column = ActiveRecord::Base.connection.columns(table_name).find { |c| c.name == column_name }
|
2017-07-25 08:02:13 +00:00
|
|
|
|
2017-07-25 14:45:13 +00:00
|
|
|
if column&.type == :binary
|
2018-09-04 22:52:03 +00:00
|
|
|
options[:length][column_name] = 20
|
2017-07-25 08:02:13 +00:00
|
|
|
end
|
|
|
|
end
|
2017-07-25 14:45:13 +00:00
|
|
|
|
|
|
|
super(table_name, column_names, options)
|
2017-07-25 08:02:13 +00:00
|
|
|
end
|
|
|
|
end
|
2017-07-25 14:45:13 +00:00
|
|
|
|
|
|
|
if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
|
|
|
|
ActiveRecord::ConnectionAdapters::Mysql2Adapter.send(:prepend, MysqlSetLengthForBinaryIndex)
|
|
|
|
end
|
2018-09-03 16:28:17 +00:00
|
|
|
|
|
|
|
if Gitlab.rails5?
|
|
|
|
module MysqlSetLengthForBinaryIndexAndIgnorePostgresOptionsForSchema
|
|
|
|
# This method is used in Rails 5 schema loading as t.index
|
|
|
|
def index(column_names, options = {})
|
2018-09-04 22:52:03 +00:00
|
|
|
options[:length] ||= {}
|
2018-09-03 16:28:17 +00:00
|
|
|
Array(column_names).each do |column_name|
|
|
|
|
column = columns.find { |c| c.name == column_name }
|
|
|
|
|
|
|
|
if column&.type == :binary
|
2018-09-04 22:52:03 +00:00
|
|
|
options[:length][column_name] = 20
|
2018-09-03 16:28:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Ignore indexes that use opclasses,
|
|
|
|
# also see config/initializers/mysql_ignore_postgresql_options.rb
|
|
|
|
unless options[:opclasses]
|
|
|
|
super(column_names, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if defined?(ActiveRecord::ConnectionAdapters::MySQL::TableDefinition)
|
|
|
|
ActiveRecord::ConnectionAdapters::MySQL::TableDefinition.send(:prepend, MysqlSetLengthForBinaryIndexAndIgnorePostgresOptionsForSchema)
|
|
|
|
end
|
|
|
|
end
|