Commit 7885eb99 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'fj-223696-add-snippet-statistics' into 'master'

Add SnippetStatistics model

See merge request gitlab-org/gitlab!35026
parents 45700a48 e634990c
......@@ -44,6 +44,7 @@ class Snippet < ApplicationRecord
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_one :snippet_repository, inverse_of: :snippet
has_one :statistics, class_name: 'SnippetStatistics'
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 (
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 (
id bigint NOT NULL,
snippet_id integer NOT NULL,
......@@ -9045,6 +9052,9 @@ ALTER TABLE ONLY public.smartcard_identities
ALTER TABLE ONLY public.snippet_repositories
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
ADD CONSTRAINT snippet_user_mentions_pkey PRIMARY KEY (id);
......@@ -12949,6 +12959,9 @@ ALTER TABLE ONLY public.protected_branch_unprotect_access_levels
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;
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
ADD CONSTRAINT fk_rails_ebc2931b90 FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE;
......@@ -14105,5 +14118,6 @@ COPY "schema_migrations" (version) FROM STDIN;
20200618105638
20200618134223
20200618134723
20200622103836
\.
......@@ -97,6 +97,7 @@ snippets:
- user_agent_detail
- user_mentions
- snippet_repository
- statistics
releases:
- author
- project
......
......@@ -20,6 +20,7 @@ describe Snippet do
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_one(:snippet_repository) }
it { is_expected.to have_one(:statistics).class_name('SnippetStatistics') }
end
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