Commit f229df90 authored by Patrick Bajao's avatar Patrick Bajao

Add merge_request_cleanup_schedules table

This table has the `scheduled_at` and `completed_at` columns
which will be populated when a MR is scheduled to be cleaned up
and when the clean up is done.
parent 2e3ed169
...@@ -49,6 +49,7 @@ class MergeRequest < ApplicationRecord ...@@ -49,6 +49,7 @@ class MergeRequest < ApplicationRecord
has_one :merge_request_diff, has_one :merge_request_diff,
-> { order('merge_request_diffs.id DESC') }, inverse_of: :merge_request -> { order('merge_request_diffs.id DESC') }, inverse_of: :merge_request
has_one :cleanup_schedule, inverse_of: :merge_request
belongs_to :latest_merge_request_diff, class_name: 'MergeRequestDiff' belongs_to :latest_merge_request_diff, class_name: 'MergeRequestDiff'
manual_inverse_association :latest_merge_request_diff, :merge_request manual_inverse_association :latest_merge_request_diff, :merge_request
......
# frozen_string_literal: true
class MergeRequest::CleanupSchedule < ApplicationRecord
belongs_to :merge_request, inverse_of: :cleanup_schedule
end
# frozen_string_literal: true
class CreateMergeRequestCleanupSchedules < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
with_lock_retries do
create_table :merge_request_cleanup_schedules, id: false do |t|
t.references :merge_request, primary_key: true, index: { unique: true }, null: false, foreign_key: { on_delete: :cascade }
t.datetime_with_timezone :scheduled_at, null: false
t.datetime_with_timezone :completed_at, null: true
t.timestamps_with_timezone
t.index :scheduled_at, where: 'completed_at IS NULL', name: 'index_mr_cleanup_schedules_timestamps'
end
end
end
def down
with_lock_retries do
drop_table :merge_request_cleanup_schedules
end
end
end
f4fb466c74e1366d5980a54d6e5fc42fe78237cae33d8cdaf5573d2fe75f8a5a
\ No newline at end of file
...@@ -13459,6 +13459,23 @@ CREATE SEQUENCE merge_request_blocks_id_seq ...@@ -13459,6 +13459,23 @@ CREATE SEQUENCE merge_request_blocks_id_seq
ALTER SEQUENCE merge_request_blocks_id_seq OWNED BY merge_request_blocks.id; ALTER SEQUENCE merge_request_blocks_id_seq OWNED BY merge_request_blocks.id;
CREATE TABLE merge_request_cleanup_schedules (
merge_request_id bigint NOT NULL,
scheduled_at timestamp with time zone NOT NULL,
completed_at timestamp with time zone,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL
);
CREATE SEQUENCE merge_request_cleanup_schedules_merge_request_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE merge_request_cleanup_schedules_merge_request_id_seq OWNED BY merge_request_cleanup_schedules.merge_request_id;
CREATE TABLE merge_request_context_commit_diff_files ( CREATE TABLE merge_request_context_commit_diff_files (
sha bytea NOT NULL, sha bytea NOT NULL,
relative_order integer NOT NULL, relative_order integer NOT NULL,
...@@ -17928,6 +17945,8 @@ ALTER TABLE ONLY merge_request_assignees ALTER COLUMN id SET DEFAULT nextval('me ...@@ -17928,6 +17945,8 @@ ALTER TABLE ONLY merge_request_assignees ALTER COLUMN id SET DEFAULT nextval('me
ALTER TABLE ONLY merge_request_blocks ALTER COLUMN id SET DEFAULT nextval('merge_request_blocks_id_seq'::regclass); ALTER TABLE ONLY merge_request_blocks ALTER COLUMN id SET DEFAULT nextval('merge_request_blocks_id_seq'::regclass);
ALTER TABLE ONLY merge_request_cleanup_schedules ALTER COLUMN merge_request_id SET DEFAULT nextval('merge_request_cleanup_schedules_merge_request_id_seq'::regclass);
ALTER TABLE ONLY merge_request_context_commits ALTER COLUMN id SET DEFAULT nextval('merge_request_context_commits_id_seq'::regclass); ALTER TABLE ONLY merge_request_context_commits ALTER COLUMN id SET DEFAULT nextval('merge_request_context_commits_id_seq'::regclass);
ALTER TABLE ONLY merge_request_diff_details ALTER COLUMN merge_request_diff_id SET DEFAULT nextval('merge_request_diff_details_merge_request_diff_id_seq'::regclass); ALTER TABLE ONLY merge_request_diff_details ALTER COLUMN merge_request_diff_id SET DEFAULT nextval('merge_request_diff_details_merge_request_diff_id_seq'::regclass);
...@@ -19140,6 +19159,9 @@ ALTER TABLE ONLY merge_request_assignees ...@@ -19140,6 +19159,9 @@ ALTER TABLE ONLY merge_request_assignees
ALTER TABLE ONLY merge_request_blocks ALTER TABLE ONLY merge_request_blocks
ADD CONSTRAINT merge_request_blocks_pkey PRIMARY KEY (id); ADD CONSTRAINT merge_request_blocks_pkey PRIMARY KEY (id);
ALTER TABLE ONLY merge_request_cleanup_schedules
ADD CONSTRAINT merge_request_cleanup_schedules_pkey PRIMARY KEY (merge_request_id);
ALTER TABLE ONLY merge_request_context_commits ALTER TABLE ONLY merge_request_context_commits
ADD CONSTRAINT merge_request_context_commits_pkey PRIMARY KEY (id); ADD CONSTRAINT merge_request_context_commits_pkey PRIMARY KEY (id);
...@@ -21076,6 +21098,8 @@ CREATE INDEX index_merge_request_assignees_on_user_id ON merge_request_assignees ...@@ -21076,6 +21098,8 @@ CREATE INDEX index_merge_request_assignees_on_user_id ON merge_request_assignees
CREATE INDEX index_merge_request_blocks_on_blocked_merge_request_id ON merge_request_blocks USING btree (blocked_merge_request_id); CREATE INDEX index_merge_request_blocks_on_blocked_merge_request_id ON merge_request_blocks USING btree (blocked_merge_request_id);
CREATE UNIQUE INDEX index_merge_request_cleanup_schedules_on_merge_request_id ON merge_request_cleanup_schedules USING btree (merge_request_id);
CREATE INDEX index_merge_request_diff_commits_on_sha ON merge_request_diff_commits USING btree (sha); CREATE INDEX index_merge_request_diff_commits_on_sha ON merge_request_diff_commits USING btree (sha);
CREATE INDEX index_merge_request_diff_details_on_merge_request_diff_id ON merge_request_diff_details USING btree (merge_request_diff_id); CREATE INDEX index_merge_request_diff_details_on_merge_request_diff_id ON merge_request_diff_details USING btree (merge_request_diff_id);
...@@ -21186,6 +21210,8 @@ CREATE INDEX index_mirror_data_on_next_execution_and_retry_count ON project_mirr ...@@ -21186,6 +21210,8 @@ CREATE INDEX index_mirror_data_on_next_execution_and_retry_count ON project_mirr
CREATE UNIQUE INDEX index_mr_blocks_on_blocking_and_blocked_mr_ids ON merge_request_blocks USING btree (blocking_merge_request_id, blocked_merge_request_id); CREATE UNIQUE INDEX index_mr_blocks_on_blocking_and_blocked_mr_ids ON merge_request_blocks USING btree (blocking_merge_request_id, blocked_merge_request_id);
CREATE INDEX index_mr_cleanup_schedules_timestamps ON merge_request_cleanup_schedules USING btree (scheduled_at) WHERE (completed_at IS NULL);
CREATE UNIQUE INDEX index_mr_context_commits_on_merge_request_id_and_sha ON merge_request_context_commits USING btree (merge_request_id, sha); CREATE UNIQUE INDEX index_mr_context_commits_on_merge_request_id_and_sha ON merge_request_context_commits USING btree (merge_request_id, sha);
CREATE UNIQUE INDEX index_namespace_aggregation_schedules_on_namespace_id ON namespace_aggregation_schedules USING btree (namespace_id); CREATE UNIQUE INDEX index_namespace_aggregation_schedules_on_namespace_id ON namespace_aggregation_schedules USING btree (namespace_id);
...@@ -23947,6 +23973,9 @@ ALTER TABLE ONLY project_error_tracking_settings ...@@ -23947,6 +23973,9 @@ ALTER TABLE ONLY project_error_tracking_settings
ALTER TABLE ONLY list_user_preferences ALTER TABLE ONLY list_user_preferences
ADD CONSTRAINT fk_rails_916d72cafd FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_916d72cafd FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE ONLY merge_request_cleanup_schedules
ADD CONSTRAINT fk_rails_92dd0e705c FOREIGN KEY (merge_request_id) REFERENCES merge_requests(id) ON DELETE CASCADE;
ALTER TABLE ONLY board_labels ALTER TABLE ONLY board_labels
ADD CONSTRAINT fk_rails_9374a16edd FOREIGN KEY (board_id) REFERENCES boards(id) ON DELETE CASCADE; ADD CONSTRAINT fk_rails_9374a16edd FOREIGN KEY (board_id) REFERENCES boards(id) ON DELETE CASCADE;
......
...@@ -179,6 +179,7 @@ merge_requests: ...@@ -179,6 +179,7 @@ merge_requests:
- user_mentions - user_mentions
- system_note_metadata - system_note_metadata
- note_authors - note_authors
- cleanup_schedule
external_pull_requests: external_pull_requests:
- project - project
merge_request_diff: merge_request_diff:
...@@ -195,6 +196,8 @@ merge_request_diff_files: ...@@ -195,6 +196,8 @@ merge_request_diff_files:
merge_request_context_commits: merge_request_context_commits:
- merge_request - merge_request
- diff_files - diff_files
cleanup_schedule:
- merge_request
ci_pipelines: ci_pipelines:
- project - project
- user - user
......
...@@ -877,3 +877,9 @@ PushRule: ...@@ -877,3 +877,9 @@ PushRule:
- reject_unsigned_commits - reject_unsigned_commits
- commit_committer_check - commit_committer_check
- regexp_uses_re2 - regexp_uses_re2
MergeRequest::CleanupSchedule:
- id
- scheduled_at
- completed_at
- created_at
- updated_at
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe MergeRequest::CleanupSchedule do
describe 'associations' do
it { is_expected.to belong_to(:merge_request) }
end
end
...@@ -30,6 +30,7 @@ RSpec.describe MergeRequest, factory_default: :keep do ...@@ -30,6 +30,7 @@ RSpec.describe MergeRequest, factory_default: :keep do
it { is_expected.to have_many(:resource_state_events) } it { is_expected.to have_many(:resource_state_events) }
it { is_expected.to have_many(:draft_notes) } it { is_expected.to have_many(:draft_notes) }
it { is_expected.to have_many(:reviews).inverse_of(:merge_request) } it { is_expected.to have_many(:reviews).inverse_of(:merge_request) }
it { is_expected.to have_one(:cleanup_schedule).inverse_of(:merge_request) }
context 'for forks' do context 'for forks' do
let!(:project) { create(:project) } let!(:project) { create(:project) }
......
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