Merge pull request #2322 from okriuchykhin/ok_SCI_4251

Fix global search for repositories and checklists update [SCI-4251]
This commit is contained in:
Alex Kriuchykhin 2020-01-16 16:37:12 +01:00 committed by GitHub
commit e3e36d40a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 34 deletions

View file

@ -138,9 +138,7 @@ class SearchController < ApplicationController
repository_results[:repository] = repository repository_results[:repository] = repository
repository_results[:count] = 0 repository_results[:count] = 0
search_results.each do |result| search_results.each do |result|
if repository.id == result.id repository_results[:count] += result.counter if repository.id == result.id
repository_results[:count] += result.counter
end
end end
team_results[:repositories][repository.name] = repository_results team_results[:repositories][repository.name] = repository_results
team_results[:count] += repository_results[:count] team_results[:count] += repository_results[:count]

View file

@ -57,33 +57,30 @@ class Repository < ApplicationRecord
repository = nil, repository = nil,
options = {} options = {}
) )
repositories = repository || repositories = repository&.id || Repository.accessible_by_teams(user.teams.pluck(:id)).pluck(:id)
Repository.accessible_by_teams(user.teams.pluck(:id))
includes_json = { repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES } readable_rows = RepositoryRow.joins(:repository).where(repository_id: repositories)
searchable_attributes = ['repository_rows.name', 'users.full_name'] +
Extends::REPOSITORY_EXTRA_SEARCH_ATTR
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 repository_row_matches =
.distinct readable_rows.where_attributes_like(['repository_rows.name', 'repository_rows.id'], query, options)
.from(all_rows, 'repository_rows')
.left_outer_joins(:created_by) repository_rows = readable_rows.where(id: repository_row_matches)
.left_outer_joins(includes_json) repository_rows = repository_rows.or(readable_rows.where(id: matched_by_user))
.where_attributes_like(searchable_attributes, query, options)
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 # Show all results if needed
if page == Constants::SEARCH_NO_LIMIT if page == Constants::SEARCH_NO_LIMIT
new_query repository_rows.select('repositories.id AS id, COUNT(DISTINCT repository_rows.id) AS counter')
.joins(:repository)
.select(
'repositories.id AS id, COUNT(DISTINCT repository_rows.id) AS counter'
)
.group('repositories.id') .group('repositories.id')
else else
new_query repository_rows.limit(Constants::SEARCH_LIMIT)
.limit(Constants::SEARCH_LIMIT)
.offset((page - 1) * Constants::SEARCH_LIMIT) .offset((page - 1) * Constants::SEARCH_LIMIT)
end end
end end

View file

@ -33,11 +33,12 @@ class RepositoryChecklistValue < ApplicationRecord
end end
def update_data!(new_data, user) def update_data!(new_data, user)
repository_checklist_items_values.destroy_all item_ids = JSON.parse(new_data)
repository_cell.repository_column return destroy! if item_ids.blank?
.repository_checklist_items.where(id: JSON.parse(new_data)).find_each do |item|
repository_checklist_items_values.create!(repository_checklist_item: item) self.repository_checklist_items = repository_cell.repository_column
end .repository_checklist_items
.where(id: item_ids)
self.last_modified_by = user self.last_modified_by = user
save! save!
end end
@ -46,7 +47,8 @@ class RepositoryChecklistValue < ApplicationRecord
value = new(attributes) value = new(attributes)
value.repository_checklist_items = value.repository_cell value.repository_checklist_items = value.repository_cell
.repository_column .repository_column
.repository_checklist_items.where(id: JSON.parse(payload)) .repository_checklist_items
.where(id: JSON.parse(payload))
value value
end end
end end

View file

@ -1,9 +1,9 @@
# frozen_string_literal: true # frozen_string_literal: true
class CreateRepositoryStatusItems < ActiveRecord::Migration[6.0] class CreateRepositoryStatusItems < ActiveRecord::Migration[6.0]
def change def up
create_table :repository_status_items do |t| 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.string :icon, null: false
t.references :repository, null: false, foreign_key: true t.references :repository, null: false, foreign_key: true
t.references :repository_column, 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 t.timestamps
end end
add_gin_index_without_tags :repository_status_items, :status
end
def down
drop_table :repository_status_items
end end
end end

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class CreateRepositoryChecklists < ActiveRecord::Migration[6.0] class CreateRepositoryChecklists < ActiveRecord::Migration[6.0]
def change def up
create_table :repository_checklist_values do |t| create_table :repository_checklist_values do |t|
t.references :created_by, index: true, foreign_key: { to_table: :users }, null: true 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 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 end
create_table :repository_checklist_items do |t| 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, null: false, foreign_key: true
t.references :repository_column, 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 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 t.timestamps
end end
add_gin_index_without_tags :repository_checklist_items, :data
create_table :repository_checklist_items_values do |t| 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_value, index: { name: 'index_on_repository_checklist_value_id' }
t.belongs_to :repository_checklist_item, index: { name: 'index_on_repository_checklist_item_id' } t.belongs_to :repository_checklist_item, index: { name: 'index_on_repository_checklist_item_id' }
t.timestamps t.timestamps
end end
end end
def down
drop_table :repository_checklist_items_values
drop_table :repository_checklist_items
drop_table :repository_checklist_values
end
end end