From 8c2b1e503750f833c813bb599e22f09180c0122e Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Thu, 16 Jan 2020 15:30:19 +0100 Subject: [PATCH 1/2] Fix global search for repositories and checklists update [SCI-4251] --- app/controllers/search_controller.rb | 4 +- app/models/repository.rb | 39 +++++++++---------- app/models/repository_checklist_value.rb | 14 ++++--- ...03091614_create_repository_status_items.rb | 10 ++++- ...1205133522_create_repository_checklists.rb | 12 +++++- 5 files changed, 45 insertions(+), 34 deletions(-) diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index 4c04fa795..af71167f3 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -138,9 +138,7 @@ class SearchController < ApplicationController repository_results[:repository] = repository repository_results[:count] = 0 search_results.each do |result| - if repository.id == result.id - repository_results[:count] += result.counter - end + repository_results[:count] += result.counter if repository.id == result.id end team_results[:repositories][repository.name] = repository_results team_results[:count] += repository_results[:count] diff --git a/app/models/repository.rb b/app/models/repository.rb index 15441ed50..a3060d6aa 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -57,34 +57,31 @@ class Repository < ApplicationRecord repository = nil, options = {} ) - repositories = repository || - Repository.accessible_by_teams(user.teams.pluck(:id)) + repositories = repository&.id || Repository.accessible_by_teams(user.teams.pluck(:id)).pluck(:id) - includes_json = { repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES } - searchable_attributes = ['repository_rows.name', 'users.full_name'] + - Extends::REPOSITORY_EXTRA_SEARCH_ATTR + readable_rows = RepositoryRow.joins(:repository).where(repository_id: repositories) - all_rows = RepositoryRow.where(repository: repositories) + matched_by_user = readable_rows.joins(:created_by).where_attributes_like('users.full_name', query, options) - new_query = RepositoryRow - .distinct - .from(all_rows, 'repository_rows') - .left_outer_joins(:created_by) - .left_outer_joins(includes_json) - .where_attributes_like(searchable_attributes, query, options) + repository_row_matches = + readable_rows.where_attributes_like(['repository_rows.name', 'repository_rows.id'], query, options) + + repository_rows = readable_rows.where(id: repository_row_matches) + repository_rows = repository_rows.or(readable_rows.where(id: matched_by_user)) + + Extends::REPOSITORY_EXTRA_SEARCH_ATTR.each do |field, include_hash| + custom_cell_matches = readable_rows.joins(repository_cells: include_hash) + .where_attributes_like(field, query, options) + repository_rows = repository_rows.or(readable_rows.where(id: custom_cell_matches)) + end # Show all results if needed if page == Constants::SEARCH_NO_LIMIT - new_query - .joins(:repository) - .select( - 'repositories.id AS id, COUNT(DISTINCT repository_rows.id) AS counter' - ) - .group('repositories.id') + repository_rows.select('repositories.id AS id, COUNT(DISTINCT repository_rows.id) AS counter') + .group('repositories.id') else - new_query - .limit(Constants::SEARCH_LIMIT) - .offset((page - 1) * Constants::SEARCH_LIMIT) + repository_rows.limit(Constants::SEARCH_LIMIT) + .offset((page - 1) * Constants::SEARCH_LIMIT) end end diff --git a/app/models/repository_checklist_value.rb b/app/models/repository_checklist_value.rb index bfd219c0a..0364bbe0f 100644 --- a/app/models/repository_checklist_value.rb +++ b/app/models/repository_checklist_value.rb @@ -33,11 +33,12 @@ class RepositoryChecklistValue < ApplicationRecord end def update_data!(new_data, user) - repository_checklist_items_values.destroy_all - repository_cell.repository_column - .repository_checklist_items.where(id: JSON.parse(new_data)).find_each do |item| - repository_checklist_items_values.create!(repository_checklist_item: item) - end + item_ids = JSON.parse(new_data) + return destroy! if item_ids.blank? + + self.repository_checklist_items = repository_cell.repository_column + .repository_checklist_items + .where(id: item_ids) self.last_modified_by = user save! end @@ -46,7 +47,8 @@ class RepositoryChecklistValue < ApplicationRecord value = new(attributes) value.repository_checklist_items = value.repository_cell .repository_column - .repository_checklist_items.where(id: JSON.parse(payload)) + .repository_checklist_items + .where(id: JSON.parse(payload)) value end end diff --git a/db/migrate/20191003091614_create_repository_status_items.rb b/db/migrate/20191003091614_create_repository_status_items.rb index 4e8ef9336..b71a0a6fd 100644 --- a/db/migrate/20191003091614_create_repository_status_items.rb +++ b/db/migrate/20191003091614_create_repository_status_items.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true class CreateRepositoryStatusItems < ActiveRecord::Migration[6.0] - def change + def up create_table :repository_status_items do |t| - t.string :status, null: false, index: true + t.string :status, null: false t.string :icon, null: false t.references :repository, null: false, foreign_key: true t.references :repository_column, null: false, foreign_key: true @@ -12,5 +12,11 @@ class CreateRepositoryStatusItems < ActiveRecord::Migration[6.0] t.timestamps end + + add_gin_index_without_tags :repository_status_items, :data + end + + def down + drop_table :repository_status_items end end diff --git a/db/migrate/20191205133522_create_repository_checklists.rb b/db/migrate/20191205133522_create_repository_checklists.rb index 4d0619c62..460994768 100644 --- a/db/migrate/20191205133522_create_repository_checklists.rb +++ b/db/migrate/20191205133522_create_repository_checklists.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class CreateRepositoryChecklists < ActiveRecord::Migration[6.0] - def change + def up create_table :repository_checklist_values do |t| t.references :created_by, index: true, foreign_key: { to_table: :users }, null: true t.references :last_modified_by, index: true, foreign_key: { to_table: :users }, null: true @@ -10,7 +10,7 @@ class CreateRepositoryChecklists < ActiveRecord::Migration[6.0] end create_table :repository_checklist_items do |t| - t.string :data, null: false, index: true + t.string :data, null: false t.references :repository, null: false, foreign_key: true t.references :repository_column, null: false, foreign_key: true t.references :created_by, index: true, foreign_key: { to_table: :users }, null: true @@ -19,10 +19,18 @@ class CreateRepositoryChecklists < ActiveRecord::Migration[6.0] t.timestamps end + add_gin_index_without_tags :repository_checklist_items, :data + create_table :repository_checklist_items_values do |t| t.belongs_to :repository_checklist_value, index: { name: 'index_on_repository_checklist_value_id' } t.belongs_to :repository_checklist_item, index: { name: 'index_on_repository_checklist_item_id' } t.timestamps end end + + def down + drop_table :repository_checklist_items_values + drop_table :repository_checklist_items + drop_table :repository_checklist_values + end end From 1c33885754dd9198a0d4c0111c0c7f890c5aaf67 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Thu, 16 Jan 2020 16:05:49 +0100 Subject: [PATCH 2/2] Fix status item migration [SCI-4251] --- db/migrate/20191003091614_create_repository_status_items.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/migrate/20191003091614_create_repository_status_items.rb b/db/migrate/20191003091614_create_repository_status_items.rb index b71a0a6fd..9692dd09d 100644 --- a/db/migrate/20191003091614_create_repository_status_items.rb +++ b/db/migrate/20191003091614_create_repository_status_items.rb @@ -13,7 +13,7 @@ class CreateRepositoryStatusItems < ActiveRecord::Migration[6.0] t.timestamps end - add_gin_index_without_tags :repository_status_items, :data + add_gin_index_without_tags :repository_status_items, :status end def down