From e1b0b65465eb1e3962ec719d17416ad017abf82b Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Tue, 29 Jun 2021 15:57:23 +0200 Subject: [PATCH] Improved code style, fixed bug [SCI-5828] --- app/models/concerns/searchable_model.rb | 12 +++++------ app/models/experiment.rb | 14 ++++++++++--- ...618104853_add_code_index_to_experiments.rb | 15 -------------- .../20210622101238_fix_experiment_indices.rb | 20 ++++++++----------- db/structure.sql | 1 - 5 files changed, 25 insertions(+), 37 deletions(-) delete mode 100644 db/migrate/20210618104853_add_code_index_to_experiments.rb diff --git a/app/models/concerns/searchable_model.rb b/app/models/concerns/searchable_model.rb index a9c5e9ee0..2502c2ce4 100644 --- a/app/models/concerns/searchable_model.rb +++ b/app/models/concerns/searchable_model.rb @@ -43,8 +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 " + elsif a == Experiment::EXPERIMENT_CODE_SQL + "#{Experiment::EXPERIMENT_CODE_SQL} #{like} :t#{i} OR " else col = options[:at_search].to_s == 'true' ? "lower(#{a})": a "(trim_html_tags(#{col})) #{like} :t#{i} OR " @@ -69,8 +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 " + elsif a == Experiment::EXPERIMENT_CODE_SQL + "#{Experiment::EXPERIMENT_CODE_SQL} #{like} ANY (array[:t#{i}]) OR " else "(trim_html_tags(#{a})) #{like} ANY (array[:t#{i}]) OR " end @@ -90,8 +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 " + elsif a == Experiment::EXPERIMENT_CODE_SQL + "#{Experiment::EXPERIMENT_CODE_SQL} #{like} :t#{i} OR " else "(trim_html_tags(#{a})) #{like} :t#{i} OR " end diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 7f636cc23..5ad42ff6e 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -3,6 +3,8 @@ class Experiment < ApplicationRecord include SearchableModel include SearchableByNameModel + EXPERIMENT_CODE_SQL = "('EX' || id)".freeze + before_save -> { report_elements.destroy_all }, if: -> { !new_record? && project_id_changed? } belongs_to :project, inverse_of: :experiments, touch: true @@ -70,19 +72,25 @@ class Experiment < ApplicationRecord new_query = Experiment .where('experiments.project_id IN (?)', projects_ids) - .where_attributes_like([:name, :description, "('EX' || id)"], query, options) + .where_attributes_like( + [:name, :description, EXPERIMENT_CODE_SQL], query, options + ) return include_archived ? new_query : new_query.active elsif include_archived new_query = Experiment .where(project: project_ids) - .where_attributes_like([:name, :description, "('EX' || id)"], query, options) + .where_attributes_like( + [:name, :description, EXPERIMENT_CODE_SQL], query, options + ) else new_query = Experiment .active .where(project: project_ids) - .where_attributes_like([:name, :description, "('EX' || id)"], query, options) + .where_attributes_like( + [:name, :description, EXPERIMENT_CODE_SQL], query, options + ) end # Show all results if needed diff --git a/db/migrate/20210618104853_add_code_index_to_experiments.rb b/db/migrate/20210618104853_add_code_index_to_experiments.rb deleted file mode 100644 index 844a132f4..000000000 --- a/db/migrate/20210618104853_add_code_index_to_experiments.rb +++ /dev/null @@ -1,15 +0,0 @@ -# frozen_string_literal: true - -class AddCodeIndexToExperiments < ActiveRecord::Migration[6.1] - 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 index e6ad4cb94..a633ccafd 100644 --- a/db/migrate/20210622101238_fix_experiment_indices.rb +++ b/db/migrate/20210622101238_fix_experiment_indices.rb @@ -1,27 +1,23 @@ # frozen_string_literal: true +require File.expand_path('app/helpers/database_helper') +include DatabaseHelper 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);' - ) + add_gin_index_without_tags(:experiments, :name) + add_gin_index_without_tags(:experiments, :description) ActiveRecord::Base.connection.execute( - 'CREATE INDEX index_experiments_on_description ON experiments ' \ - 'using gin (trim_html_tags(description) gin_trgm_ops);' + "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_name;' - ) - - ActiveRecord::Base.connection.execute( - 'DROP INDEX index_experiments_on_description;' - ) + remove_index :experiments, name: 'index_experiments_on_code' + remove_index :experiments, name: 'index_experiments_on_description', column: 'description' + remove_index :experiments, name: 'index_experiments_on_name', column: 'name' add_index :experiments, :name end diff --git a/db/structure.sql b/db/structure.sql index ffb12c013..8e83ef5dc 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -7250,7 +7250,6 @@ INSERT INTO "schema_migrations" (version) VALUES ('20210407143303'), ('20210410100006'), ('20210506125657'), -('20210618104853'), ('20210622101238');