From 0866a2e251dee705c5003571a64f8d717d183b9a Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Fri, 18 Jun 2021 11:58:03 +0200 Subject: [PATCH 01/10] Added experiment ID to card and table views [SCI-5828] --- app/assets/stylesheets/experiments.scss | 24 ++++++++++++------- .../shared_styles/elements/dropdown.scss | 16 +++++++++++-- app/models/experiment.rb | 4 ++++ app/views/projects/show.html.erb | 1 + .../_experiment_actions_dropdown.html.erb | 8 +++++++ .../projects/show/_experiment_card.html.erb | 13 ++++++---- config/locales/en.yml | 2 ++ 7 files changed, 53 insertions(+), 15 deletions(-) diff --git a/app/assets/stylesheets/experiments.scss b/app/assets/stylesheets/experiments.scss index 17c3889e6..43bd32618 100644 --- a/app/assets/stylesheets/experiments.scss +++ b/app/assets/stylesheets/experiments.scss @@ -47,11 +47,14 @@ .cards-wrapper { --card-min-width: 350px; - --list-columns-number: 7; + --list-columns-number: 8; .card { - grid-row: span 6; + grid-row: span 7; + .experiment-code-cell { + display: none; + } &.experiment-card { border-radius: 4px; @@ -174,7 +177,7 @@ &.list { grid-auto-rows: 1px 5em; - grid-template-columns: max-content repeat(calc(var(--list-columns-number) - 2), minmax(100px, auto)) max-content; + grid-template-columns: max-content minmax(100px, auto) minmax(80px, max-content) repeat(calc(var(--list-columns-number) - 4), minmax(100px, auto)) max-content; grid-template-rows: 3em; .card { @@ -230,20 +233,25 @@ } } - .start-date-cell { + .experiment-code-cell { + display: block; grid-column: 3; } - .modified-date-cell { + .start-date-cell { grid-column: 4; } - .completed-task-cell { + .modified-date-cell { grid-column: 5; } - .description-cell { + .completed-task-cell { grid-column: 6; + } + + .description-cell { + grid-column: 7; position: relative; .description-text { @@ -262,7 +270,7 @@ } .actions-cell { - grid-column: 7; + grid-column: 8; padding-top: 3px; position: initial; } diff --git a/app/assets/stylesheets/shared_styles/elements/dropdown.scss b/app/assets/stylesheets/shared_styles/elements/dropdown.scss index c76aa898a..3d767f2e0 100644 --- a/app/assets/stylesheets/shared_styles/elements/dropdown.scss +++ b/app/assets/stylesheets/shared_styles/elements/dropdown.scss @@ -40,17 +40,29 @@ } .dropdown-menu { + .form-dropdown-break hr { + margin-top: 0; + margin-bottom: 8px; + } + .form-dropdown-item { padding: 0 !important; - button { + button, .form-dropdown-item-info { border-radius: 0; - color: $color-black !important; padding-left: .9em; text-align: left; width: 100%; } + button { + color: $color-black !important; + } + + .form-dropdown-item-info { + color: $color-silver-chalice !important; + } + .project-archive-restore-form { .button-to { &:hover { diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 2f5a884b6..c0a44e5d1 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -99,6 +99,10 @@ class Experiment < ApplicationRecord where(project: Project.viewable_by_user(user, teams)) end + def code + "EX#{id}" + end + def archived_branch? archived? || project.archived? end diff --git a/app/views/projects/show.html.erb b/app/views/projects/show.html.erb index 7651dd66e..a53f43e3b 100644 --- a/app/views/projects/show.html.erb +++ b/app/views/projects/show.html.erb @@ -25,6 +25,7 @@ <% end %>
<%= t('experiments.card.name') %>
+
<%= t('experiments.id') %>
<%= t('experiments.card.start_date') %>
<%= t('experiments.card.modified_date') %>
<%= t('experiments.card.archived_date') %>
diff --git a/app/views/projects/show/_experiment_actions_dropdown.html.erb b/app/views/projects/show/_experiment_actions_dropdown.html.erb index f57add79e..6ba1e488f 100644 --- a/app/views/projects/show/_experiment_actions_dropdown.html.erb +++ b/app/views/projects/show/_experiment_actions_dropdown.html.erb @@ -63,6 +63,14 @@ <% end %> <% end %> +
  • +
    +
  • +
  • +
    + <%= "#{t('experiments.experiment_id')}: #{experiment.code}" %> +
    +
  • <% end %> diff --git a/app/views/projects/show/_experiment_card.html.erb b/app/views/projects/show/_experiment_card.html.erb index 83de8c23a..8383b4f4f 100644 --- a/app/views/projects/show/_experiment_card.html.erb +++ b/app/views/projects/show/_experiment_card.html.erb @@ -25,11 +25,14 @@ <%= link_to experiment.name, canvas_experiment_path(experiment), title: experiment.name, class: 'name-link' %> <% end %> -
    - <% if can_manage_experiments?(experiment.project) %> - <%= render partial: 'projects/show/experiment_actions_dropdown.html.erb', locals: { experiment: experiment } %> - <% end %> -
    +
    + <%= experiment.code %> +
    +
    + <% if can_manage_experiments?(experiment.project) %> + <%= render partial: 'projects/show/experiment_actions_dropdown.html.erb', locals: { experiment: experiment } %> + <% end %> +
    diff --git a/config/locales/en.yml b/config/locales/en.yml index c31c7455f..c3cd176d8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1001,6 +1001,8 @@ en: body_html: This inventory has been unshared with your team by the inventory’s owner. To view the item/s that are assigned to your task/s contact the %{team_name} team administrator %{admin_name} (%{admin_email}). open_mobile_app: "Open mobile app" experiments: + id: "ID" + experiment_id: "Experiment ID" header: cards: "Cards" table: "Table" From 29ff910eba26ed13e7d9b0272c5131d26e41fcd8 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Fri, 18 Jun 2021 13:24:17 +0200 Subject: [PATCH 02/10] Added searching experiments by EX[ID] [SCI-5829] --- app/models/experiment.rb | 6 +++--- .../20210618104853_add_code_index_to_experiments.rb | 5 +++++ db/structure.sql | 10 +++++++++- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20210618104853_add_code_index_to_experiments.rb diff --git a/app/models/experiment.rb b/app/models/experiment.rb index c0a44e5d1..7f636cc23 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -70,19 +70,19 @@ class Experiment < ApplicationRecord new_query = Experiment .where('experiments.project_id IN (?)', projects_ids) - .where_attributes_like([:name, :description], query, options) + .where_attributes_like([:name, :description, "('EX' || id)"], 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], query, options) + .where_attributes_like([:name, :description, "('EX' || id)"], query, options) else new_query = Experiment .active .where(project: project_ids) - .where_attributes_like([:name, :description], query, options) + .where_attributes_like([:name, :description, "('EX' || id)"], 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 new file mode 100644 index 000000000..54a9ce638 --- /dev/null +++ b/db/migrate/20210618104853_add_code_index_to_experiments.rb @@ -0,0 +1,5 @@ +class AddCodeIndexToExperiments < ActiveRecord::Migration[6.1] + def change + add_index :experiments, "('EX' || id)", name: "index_experiments_on_experiment_code", unique: true + end +end diff --git a/db/structure.sql b/db/structure.sql index 4fa9d13bb..5160ca820 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -4349,6 +4349,13 @@ 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_experiment_code; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX index_experiments_on_experiment_code ON public.experiments USING btree ((('EX'::text || id))); + + -- -- Name: index_experiments_on_last_modified_by_id; Type: INDEX; Schema: public; Owner: - -- @@ -7235,6 +7242,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20210325152257'), ('20210407143303'), ('20210410100006'), -('20210506125657'); +('20210506125657'), +('20210618104853'); From afc1b2d8b69ba6a7e499975689dda6099e71fd32 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Fri, 18 Jun 2021 14:11:54 +0200 Subject: [PATCH 03/10] Added experiment code to PDF and DOCX [SCI-5830] --- app/services/reports/docx/draw_experiment.rb | 2 +- app/views/reports/elements/_experiment_element.html.erb | 2 +- config/locales/en.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/services/reports/docx/draw_experiment.rb b/app/services/reports/docx/draw_experiment.rb index de91d5c3a..cc9a671b5 100644 --- a/app/services/reports/docx/draw_experiment.rb +++ b/app/services/reports/docx/draw_experiment.rb @@ -11,7 +11,7 @@ module Reports::Docx::DrawExperiment @docx.h2 experiment.name, size: Constants::REPORT_DOCX_EXPERIMENT_TITLE_SIZE @docx.p do text I18n.t('projects.reports.elements.experiment.user_time', - timestamp: I18n.l(experiment.created_at, format: :full)), color: color[:gray] + code: experiment.code, timestamp: I18n.l(experiment.created_at, format: :full)), color: color[:gray] if experiment.archived? text ' | ' text I18n.t('search.index.archived'), color: color[:gray] diff --git a/app/views/reports/elements/_experiment_element.html.erb b/app/views/reports/elements/_experiment_element.html.erb index f75635068..dc90adf49 100644 --- a/app/views/reports/elements/_experiment_element.html.erb +++ b/app/views/reports/elements/_experiment_element.html.erb @@ -5,7 +5,7 @@
    - <%= t('projects.reports.elements.experiment.user_time', timestamp: l(timestamp, format: :full)) %> + <%= t('projects.reports.elements.experiment.user_time', code: experiment.code, timestamp: l(timestamp, format: :full)) %> <%= link_to t('projects.reports.elements.all.scinote_link'),canvas_experiment_url(experiment), target: :_blank %>
    diff --git a/config/locales/en.yml b/config/locales/en.yml index c3cd176d8..3b6214450 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -677,7 +677,7 @@ en: protocol: user_time: "Protocol created on %{timestamp}." experiment: - user_time: "Experiment created on %{timestamp}." + user_time: "Experiment %{code} created on %{timestamp}." no_description: "No description" no_tags: "No tags" module_activity: From a9d6c4bb94fce527778cf68e41951a345e106242 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Fri, 18 Jun 2021 14:19:35 +0200 Subject: [PATCH 04/10] Code style fixes [SCI-5828] --- app/assets/stylesheets/shared_styles/elements/dropdown.scss | 5 +++-- db/migrate/20210618104853_add_code_index_to_experiments.rb | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/assets/stylesheets/shared_styles/elements/dropdown.scss b/app/assets/stylesheets/shared_styles/elements/dropdown.scss index 3d767f2e0..4be60fc47 100644 --- a/app/assets/stylesheets/shared_styles/elements/dropdown.scss +++ b/app/assets/stylesheets/shared_styles/elements/dropdown.scss @@ -41,14 +41,15 @@ .dropdown-menu { .form-dropdown-break hr { - margin-top: 0; margin-bottom: 8px; + margin-top: 0; } .form-dropdown-item { padding: 0 !important; - button, .form-dropdown-item-info { + button, + .form-dropdown-item-info { border-radius: 0; padding-left: .9em; text-align: left; diff --git a/db/migrate/20210618104853_add_code_index_to_experiments.rb b/db/migrate/20210618104853_add_code_index_to_experiments.rb index 54a9ce638..d046270ab 100644 --- a/db/migrate/20210618104853_add_code_index_to_experiments.rb +++ b/db/migrate/20210618104853_add_code_index_to_experiments.rb @@ -1,5 +1,7 @@ +# 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 + add_index :experiments, "('EX' || id)", name: 'index_experiments_on_experiment_code', unique: true end end From 879cf5081d5afa13d1d96f3a24d5b5d9b690a0a5 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Fri, 18 Jun 2021 14:38:07 +0200 Subject: [PATCH 05/10] Added experiment code to experiment cards filter [SCI-5829] --- app/services/experiments_overview_service.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/services/experiments_overview_service.rb b/app/services/experiments_overview_service.rb index 98f9ae8b7..64a47037b 100644 --- a/app/services/experiments_overview_service.rb +++ b/app/services/experiments_overview_service.rb @@ -49,7 +49,10 @@ class ExperimentsOverviewService records = records.archived if @view_mode == 'archived' && @project.active? records = records.active if @view_mode == 'active' if @params[:search].present? - records = records.where_attributes_like(%w(experiments.name experiments.description), @params[:search]) + records = records.where_attributes_like( + ["experiments.name", "experiments.description", "('EX' || experiments.id)"], + @params[:search] + ) end if @params[:created_on_from].present? records = records.where('experiments.created_at > ?', @params[:created_on_from]) From eaa713f5b8f3b736751d0a060d0d9c6128a949b9 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Tue, 22 Jun 2021 12:51:25 +0200 Subject: [PATCH 06/10] 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'); From d67d53b9264000da1d9b5d3523421dd1a838e0ca Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Tue, 22 Jun 2021 13:27:03 +0200 Subject: [PATCH 07/10] Code style fixes --- app/services/experiments_overview_service.rb | 2 +- db/migrate/20210622101238_fix_experiment_indices.rb | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/services/experiments_overview_service.rb b/app/services/experiments_overview_service.rb index 64a47037b..b27e1fd61 100644 --- a/app/services/experiments_overview_service.rb +++ b/app/services/experiments_overview_service.rb @@ -50,7 +50,7 @@ class ExperimentsOverviewService records = records.active if @view_mode == 'active' if @params[:search].present? records = records.where_attributes_like( - ["experiments.name", "experiments.description", "('EX' || experiments.id)"], + ['experiments.name', 'experiments.description', "('EX' || experiments.id)"], @params[:search] ) end diff --git a/db/migrate/20210622101238_fix_experiment_indices.rb b/db/migrate/20210622101238_fix_experiment_indices.rb index 9111aa61b..e6ad4cb94 100644 --- a/db/migrate/20210622101238_fix_experiment_indices.rb +++ b/db/migrate/20210622101238_fix_experiment_indices.rb @@ -1,14 +1,16 @@ +# frozen_string_literal: true + 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);" + '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);" + 'CREATE INDEX index_experiments_on_description ON experiments ' \ + 'using gin (trim_html_tags(description) gin_trgm_ops);' ) end From e1b0b65465eb1e3962ec719d17416ad017abf82b Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Tue, 29 Jun 2021 15:57:23 +0200 Subject: [PATCH 08/10] 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'); From 3e1f0fe109becc1fe7d05209e7a38215492cc17d Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Wed, 30 Jun 2021 10:28:55 +0200 Subject: [PATCH 09/10] Migration code style fix [SCI-5828] --- db/migrate/20210622101238_fix_experiment_indices.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/db/migrate/20210622101238_fix_experiment_indices.rb b/db/migrate/20210622101238_fix_experiment_indices.rb index a633ccafd..634cac14a 100644 --- a/db/migrate/20210622101238_fix_experiment_indices.rb +++ b/db/migrate/20210622101238_fix_experiment_indices.rb @@ -1,8 +1,10 @@ # frozen_string_literal: true + require File.expand_path('app/helpers/database_helper') -include DatabaseHelper class FixExperimentIndices < ActiveRecord::Migration[6.1] + include DatabaseHelper + def up remove_index :experiments, name: 'index_experiments_on_name', column: 'name' From 524ea05873ea2f78b1215821c596ad07a6d2a27d Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Wed, 30 Jun 2021 10:37:11 +0200 Subject: [PATCH 10/10] Fix migration rollback [SCI-5828] --- db/migrate/20210622101238_fix_experiment_indices.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/db/migrate/20210622101238_fix_experiment_indices.rb b/db/migrate/20210622101238_fix_experiment_indices.rb index 634cac14a..e33d7cd6b 100644 --- a/db/migrate/20210622101238_fix_experiment_indices.rb +++ b/db/migrate/20210622101238_fix_experiment_indices.rb @@ -17,9 +17,9 @@ class FixExperimentIndices < ActiveRecord::Migration[6.1] end def down - 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' + remove_index :experiments, name: 'index_experiments_on_experiment_code' + remove_index :experiments, name: 'index_experiments_on_description' + remove_index :experiments, name: 'index_experiments_on_name' add_index :experiments, :name end