Add SnippetStatistics model

parent f4462041
...@@ -44,6 +44,7 @@ class Snippet < ApplicationRecord ...@@ -44,6 +44,7 @@ class Snippet < ApplicationRecord
has_many :notes, as: :noteable, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :notes, as: :noteable, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :user_mentions, class_name: "SnippetUserMention", dependent: :delete_all # rubocop:disable Cop/ActiveRecordDependent has_many :user_mentions, class_name: "SnippetUserMention", dependent: :delete_all # rubocop:disable Cop/ActiveRecordDependent
has_one :snippet_repository, inverse_of: :snippet has_one :snippet_repository, inverse_of: :snippet
has_one :statistics, class_name: 'SnippetStatistics'
delegate :name, :email, to: :author, prefix: true, allow_nil: true delegate :name, :email, to: :author, prefix: true, allow_nil: true
......
# frozen_string_literal: true
class SnippetStatistics < ApplicationRecord
belongs_to :snippet
validates :snippet, presence: true
end
---
title: Add SnippetStatistics model
merge_request: 35026
author:
type: added
# frozen_string_literal: true
class CreateSnippetStatistics < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
with_lock_retries do
create_table :snippet_statistics, id: false do |t|
t.references :snippet, primary_key: true, default: nil, index: false, foreign_key: { on_delete: :cascade }
t.bigint :repository_size, default: 0, null: false
t.bigint :file_count, default: 0, null: false
t.bigint :commit_count, default: 0, null: false
end
end
end
def down
with_lock_retries do
drop_table :snippet_statistics # rubocop:disable Migration/DropTable
end
end
end
...@@ -6335,6 +6335,13 @@ CREATE TABLE public.snippet_repositories ( ...@@ -6335,6 +6335,13 @@ CREATE TABLE public.snippet_repositories (
disk_path character varying(80) NOT NULL disk_path character varying(80) NOT NULL
); );
CREATE TABLE public.snippet_statistics (
snippet_id bigint NOT NULL,
repository_size bigint DEFAULT 0 NOT NULL,
file_count bigint DEFAULT 0 NOT NULL,
commit_count bigint DEFAULT 0 NOT NULL
);
CREATE TABLE public.snippet_user_mentions ( CREATE TABLE public.snippet_user_mentions (
id bigint NOT NULL, id bigint NOT NULL,
snippet_id integer NOT NULL, snippet_id integer NOT NULL,
...@@ -9045,6 +9052,9 @@ ALTER TABLE ONLY public.smartcard_identities ...@@ -9045,6 +9052,9 @@ ALTER TABLE ONLY public.smartcard_identities
ALTER TABLE ONLY public.snippet_repositories ALTER TABLE ONLY public.snippet_repositories
ADD CONSTRAINT snippet_repositories_pkey PRIMARY KEY (snippet_id); ADD CONSTRAINT snippet_repositories_pkey PRIMARY KEY (snippet_id);
ALTER TABLE ONLY public.snippet_statistics
ADD CONSTRAINT snippet_statistics_pkey PRIMARY KEY (snippet_id);
ALTER TABLE ONLY public.snippet_user_mentions ALTER TABLE ONLY public.snippet_user_mentions
ADD CONSTRAINT snippet_user_mentions_pkey PRIMARY KEY (id); ADD CONSTRAINT snippet_user_mentions_pkey PRIMARY KEY (id);
...@@ -12949,6 +12959,9 @@ ALTER TABLE ONLY public.protected_branch_unprotect_access_levels ...@@ -12949,6 +12959,9 @@ ALTER TABLE ONLY public.protected_branch_unprotect_access_levels
ALTER TABLE ONLY public.alert_management_alert_user_mentions ALTER TABLE ONLY public.alert_management_alert_user_mentions
ADD CONSTRAINT fk_rails_eb2de0cdef FOREIGN KEY (note_id) REFERENCES public.notes(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_eb2de0cdef FOREIGN KEY (note_id) REFERENCES public.notes(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.snippet_statistics
ADD CONSTRAINT fk_rails_ebc283ccf1 FOREIGN KEY (snippet_id) REFERENCES public.snippets(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.ci_daily_report_results ALTER TABLE ONLY public.ci_daily_report_results
ADD CONSTRAINT fk_rails_ebc2931b90 FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_ebc2931b90 FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE;
...@@ -14104,5 +14117,6 @@ COPY "schema_migrations" (version) FROM STDIN; ...@@ -14104,5 +14117,6 @@ COPY "schema_migrations" (version) FROM STDIN;
20200618105638 20200618105638
20200618134223 20200618134223
20200618134723 20200618134723
20200622103836
\. \.
...@@ -97,6 +97,7 @@ snippets: ...@@ -97,6 +97,7 @@ snippets:
- user_agent_detail - user_agent_detail
- user_mentions - user_mentions
- snippet_repository - snippet_repository
- statistics
releases: releases:
- author - author
- project - project
......
...@@ -20,6 +20,7 @@ describe Snippet do ...@@ -20,6 +20,7 @@ describe Snippet do
it { is_expected.to have_many(:award_emoji).dependent(:destroy) } it { is_expected.to have_many(:award_emoji).dependent(:destroy) }
it { is_expected.to have_many(:user_mentions).class_name("SnippetUserMention") } it { is_expected.to have_many(:user_mentions).class_name("SnippetUserMention") }
it { is_expected.to have_one(:snippet_repository) } it { is_expected.to have_one(:snippet_repository) }
it { is_expected.to have_one(:statistics).class_name('SnippetStatistics') }
end end
describe 'validation' do describe 'validation' do
......
# frozen_string_literal: true
require 'spec_helper'
describe SnippetStatistics do
it { is_expected.to belong_to(:snippet) }
it { is_expected.to validate_presence_of(:snippet) }
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment