Skip to content
代码片段 群组 项目
提交 352f692c 编辑于 作者: Adam Hegyi's avatar Adam Hegyi
浏览文件

Create namespace descendants table

This change adds a cache table for storing namespace descendants and
project ids so we can retrieve them efficiently.

Changelog: added
上级 a22a57d9
未找到相关分支
未找到相关标签
无相关合并请求
# frozen_string_literal: true
module Namespaces
class Descendants < ApplicationRecord
self.table_name = :namespace_descendants
belongs_to :namespace
validates :namespace_id, uniqueness: true
end
end
---
table_name: namespace_descendants
classes:
- Namespaces::Descendants
feature_categories:
- groups_and_projects
description: Storing de-normalized descendant ids for Namespace records
introduced_by_url:
milestone: '16.8'
gitlab_schema: gitlab_main_cell
sharding_key:
namespace_id: namespaces
# frozen_string_literal: true
class CreateNamespaceDescendantsTable < Gitlab::Database::Migration[2.2]
include Gitlab::Database::PartitioningMigrationHelpers::TableManagementHelpers
milestone '16.8'
def up
execute <<~SQL
CREATE TABLE namespace_descendants (
namespace_id bigint NOT NULL,
self_and_descendant_group_ids bigint[] NOT NULL DEFAULT ARRAY[]::bigint[],
all_project_ids bigint[] NOT NULL DEFAULT ARRAY[]::bigint[],
traversal_ids bigint[] NOT NULL DEFAULT ARRAY[]::bigint[],
outdated_at timestamp with time zone,
calculated_at timestamp with time zone,
PRIMARY KEY(namespace_id)
)
PARTITION BY HASH (namespace_id);
SQL
execute <<~SQL
CREATE INDEX
index_on_namespace_descendants_outdated
ON namespace_descendants (namespace_id)
WHERE outdated_at IS NOT NULL
SQL
create_hash_partitions(:namespace_descendants, 32)
end
def down
drop_table :namespace_descendants
end
end
b03eee7eff8f7402f3c590b6ae2010c6c278aaa433db52d444a60357bbd8b582
\ No newline at end of file
此差异已折叠。
......@@ -87,6 +87,7 @@
merge_request_diffs: %w[project_id],
merge_request_diff_commits: %w[commit_author_id committer_id],
namespaces: %w[owner_id parent_id],
namespace_descendants: %w[namespace_id],
notes: %w[author_id commit_id noteable_id updated_by_id resolved_by_id confirmed_by_id discussion_id namespace_id],
notification_settings: %w[source_id],
oauth_access_grants: %w[resource_owner_id application_id],
......
# frozen_string_literal: true
FactoryBot.define do
factory :namespace_descendants, class: 'Namespaces::Descendants' do
namespace { association(:group) }
self_and_descendant_group_ids { namespace.self_and_descendant_ids.pluck(:id).sort }
all_project_ids { namespace.all_projects.pluck(:id).sort }
traversal_ids { namespace.traversal_ids }
outdated_at { nil }
calculated_at { Time.current }
end
end
......@@ -27,7 +27,8 @@
let(:allowed_to_be_missing_foreign_key) do
[
'p_catalog_resource_sync_events.project_id',
'zoekt_indices.namespace_id'
'zoekt_indices.namespace_id',
'namespace_descendants.namespace_id'
]
end
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Namespaces::Descendants, feature_category: :database do
describe 'associations' do
it { is_expected.to belong_to(:namespace) }
end
describe 'validations' do
subject(:namespace_descendants) { create(:namespace_descendants) }
it { is_expected.to validate_uniqueness_of(:namespace_id) }
end
describe 'factory' do
let_it_be(:group) { create(:group) }
let_it_be(:subgroup) { create(:group, parent: group) }
let_it_be(:project1) { create(:project, group: subgroup) }
let_it_be(:project2) { create(:project, group: group) }
it 'up to date descendant record for a group' do
descendants = create(:namespace_descendants, namespace: group)
expect(descendants).to have_attributes(
self_and_descendant_group_ids: [group.id, subgroup.id],
all_project_ids: [project1.id, project2.id],
traversal_ids: [group.id]
)
end
it 'creates up-to-date descendant record for a subgroup' do
descendants = create(:namespace_descendants, namespace: subgroup)
expect(descendants).to have_attributes(
self_and_descendant_group_ids: [subgroup.id],
all_project_ids: [project1.id],
traversal_ids: [group.id, subgroup.id]
)
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
想要评论请 注册