diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-09-04 11:38:03 +0000 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-09-04 11:38:03 +0000 |
commit | f7c1a5e1ed7003f9c35e9f81b94789562f968f57 (patch) | |
tree | 9f6f7327baa01f6acc9bd050df00f40651167289 | |
parent | fcd1c94ab8b1dc35c35a4ea856926ea6fa12d936 (diff) | |
parent | ba914c32e1a8cd35f17e42df5ab4c7730c617a23 (diff) | |
download | gitlab-ce-f7c1a5e1ed7003f9c35e9f81b94789562f968f57.tar.gz |
Merge branch 'rails5-mysql-binary-column-index-length' into 'master'
Rails 5: support schema t.index for mysql
See merge request gitlab-org/gitlab-ce!21485
-rw-r--r-- | changelogs/unreleased/rails5-mysql-binary-column-index-length.yml | 5 | ||||
-rw-r--r-- | config/initializers/mysql_set_length_for_binary_indexes.rb | 28 |
2 files changed, 33 insertions, 0 deletions
diff --git a/changelogs/unreleased/rails5-mysql-binary-column-index-length.yml b/changelogs/unreleased/rails5-mysql-binary-column-index-length.yml new file mode 100644 index 00000000000..c4eb0ddac4c --- /dev/null +++ b/changelogs/unreleased/rails5-mysql-binary-column-index-length.yml @@ -0,0 +1,5 @@ +--- +title: 'Rails 5: support schema t.index for mysql' +merge_request: 21485 +author: Jasper Maes +type: other diff --git a/config/initializers/mysql_set_length_for_binary_indexes.rb b/config/initializers/mysql_set_length_for_binary_indexes.rb index de0bc5322aa..1b16b39d517 100644 --- a/config/initializers/mysql_set_length_for_binary_indexes.rb +++ b/config/initializers/mysql_set_length_for_binary_indexes.rb @@ -2,6 +2,9 @@ # MySQL adapter apply a length of 20. Otherwise MySQL can't create an index on # binary columns. +# 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 module MysqlSetLengthForBinaryIndex def add_index(table_name, column_names, options = {}) Array(column_names).each do |column_name| @@ -19,3 +22,28 @@ end if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) ActiveRecord::ConnectionAdapters::Mysql2Adapter.send(:prepend, MysqlSetLengthForBinaryIndex) end + +if Gitlab.rails5? + module MysqlSetLengthForBinaryIndexAndIgnorePostgresOptionsForSchema + # This method is used in Rails 5 schema loading as t.index + def index(column_names, options = {}) + Array(column_names).each do |column_name| + column = columns.find { |c| c.name == column_name } + + if column&.type == :binary + options[:length] = 20 + 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 |