mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-09 22:55:34 +08:00
Add counter cache for counting repository row on repository [SCI-11114]
This commit is contained in:
parent
f6bb7022d9
commit
94c478ab25
4 changed files with 16 additions and 5 deletions
|
@ -10,7 +10,7 @@ class RepositoryRow < ApplicationRecord
|
||||||
ID_PREFIX = 'IT'
|
ID_PREFIX = 'IT'
|
||||||
include PrefixedIdModel
|
include PrefixedIdModel
|
||||||
|
|
||||||
belongs_to :repository, class_name: 'RepositoryBase'
|
belongs_to :repository, class_name: 'RepositoryBase', counter_cache: :repository_rows_count
|
||||||
delegate :team, to: :repository
|
delegate :team, to: :repository
|
||||||
belongs_to :parent, class_name: 'RepositoryRow', optional: true
|
belongs_to :parent, class_name: 'RepositoryRow', optional: true
|
||||||
belongs_to :created_by, class_name: 'User'
|
belongs_to :created_by, class_name: 'User'
|
||||||
|
|
|
@ -9,7 +9,7 @@ module Lists
|
||||||
attributes :name, :code, :nr_of_rows, :team, :created_at, :created_by, :archived_on, :archived_by, :urls
|
attributes :name, :code, :nr_of_rows, :team, :created_at, :created_by, :archived_on, :archived_by, :urls
|
||||||
|
|
||||||
def nr_of_rows
|
def nr_of_rows
|
||||||
object[:row_count]
|
object[:repository_rows_count]
|
||||||
end
|
end
|
||||||
|
|
||||||
def team
|
def team
|
||||||
|
|
|
@ -9,11 +9,9 @@ module Lists
|
||||||
'ON repositories.created_by_id = creators.id')
|
'ON repositories.created_by_id = creators.id')
|
||||||
.joins('LEFT OUTER JOIN users AS archivers ' \
|
.joins('LEFT OUTER JOIN users AS archivers ' \
|
||||||
'ON repositories.archived_by_id = archivers.id')
|
'ON repositories.archived_by_id = archivers.id')
|
||||||
.left_outer_joins(:repository_rows)
|
|
||||||
.joins(:team)
|
.joins(:team)
|
||||||
.select('repositories.*')
|
.select('repositories.*')
|
||||||
.select('MAX(teams.name) AS team_name')
|
.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(creators.full_name) AS created_by_user')
|
||||||
.select('MAX(archivers.full_name) AS archived_by_user')
|
.select('MAX(archivers.full_name) AS archived_by_user')
|
||||||
.select(shared_sql_select)
|
.select(shared_sql_select)
|
||||||
|
@ -47,7 +45,7 @@ module Lists
|
||||||
created_at: 'repositories.created_at',
|
created_at: 'repositories.created_at',
|
||||||
archived_on: 'repositories.archived_on',
|
archived_on: 'repositories.archived_on',
|
||||||
archived_by: 'archived_by_user',
|
archived_by: 'archived_by_user',
|
||||||
nr_of_rows: 'row_count',
|
nr_of_rows: 'repository_rows_count',
|
||||||
code: 'repositories.id',
|
code: 'repositories.id',
|
||||||
shared_label: 'shared'
|
shared_label: 'shared'
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
Loading…
Add table
Reference in a new issue