summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2016-03-03 12:55:44 +0100
committerRobert Speicher <rspeicher@gmail.com>2016-03-11 15:25:22 -0500
commitd38dcf1e3b08608aab15e9dd811f9fc878751ce5 (patch)
tree0acd5304d4da798d9dddbced42f5fa346f293c6a
parent2080fd395c98e1f5c822450b6dc9e8638b4e0329 (diff)
downloadgitlab-ce-d38dcf1e3b08608aab15e9dd811f9fc878751ce5.tar.gz
Patch MySQL to ignore PostgreSQL schema options
This ensures that options such as `using: :gin` and PostgreSQL operator classes are ignored when loading a schema into a MySQL database.
-rw-r--r--config/initializers/mysql_ignore_postgresql_options.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/config/initializers/mysql_ignore_postgresql_options.rb b/config/initializers/mysql_ignore_postgresql_options.rb
new file mode 100644
index 00000000000..835f3ec5574
--- /dev/null
+++ b/config/initializers/mysql_ignore_postgresql_options.rb
@@ -0,0 +1,49 @@
+# This patches ActiveRecord so indexes created using the MySQL adapter ignore
+# any PostgreSQL specific options (e.g. `using: :gin`).
+#
+# These patches do the following for MySQL:
+#
+# 1. Indexes created using the :opclasses option are ignored (as they serve no
+# purpose on MySQL).
+# 2. When creating an index with `using: :gin` the `using` option is discarded
+# as :gin is not a valid value for MySQL.
+# 3. The `:opclasses` option is stripped from add_index_options in case it's
+# used anywhere other than in the add_index methods.
+
+if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
+ module ActiveRecord
+ module ConnectionAdapters
+ class Mysql2Adapter < AbstractMysqlAdapter
+ alias_method :__gitlab_add_index, :add_index
+ alias_method :__gitlab_add_index_sql, :add_index_sql
+ alias_method :__gitlab_add_index_options, :add_index_options
+
+ def add_index(table_name, column_name, options = {})
+ unless options[:opclasses]
+ __gitlab_add_index(table_name, column_name, options)
+ end
+ end
+
+ def add_index_sql(table_name, column_name, options = {})
+ unless options[:opclasses]
+ __gitlab_add_index_sql(table_name, column_name, options)
+ end
+ end
+
+ def add_index_options(table_name, column_name, options = {})
+ if options[:using] and options[:using] == :gin
+ options = options.dup
+ options.delete(:using)
+ end
+
+ if options[:opclasses]
+ options = options.dup
+ options.delete(:opclasses)
+ end
+
+ __gitlab_add_index_options(table_name, column_name, options)
+ end
+ end
+ end
+ end
+end