From eaa713f5b8f3b736751d0a060d0d9c6128a949b9 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Tue, 22 Jun 2021 12:51:25 +0200 Subject: [PATCH] Fix indices [SCI-5829] --- app/models/concerns/searchable_model.rb | 6 +++++ ...618104853_add_code_index_to_experiments.rb | 12 +++++++-- .../20210622101238_fix_experiment_indices.rb | 26 +++++++++++++++++++ db/structure.sql | 14 +++++++--- 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20210622101238_fix_experiment_indices.rb diff --git a/app/models/concerns/searchable_model.rb b/app/models/concerns/searchable_model.rb index f01c9e620..a9c5e9ee0 100644 --- a/app/models/concerns/searchable_model.rb +++ b/app/models/concerns/searchable_model.rb @@ -43,6 +43,8 @@ module SearchableModel (attrs.map.with_index do |a, i| if %w(repository_rows.id repository_number_values.data).include?(a) "((#{a})::text) #{like} :t#{i} OR " + elsif a == "('EX' || id)" + "('EX' || id) #{like} :t#{i} OR " else col = options[:at_search].to_s == 'true' ? "lower(#{a})": a "(trim_html_tags(#{col})) #{like} :t#{i} OR " @@ -67,6 +69,8 @@ module SearchableModel (attrs.map.with_index do |a, i| if %w(repository_rows.id repository_number_values.data).include?(a) "((#{a})::text) #{like} ANY (array[:t#{i}]) OR " + elsif a == "('EX' || id)" + "('EX' || id) #{like} :t#{i} OR " else "(trim_html_tags(#{a})) #{like} ANY (array[:t#{i}]) OR " end @@ -86,6 +90,8 @@ module SearchableModel (attrs.map.with_index do |a, i| if %w(repository_rows.id repository_number_values.data).include?(a) "((#{a})::text) #{like} :t#{i} OR " + elsif a == "('EX' || id)" + "('EX' || id) #{like} :t#{i} OR " else "(trim_html_tags(#{a})) #{like} :t#{i} OR " end diff --git a/db/migrate/20210618104853_add_code_index_to_experiments.rb b/db/migrate/20210618104853_add_code_index_to_experiments.rb index d046270ab..844a132f4 100644 --- a/db/migrate/20210618104853_add_code_index_to_experiments.rb +++ b/db/migrate/20210618104853_add_code_index_to_experiments.rb @@ -1,7 +1,15 @@ # frozen_string_literal: true class AddCodeIndexToExperiments < ActiveRecord::Migration[6.1] - def change - add_index :experiments, "('EX' || id)", name: 'index_experiments_on_experiment_code', unique: true + def up + ActiveRecord::Base.connection.execute( + "CREATE INDEX index_experiments_on_experiment_code ON experiments using gin (('EX'::text || id) gin_trgm_ops);" + ) + end + + def down + ActiveRecord::Base.connection.execute( + 'DROP INDEX index_experiments_on_experiment_code;' + ) end end diff --git a/db/migrate/20210622101238_fix_experiment_indices.rb b/db/migrate/20210622101238_fix_experiment_indices.rb new file mode 100644 index 000000000..9111aa61b --- /dev/null +++ b/db/migrate/20210622101238_fix_experiment_indices.rb @@ -0,0 +1,26 @@ +class FixExperimentIndices < ActiveRecord::Migration[6.1] + def up + remove_index :experiments, name: 'index_experiments_on_name', column: 'name' + + + ActiveRecord::Base.connection.execute( + "CREATE INDEX index_experiments_on_name ON experiments using gin (trim_html_tags(name) gin_trgm_ops);" + ) + + ActiveRecord::Base.connection.execute( + "CREATE INDEX index_experiments_on_description ON experiments using gin (trim_html_tags(description) gin_trgm_ops);" + ) + end + + def down + ActiveRecord::Base.connection.execute( + 'DROP INDEX index_experiments_on_name;' + ) + + ActiveRecord::Base.connection.execute( + 'DROP INDEX index_experiments_on_description;' + ) + + add_index :experiments, :name + end +end diff --git a/db/structure.sql b/db/structure.sql index 5160ca820..ffb12c013 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -4349,11 +4349,18 @@ CREATE INDEX index_experiments_on_archived_by_id ON public.experiments USING btr CREATE INDEX index_experiments_on_created_by_id ON public.experiments USING btree (created_by_id); +-- +-- Name: index_experiments_on_description; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_experiments_on_description ON public.experiments USING gin (public.trim_html_tags(description) public.gin_trgm_ops); + + -- -- Name: index_experiments_on_experiment_code; Type: INDEX; Schema: public; Owner: - -- -CREATE UNIQUE INDEX index_experiments_on_experiment_code ON public.experiments USING btree ((('EX'::text || id))); +CREATE INDEX index_experiments_on_experiment_code ON public.experiments USING gin ((('EX'::text || id)) public.gin_trgm_ops); -- @@ -4367,7 +4374,7 @@ CREATE INDEX index_experiments_on_last_modified_by_id ON public.experiments USIN -- Name: index_experiments_on_name; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_experiments_on_name ON public.experiments USING btree (name); +CREATE INDEX index_experiments_on_name ON public.experiments USING gin (public.trim_html_tags((name)::text) public.gin_trgm_ops); -- @@ -7243,6 +7250,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20210407143303'), ('20210410100006'), ('20210506125657'), -('20210618104853'); +('20210618104853'), +('20210622101238');