Add counter cache for counting repository row on repository [SCI-11114]

This commit is contained in:
Andrej 2024-10-03 07:58:53 +02:00
parent f6bb7022d9
commit 94c478ab25
4 changed files with 16 additions and 5 deletions

View file

@ -10,7 +10,7 @@ class RepositoryRow < ApplicationRecord
ID_PREFIX = 'IT'
include PrefixedIdModel
belongs_to :repository, class_name: 'RepositoryBase'
belongs_to :repository, class_name: 'RepositoryBase', counter_cache: :repository_rows_count
delegate :team, to: :repository
belongs_to :parent, class_name: 'RepositoryRow', optional: true
belongs_to :created_by, class_name: 'User'

View file

@ -9,7 +9,7 @@ module Lists
attributes :name, :code, :nr_of_rows, :team, :created_at, :created_by, :archived_on, :archived_by, :urls
def nr_of_rows
object[:row_count]
object[:repository_rows_count]
end
def team

View file

@ -9,11 +9,9 @@ module Lists
'ON repositories.created_by_id = creators.id')
.joins('LEFT OUTER JOIN users AS archivers ' \
'ON repositories.archived_by_id = archivers.id')
.left_outer_joins(:repository_rows)
.joins(:team)
.select('repositories.*')
.select('MAX(teams.name) AS team_name')
.select('COUNT(DISTINCT(repository_rows.*)) AS row_count')
.select('MAX(creators.full_name) AS created_by_user')
.select('MAX(archivers.full_name) AS archived_by_user')
.select(shared_sql_select)
@ -47,7 +45,7 @@ module Lists
created_at: 'repositories.created_at',
archived_on: 'repositories.archived_on',
archived_by: 'archived_by_user',
nr_of_rows: 'row_count',
nr_of_rows: 'repository_rows_count',
code: 'repositories.id',
shared_label: 'shared'
}

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
class AddCounterCacheRepositoryRow < ActiveRecord::Migration[7.0]
def change
change_table :repositories, bulk: true do |t|
t.integer :repository_rows_count, default: 0, null: false
end
Repository.find_each do |repository|
Repository.reset_counters(repository.id, :repository_rows)
end
end
end