diff --git a/app/models/my_module.rb b/app/models/my_module.rb index 271a8f08d..94e2f3540 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -2,10 +2,9 @@ class MyModule < ApplicationRecord ID_PREFIX = 'TA' + SEARCHABLE_ATTRIBUTES = ['my_modules.name', 'my_modules.description', PREFIXED_ID_SQL].freeze include PrefixedIdModel - SEARCHABLE_ATTRIBUTES = ['my_modules.name', 'my_modules.description', PREFIXED_ID_SQL].freeze - include ArchivableModel include SearchableModel include SearchableByNameModel diff --git a/app/models/project.rb b/app/models/project.rb index d50962d7c..a203763d5 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -2,6 +2,7 @@ class Project < ApplicationRecord ID_PREFIX = 'PR' + SEARCHABLE_ATTRIBUTES = ['projects.name', PREFIXED_ID_SQL].freeze include PrefixedIdModel include ArchivableModel @@ -85,7 +86,7 @@ class Project < ApplicationRecord ) new_query = Project.viewable_by_user(user, current_team || user.teams) - .where_attributes_like('projects.name', query, options) + .where_attributes_like(SEARCHABLE_ATTRIBUTES, query, options) new_query = new_query.active unless include_archived # Show all results if needed diff --git a/app/models/protocol.rb b/app/models/protocol.rb index c39c29ba6..cfc8ce356 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -2,6 +2,8 @@ class Protocol < ApplicationRecord ID_PREFIX = 'PT' + SEARCHABLE_ATTRIBUTES = ['protocols.name', 'protocols.description', + 'protocols.authors', 'protocol_keywords.name', PREFIXED_ID_SQL].freeze include PrefixedIdModel include SearchableModel @@ -183,15 +185,7 @@ class Protocol < ApplicationRecord .joins('LEFT JOIN protocol_keywords ' \ 'ON protocol_keywords.id = ' \ 'protocol_protocol_keywords.protocol_keyword_id') - .where_attributes_like( - [ - 'protocols.name', - 'protocols.description', - 'protocols.authors', - 'protocol_keywords.name' - ], - query, options - ) + .where_attributes_like(SEARCHABLE_ATTRIBUTES, query, options) # Show all results if needed if page == Constants::SEARCH_NO_LIMIT diff --git a/app/models/report.rb b/app/models/report.rb index e5865c570..c06854caf 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true class Report < ApplicationRecord + ID_PREFIX = 'RP' + SEARCHABLE_ATTRIBUTES = ['reports.name', 'reports.description', PREFIXED_ID_SQL].freeze + + include PrefixedIdModel include SettingsModel include Assignable include PermissionCheckableModel @@ -74,7 +78,7 @@ class Report < ApplicationRecord new_query = Report.distinct .where(reports: { project_id: project_ids }) - .where_attributes_like(%i(name description), query, options) + .where_attributes_like(SEARCHABLE_ATTRIBUTES, query, options) # Show all results if needed if page == Constants::SEARCH_NO_LIMIT diff --git a/db/migrate/20221028085051_add_code_indices.rb b/db/migrate/20221028085051_add_code_indices.rb new file mode 100644 index 000000000..d38ea976a --- /dev/null +++ b/db/migrate/20221028085051_add_code_indices.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +class AddCodeIndices < ActiveRecord::Migration[6.1] + def up + ActiveRecord::Base.connection.execute( + "CREATE INDEX index_projects_on_project_code ON "\ + "projects using gin (('#{Project::ID_PREFIX}'::text || id) gin_trgm_ops);" + ) + ActiveRecord::Base.connection.execute( + "CREATE INDEX index_my_modules_on_my_module_code ON "\ + "my_modules using gin (('#{MyModule::ID_PREFIX}'::text || id) gin_trgm_ops);" + ) + ActiveRecord::Base.connection.execute( + "CREATE INDEX index_protocols_on_protocol_code ON "\ + "protocols using gin (('#{Protocol::ID_PREFIX}'::text || id) gin_trgm_ops);" + ) + ActiveRecord::Base.connection.execute( + "CREATE INDEX index_reports_on_report_code ON "\ + "reports using gin (('#{Report::ID_PREFIX}'::text || id) gin_trgm_ops);" + ) + end + + def down + remove_index :projects, name: 'index_projects_on_project_code' + remove_index :my_modules, name: 'index_my_modules_on_my_module_code' + remove_index :protocols, name: 'index_protocols_on_protocol_code' + remove_index :reports, name: 'index_reports_on_report_code' + end +end diff --git a/db/migrate/20221108104529_add_my_module_code_index.rb b/db/migrate/20221108104529_add_my_module_code_index.rb deleted file mode 100644 index 3dc6bca66..000000000 --- a/db/migrate/20221108104529_add_my_module_code_index.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class AddMyModuleCodeIndex < ActiveRecord::Migration[6.1] - def up - ActiveRecord::Base.connection.execute( - "CREATE INDEX index_my_modules_on_my_module_code ON "\ - "my_modules using gin (('TA'::text || id) gin_trgm_ops);" - ) - end - - def down - remove_index :my_modules, name: 'index_my_modules_on_my_module_code' - end -end diff --git a/db/migrate/20221121132449_add_protocol_repository_code_index.rb b/db/migrate/20221121132449_add_protocol_repository_code_index.rb deleted file mode 100644 index 6c3129c1f..000000000 --- a/db/migrate/20221121132449_add_protocol_repository_code_index.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -class AddProtocolRepositoryCodeIndex < ActiveRecord::Migration[6.1] - def up - ActiveRecord::Base.connection.execute( - "CREATE INDEX index_protocol_repository_on_protocol_repository_code ON "\ - "protocols using gin (('PT'::text || id) gin_trgm_ops);" - ) - end - - def down - remove_index :protocols, name: 'index_protocol_repository_on_protocol_repository_code' - end -end diff --git a/db/structure.sql b/db/structure.sql index 6f1eec5da..8587d0f11 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -50,8 +50,6 @@ CREATE FUNCTION public.trim_html_tags(input text, OUT output text) RETURNS text SET default_tablespace = ''; -SET default_table_access_method = heap; - -- -- Name: active_storage_attachments; Type: TABLE; Schema: public; Owner: - -- @@ -710,7 +708,6 @@ CREATE TABLE public.label_templates ( type character varying, width_mm double precision, height_mm double precision, - height_mm double precision, unit integer DEFAULT 0, density integer DEFAULT 12 ); @@ -5330,6 +5327,13 @@ CREATE INDEX index_my_modules_on_experiment_id ON public.my_modules USING btree CREATE INDEX index_my_modules_on_last_modified_by_id ON public.my_modules USING btree (last_modified_by_id); +-- +-- Name: index_my_modules_on_my_module_code; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_my_modules_on_my_module_code ON public.my_modules USING gin ((('TA'::text || id)) public.gin_trgm_ops); + + -- -- Name: index_my_modules_on_my_module_group_id; Type: INDEX; Schema: public; Owner: - -- @@ -5505,6 +5509,13 @@ CREATE INDEX index_projects_on_last_modified_by_id ON public.projects USING btre CREATE INDEX index_projects_on_name ON public.projects USING gin (public.trim_html_tags((name)::text) public.gin_trgm_ops); +-- +-- Name: index_projects_on_project_code; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_projects_on_project_code ON public.projects USING gin ((('PR'::text || id)) public.gin_trgm_ops); + + -- -- Name: index_projects_on_project_folder_id; Type: INDEX; Schema: public; Owner: - -- @@ -5603,6 +5614,13 @@ CREATE INDEX index_protocols_on_name ON public.protocols USING gin (public.trim_ CREATE INDEX index_protocols_on_parent_id ON public.protocols USING btree (parent_id); +-- +-- Name: index_protocols_on_protocol_code; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_protocols_on_protocol_code ON public.protocols USING gin ((('PT'::text || id)) public.gin_trgm_ops); + + -- -- Name: index_protocols_on_protocol_type; Type: INDEX; Schema: public; Owner: - -- @@ -5743,6 +5761,13 @@ CREATE INDEX index_reports_on_name ON public.reports USING gin (public.trim_html CREATE INDEX index_reports_on_project_id ON public.reports USING btree (project_id); +-- +-- Name: index_reports_on_report_code; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_reports_on_report_code ON public.reports USING gin ((('RP'::text || id)) public.gin_trgm_ops); + + -- -- Name: index_reports_on_team_id; Type: INDEX; Schema: public; Owner: - -- @@ -8568,6 +8593,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20220803122405'), ('20220818094636'), ('20220914124900'), -('20221007113010'); +('20221007113010'), +('20221028085051');