From bde41ee866d0fe0b1bb5ece1130fb6e24d95ad17 Mon Sep 17 00:00:00 2001 From: Mayra Cabrera Date: Wed, 12 Jun 2019 16:11:14 -0500 Subject: Add two new ActiveRecord models - Namespace::Storagestatistics will persist root namespace statistics - Namespace::AggregationSchedule will save information when a new update to the namespace statistics needs to be scheduled Both tables use 'namespace_id' as primary key --- app/models/namespace.rb | 2 ++ app/models/namespace/aggregation_schedule.rb | 7 +++++++ app/models/namespace/root_storage_statistics.rb | 10 ++++++++++ ...110_create_namespace_root_storage_statistics.rb | 22 ++++++++++++++++++++++ ...84422_create_namespace_aggregation_schedules.rb | 14 ++++++++++++++ db/schema.rb | 17 +++++++++++++++++ spec/factories/namespace/aggregation_schedules.rb | 7 +++++++ .../factories/namespace/root_storage_statistics.rb | 7 +++++++ spec/factories/namespaces.rb | 8 ++++++++ spec/models/namespace/aggregation_schedule_spec.rb | 7 +++++++ .../namespace/root_storage_statistics_spec.rb | 10 ++++++++++ spec/models/namespace_spec.rb | 2 ++ 12 files changed, 113 insertions(+) create mode 100644 app/models/namespace/aggregation_schedule.rb create mode 100644 app/models/namespace/root_storage_statistics.rb create mode 100644 db/migrate/20190531153110_create_namespace_root_storage_statistics.rb create mode 100644 db/migrate/20190605184422_create_namespace_aggregation_schedules.rb create mode 100644 spec/factories/namespace/aggregation_schedules.rb create mode 100644 spec/factories/namespace/root_storage_statistics.rb create mode 100644 spec/models/namespace/aggregation_schedule_spec.rb create mode 100644 spec/models/namespace/root_storage_statistics_spec.rb diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 3c270c7396a..f9b53b2b70a 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -35,6 +35,8 @@ class Namespace < ApplicationRecord belongs_to :parent, class_name: "Namespace" has_many :children, class_name: "Namespace", foreign_key: :parent_id has_one :chat_team, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent + has_one :root_storage_statistics, class_name: 'Namespace::RootStorageStatistics' + has_one :aggregation_schedule, class_name: 'Namespace::AggregationSchedule' validates :owner, presence: true, unless: ->(n) { n.type == "Group" } validates :name, diff --git a/app/models/namespace/aggregation_schedule.rb b/app/models/namespace/aggregation_schedule.rb new file mode 100644 index 00000000000..43afd0b954c --- /dev/null +++ b/app/models/namespace/aggregation_schedule.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class Namespace::AggregationSchedule < ApplicationRecord + self.primary_key = :namespace_id + + belongs_to :namespace +end diff --git a/app/models/namespace/root_storage_statistics.rb b/app/models/namespace/root_storage_statistics.rb new file mode 100644 index 00000000000..de28eb6b37f --- /dev/null +++ b/app/models/namespace/root_storage_statistics.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class Namespace::RootStorageStatistics < ApplicationRecord + self.primary_key = :namespace_id + + belongs_to :namespace + has_one :route, through: :namespace + + delegate :all_projects, to: :namespace +end diff --git a/db/migrate/20190531153110_create_namespace_root_storage_statistics.rb b/db/migrate/20190531153110_create_namespace_root_storage_statistics.rb new file mode 100644 index 00000000000..702560d05cc --- /dev/null +++ b/db/migrate/20190531153110_create_namespace_root_storage_statistics.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class CreateNamespaceRootStorageStatistics < ActiveRecord::Migration[5.1] + DOWNTIME = false + + def change + create_table :namespace_root_storage_statistics, id: false, primary_key: :namespace_id do |t| + t.integer :namespace_id, null: false, primary_key: true + t.datetime_with_timezone :updated_at, null: false + + t.bigint :repository_size, null: false, default: 0 + t.bigint :lfs_objects_size, null: false, default: 0 + t.bigint :wiki_size, null: false, default: 0 + t.bigint :build_artifacts_size, null: false, default: 0 + t.bigint :storage_size, null: false, default: 0 + t.bigint :packages_size, null: false, default: 0 + + t.index :namespace_id, unique: true + t.foreign_key :namespaces, column: :namespace_id, on_delete: :cascade + end + end +end diff --git a/db/migrate/20190605184422_create_namespace_aggregation_schedules.rb b/db/migrate/20190605184422_create_namespace_aggregation_schedules.rb new file mode 100644 index 00000000000..5e8cb616cc1 --- /dev/null +++ b/db/migrate/20190605184422_create_namespace_aggregation_schedules.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class CreateNamespaceAggregationSchedules < ActiveRecord::Migration[5.1] + DOWNTIME = false + + def change + create_table :namespace_aggregation_schedules, id: false, primary_key: :namespace_id do |t| + t.integer :namespace_id, null: false, primary_key: true + + t.index :namespace_id, unique: true + t.foreign_key :namespaces, column: :namespace_id, on_delete: :cascade + end + end +end diff --git a/db/schema.rb b/db/schema.rb index a94e5142627..192ec8007d9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2055,6 +2055,21 @@ ActiveRecord::Schema.define(version: 20190620112608) do t.index ["title"], name: "index_milestones_on_title_trigram", using: :gin, opclasses: {"title"=>"gin_trgm_ops"} end + create_table "namespace_aggregation_schedules", primary_key: "namespace_id", id: :integer, default: nil, force: :cascade do |t| + t.index ["namespace_id"], name: "index_namespace_aggregation_schedules_on_namespace_id", unique: true, using: :btree + end + + create_table "namespace_root_storage_statistics", primary_key: "namespace_id", id: :integer, default: nil, force: :cascade do |t| + t.datetime_with_timezone "updated_at", null: false + t.bigint "repository_size", default: 0, null: false + t.bigint "lfs_objects_size", default: 0, null: false + t.bigint "wiki_size", default: 0, null: false + t.bigint "build_artifacts_size", default: 0, null: false + t.bigint "storage_size", default: 0, null: false + t.bigint "packages_size", default: 0, null: false + t.index ["namespace_id"], name: "index_namespace_root_storage_statistics_on_namespace_id", unique: true, using: :btree + end + create_table "namespace_statistics", id: :serial, force: :cascade do |t| t.integer "namespace_id", null: false t.integer "shared_runners_seconds", default: 0, null: false @@ -3757,6 +3772,8 @@ ActiveRecord::Schema.define(version: 20190620112608) do add_foreign_key "merge_trains", "users", on_delete: :cascade add_foreign_key "milestones", "namespaces", column: "group_id", name: "fk_95650a40d4", on_delete: :cascade add_foreign_key "milestones", "projects", name: "fk_9bd0a0c791", on_delete: :cascade + add_foreign_key "namespace_aggregation_schedules", "namespaces", on_delete: :cascade + add_foreign_key "namespace_root_storage_statistics", "namespaces", on_delete: :cascade add_foreign_key "namespace_statistics", "namespaces", on_delete: :cascade add_foreign_key "namespaces", "namespaces", column: "custom_project_templates_group_id", name: "fk_e7a0b20a6b", on_delete: :nullify add_foreign_key "namespaces", "plans", name: "fk_fdd12e5b80", on_delete: :nullify diff --git a/spec/factories/namespace/aggregation_schedules.rb b/spec/factories/namespace/aggregation_schedules.rb new file mode 100644 index 00000000000..c172c3360e2 --- /dev/null +++ b/spec/factories/namespace/aggregation_schedules.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :namespace_aggregation_schedules, class: Namespace::AggregationSchedule do + namespace + end +end diff --git a/spec/factories/namespace/root_storage_statistics.rb b/spec/factories/namespace/root_storage_statistics.rb new file mode 100644 index 00000000000..54c5921eb44 --- /dev/null +++ b/spec/factories/namespace/root_storage_statistics.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :namespace_root_storage_statistics, class: Namespace::RootStorageStatistics do + namespace + end +end diff --git a/spec/factories/namespaces.rb b/spec/factories/namespaces.rb index 6feafa5ece9..0cfc6e3aa46 100644 --- a/spec/factories/namespaces.rb +++ b/spec/factories/namespaces.rb @@ -19,5 +19,13 @@ FactoryBot.define do owner.namespace = namespace end end + + trait :with_aggregation_schedule do + association :aggregation_schedule, factory: :namespace_aggregation_schedules + end + + trait :with_root_storage_statistics do + association :root_storage_statistics, factory: :namespace_root_storage_statistics + end end end diff --git a/spec/models/namespace/aggregation_schedule_spec.rb b/spec/models/namespace/aggregation_schedule_spec.rb new file mode 100644 index 00000000000..5ba7547ff4d --- /dev/null +++ b/spec/models/namespace/aggregation_schedule_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Namespace::AggregationSchedule, type: :model do + it { is_expected.to belong_to :namespace } +end diff --git a/spec/models/namespace/root_storage_statistics_spec.rb b/spec/models/namespace/root_storage_statistics_spec.rb new file mode 100644 index 00000000000..f6fb5af5aae --- /dev/null +++ b/spec/models/namespace/root_storage_statistics_spec.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Namespace::RootStorageStatistics, type: :model do + it { is_expected.to belong_to :namespace } + it { is_expected.to have_one(:route).through(:namespace) } + + it { is_expected.to delegate_method(:all_projects).to(:namespace) } +end diff --git a/spec/models/namespace_spec.rb b/spec/models/namespace_spec.rb index d80183af33e..30e49cf204f 100644 --- a/spec/models/namespace_spec.rb +++ b/spec/models/namespace_spec.rb @@ -15,6 +15,8 @@ describe Namespace do it { is_expected.to have_many :project_statistics } it { is_expected.to belong_to :parent } it { is_expected.to have_many :children } + it { is_expected.to have_one :root_storage_statistics } + it { is_expected.to have_one :aggregation_schedule } end describe 'validations' do -- cgit v1.2.1