Sha256: 50f412f064a9d94af7d6ea1c1a00a0c1d1a11800c7be8a2e61bf5cdf18dbf6d3

Contents?: true

Size: 1.54 KB

Versions: 18

Compression:

Stored size: 1.54 KB

Contents

module Qa
  module Authorities
    module Local
      class MysqlTableBasedAuthority < Local::TableBasedAuthority
        self.table_index = "index_qa_local_authority_entries_on_lower_label_and_authority"

        def self.check_for_index
          if table_or_view_exists? && index_name_exists? # rubocop:disable Style/GuardClause
            Rails.logger.error "You've installed mysql local authority tables, but you haven't indexed the lower label. "
            "Rails doesn't support functional indexes in migrations, so we tried to execute it for you but something went wrong...\n" \
            "Make sure your table has a lower_label column, which is virtually created, and that the column is indexed." \
            " ALTER TABLE #{table_name} ADD lower_label VARCHAR(256) GENERATED ALWAYS AS (lower(label)) VIRTUAL" \
            " CREATE INDEX #{table_index} ON #{table_name} (local_authority_id, lower_label)"
          end
        end

        def search(q)
          return [] if q.blank?
          output_set(base_relation.where('lower_label like ?', "#{q.downcase}%").limit(25))
        end

        def self.index_name_exists?
          conn = ActiveRecord::Base.connection
          if (ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR >= 1) || ActiveRecord::VERSION::MAJOR >= 6
            conn.index_name_exists?(table_name, table_index).blank?
          else
            conn.index_name_exists?(table_name, table_index, :default).blank?
          end
        end
        private_class_method :index_name_exists?
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
qa-5.14.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.13.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.12.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.11.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.10.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.9.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.8.1 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.8.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.7.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.6.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.5.2 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.5.1 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.5.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.4.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.3.1 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.3.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.2.0 lib/qa/authorities/local/mysql_table_based_authority.rb
qa-5.1.0 lib/qa/authorities/local/mysql_table_based_authority.rb