Commit 5cf76283 authored by Alex Kalderimis's avatar Alex Kalderimis

Merge branch 'add-prpoject_id-to-timelogs' into 'master'

Add project ID to timelogs

See merge request gitlab-org/gitlab!60040
parents 3cb9c129 75b3361e
......@@ -367,6 +367,8 @@ class Project < ApplicationRecord
has_one :operations_feature_flags_client, class_name: 'Operations::FeatureFlagsClient'
has_many :operations_feature_flags_user_lists, class_name: 'Operations::FeatureFlags::UserList'
has_many :timelogs
accepts_nested_attributes_for :variables, allow_destroy: true
accepts_nested_attributes_for :project_feature, update_only: true
accepts_nested_attributes_for :project_setting, update_only: true
......@@ -615,7 +617,7 @@ class Project < ApplicationRecord
mount_uploader :bfg_object_map, AttachmentUploader
def self.with_api_entity_associations
preload(:project_feature, :route, :tags, :group, namespace: [:route, :owner])
preload(:project_feature, :route, :tags, :group, :timelogs, namespace: [:route, :owner])
end
def self.with_web_entity_associations
......
......@@ -3,11 +3,14 @@
class Timelog < ApplicationRecord
include Importable
before_save :set_project
validates :time_spent, :user, presence: true
validate :issuable_id_is_present, unless: :importing?
belongs_to :issue, touch: true
belongs_to :merge_request, touch: true
belongs_to :project
belongs_to :user
belongs_to :note
......@@ -37,6 +40,10 @@ class Timelog < ApplicationRecord
end
end
def set_project
self.project_id = issuable.project_id
end
# Rails5 defaults to :touch_later, overwrite for normal touch
def belongs_to_touch_method
:touch
......
---
title: Add project_id foreign key to timelogs
merge_request: 60040
author: Lee Tickett @leetickett
type: added
# frozen_string_literal: true
class AddProjectToTimelogs < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
with_lock_retries do
add_column :timelogs, :project_id, :integer
end
end
def down
with_lock_retries do
remove_column :timelogs, :project_id
end
end
end
# frozen_string_literal: true
class AddProjectIdFkToTimelogs < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
INDEX_NAME = 'index_timelogs_on_project_id_and_spent_at'
disable_ddl_transaction!
def up
add_concurrent_index :timelogs, [:project_id, :spent_at], name: INDEX_NAME
add_concurrent_foreign_key :timelogs, :projects, column: :project_id, on_delete: :cascade
end
def down
with_lock_retries do
remove_foreign_key_if_exists :timelogs, column: :project_id
end
remove_concurrent_index_by_name :timelogs, INDEX_NAME
end
end
870589d3a4b4bc139ac29b0d87b0f9e777de21e854e5692c0dedd6683c83649a
\ No newline at end of file
808e4c1b0bb4f44afea57cce84820ef1371ae852d7cbc79ef454c04219ea956d
\ No newline at end of file
......@@ -18015,7 +18015,8 @@ CREATE TABLE timelogs (
issue_id integer,
merge_request_id integer,
spent_at timestamp without time zone,
note_id integer
note_id integer,
project_id integer
);
CREATE SEQUENCE timelogs_id_seq
......@@ -24149,6 +24150,8 @@ CREATE INDEX index_timelogs_on_merge_request_id ON timelogs USING btree (merge_r
CREATE INDEX index_timelogs_on_note_id ON timelogs USING btree (note_id);
CREATE INDEX index_timelogs_on_project_id_and_spent_at ON timelogs USING btree (project_id, spent_at);
CREATE INDEX index_timelogs_on_spent_at ON timelogs USING btree (spent_at) WHERE (spent_at IS NOT NULL);
CREATE INDEX index_timelogs_on_user_id ON timelogs USING btree (user_id);
......@@ -25324,6 +25327,9 @@ ALTER TABLE ONLY geo_event_log
ALTER TABLE ONLY vulnerability_exports
ADD CONSTRAINT fk_c3d3cb5d0f FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE;
ALTER TABLE ONLY timelogs
ADD CONSTRAINT fk_c49c83dd77 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
ALTER TABLE ONLY geo_event_log
ADD CONSTRAINT fk_c4b1c1f66e FOREIGN KEY (repository_deleted_event_id) REFERENCES geo_repository_deleted_events(id) ON DELETE CASCADE;
......@@ -568,6 +568,7 @@ project:
- debian_distributions
- merge_request_metrics
- security_orchestration_policy_configuration
- timelogs
award_emoji:
- awardable
- user
......
......@@ -640,6 +640,7 @@ Timelog:
- time_spent
- merge_request_id
- user_id
- project_id
- spent_at
- created_at
- updated_at
......
......@@ -130,6 +130,7 @@ RSpec.describe Project, factory_default: :keep do
it { is_expected.to have_many(:debian_distributions).class_name('Packages::Debian::ProjectDistribution').dependent(:destroy) }
it { is_expected.to have_many(:pipeline_artifacts) }
it { is_expected.to have_many(:terraform_states).class_name('Terraform::State').inverse_of(:project) }
it { is_expected.to have_many(:timelogs) }
# GitLab Pages
it { is_expected.to have_many(:pages_domains) }
......
......@@ -3,11 +3,12 @@
require 'spec_helper'
RSpec.describe Timelog do
subject { build(:timelog) }
subject { create(:timelog) }
let(:issue) { create(:issue) }
let(:merge_request) { create(:merge_request) }
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:issue).touch(true) }
it { is_expected.to belong_to(:merge_request).touch(true) }
......@@ -16,6 +17,8 @@ RSpec.describe Timelog do
it { is_expected.to validate_presence_of(:time_spent) }
it { is_expected.to validate_presence_of(:user) }
it { expect(subject.project_id).not_to be_nil }
describe 'Issuable validation' do
it 'is invalid if issue_id and merge_request_id are missing' do
subject.attributes = { issue: nil, merge_request: nil }
......
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