Browse Source

Follow up 1c2c02921ed5ba632b7a2459da52e643984b7172 - Fixes #4148 - Add bugfix for older mariadb versions.

Rolf Schmidt 2 years ago
parent
commit
205f9ec322
2 changed files with 12 additions and 0 deletions
  1. 1 0
      .rubocop/todo.yml
  2. 11 0
      config/initializers/mariadb_json_columns.rb

+ 1 - 0
.rubocop/todo.yml

@@ -353,6 +353,7 @@ Metrics/AbcSize:
 Metrics/BlockLength:
   Exclude:
     - 'Gemfile'
+    - 'config/initializers/mariadb_json_columns.rb'
     - 'app/controllers/application_controller/authenticates.rb'
     - 'app/controllers/reports_controller.rb'
     - 'app/controllers/time_accountings_controller.rb'

+ 11 - 0
config/initializers/mariadb_json_columns.rb

@@ -25,9 +25,20 @@ ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.class_eval do
   # the constraint checks to find out if the column was created as json.
   # Based on this detection we will hack the type so rails will handle
   # values properly as json values.
+  # INFORMATION_SCHEMA.CHECK_CONSTRAINTS is only support on > 10.4 mariadb
+  # so we use object manager attributes table now to detect them.
   def mariadb_column_json?(table_name, field_name)
     field = quote(field_name)
     scope = quoted_scope(table_name)
+
+    # for older versions
+    if database_version < '10.4' && %w[tickets users groups organizations].include?(table_name)
+      class_name = table_name.classify
+      execute_and_free("SELECT 1 FROM object_manager_attributes, object_lookups WHERE object_manager_attributes.object_lookup_id = object_lookups.id AND object_lookups.name = '#{class_name}' AND object_manager_attributes.name = #{field} AND object_manager_attributes.data_type IN ('multiselect', 'multi_tree_select') LIMIT 1") do |r|
+        return r.to_a.present?
+      end
+    end
+
     execute_and_free("SELECT 1 FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE TABLE_NAME = #{scope[:name]} AND CONSTRAINT_SCHEMA = #{scope[:schema]} AND CONSTRAINT_NAME = #{field} AND CHECK_CLAUSE LIKE '%json_valid%'") do |r|
       return r.to_a.present?
     end