diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 11:43:02 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-10-20 11:43:02 +0300 |
commit | d9ab72d6080f594d0b3cae15f14b3ef2c6c638cb (patch) | |
tree | 2341ef426af70ad1e289c38036737e04b0aa5007 /lib/gitlab/background_migration | |
parent | d6e514dd13db8947884cd58fe2a9c2a063400a9b (diff) |
Add latest changes from gitlab-org/gitlab@14-4-stable-eev14.4.0-rc42
Diffstat (limited to 'lib/gitlab/background_migration')
4 files changed, 134 insertions, 1 deletions
diff --git a/lib/gitlab/background_migration/fix_first_mentioned_in_commit_at.rb b/lib/gitlab/background_migration/fix_first_mentioned_in_commit_at.rb new file mode 100644 index 00000000000..9b278efaedd --- /dev/null +++ b/lib/gitlab/background_migration/fix_first_mentioned_in_commit_at.rb @@ -0,0 +1,91 @@ +# frozen_string_literal: true + +module Gitlab + module BackgroundMigration + # Class that fixes the incorrectly set authored_date within + # issue_metrics table + class FixFirstMentionedInCommitAt + SUB_BATCH_SIZE = 500 + + # rubocop: disable Style/Documentation + class TmpIssueMetrics < ActiveRecord::Base + include EachBatch + + self.table_name = 'issue_metrics' + + def self.from_2020 + where('EXTRACT(YEAR FROM first_mentioned_in_commit_at) > 2019') + end + end + # rubocop: enable Style/Documentation + + def perform(start_id, end_id) + scope(start_id, end_id).each_batch(of: SUB_BATCH_SIZE, column: :issue_id) do |sub_batch| + first, last = sub_batch.pluck(Arel.sql('min(issue_id), max(issue_id)')).first + + # The query need to be reconstructed because .each_batch modifies the default scope + # See: https://gitlab.com/gitlab-org/gitlab/-/issues/330510 + inner_query = TmpIssueMetrics + .unscoped + .merge(scope(first, last)) + .from("issue_metrics, #{lateral_query}") + .select('issue_metrics.issue_id', 'first_authored_date.authored_date') + .where('issue_metrics.first_mentioned_in_commit_at > first_authored_date.authored_date') + + TmpIssueMetrics.connection.execute <<~UPDATE_METRICS + WITH cte AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} ( + #{inner_query.to_sql} + ) + UPDATE issue_metrics + SET + first_mentioned_in_commit_at = cte.authored_date + FROM + cte + WHERE + cte.issue_id = issue_metrics.issue_id + UPDATE_METRICS + end + + mark_job_as_succeeded(start_id, end_id) + end + + private + + def mark_job_as_succeeded(*arguments) + Gitlab::Database::BackgroundMigrationJob.mark_all_as_succeeded( + 'FixFirstMentionedInCommitAt', + arguments + ) + end + + def scope(start_id, end_id) + TmpIssueMetrics.from_2020.where(issue_id: start_id..end_id) + end + + def lateral_query + <<~SQL + LATERAL ( + SELECT MIN(first_authored_date.authored_date) as authored_date + FROM merge_requests_closing_issues, + LATERAL ( + SELECT id + FROM merge_request_diffs + WHERE merge_request_id = merge_requests_closing_issues.merge_request_id + ORDER BY id DESC + LIMIT 1 + ) last_diff_id, + LATERAL ( + SELECT authored_date + FROM merge_request_diff_commits + WHERE + merge_request_diff_id = last_diff_id.id + ORDER BY relative_order DESC + LIMIT 1 + ) first_authored_date + WHERE merge_requests_closing_issues.issue_id = issue_metrics.issue_id + ) first_authored_date + SQL + end + end + end +end diff --git a/lib/gitlab/background_migration/populate_finding_uuid_for_vulnerability_feedback.rb b/lib/gitlab/background_migration/populate_finding_uuid_for_vulnerability_feedback.rb index dc31f995ae0..909bf10341a 100644 --- a/lib/gitlab/background_migration/populate_finding_uuid_for_vulnerability_feedback.rb +++ b/lib/gitlab/background_migration/populate_finding_uuid_for_vulnerability_feedback.rb @@ -38,7 +38,7 @@ module Gitlab end def vulnerability_finding - BatchLoader.for(finding_key).batch(replace_methods: false) do |finding_keys, loader| + BatchLoader.for(finding_key).batch do |finding_keys, loader| project_ids = finding_keys.map { |key| key[:project_id] } categories = finding_keys.map { |key| key[:category] } fingerprints = finding_keys.map { |key| key[:project_fingerprint] } diff --git a/lib/gitlab/background_migration/populate_status_column_of_security_scans.rb b/lib/gitlab/background_migration/populate_status_column_of_security_scans.rb new file mode 100644 index 00000000000..9740bcaa86b --- /dev/null +++ b/lib/gitlab/background_migration/populate_status_column_of_security_scans.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Gitlab + module BackgroundMigration + class PopulateStatusColumnOfSecurityScans # rubocop:disable Style/Documentation + def perform(_start_id, _end_id) + # no-op + end + end + end +end + +Gitlab::BackgroundMigration::PopulateStatusColumnOfSecurityScans.prepend_mod diff --git a/lib/gitlab/background_migration/populate_topics_total_projects_count_cache.rb b/lib/gitlab/background_migration/populate_topics_total_projects_count_cache.rb new file mode 100644 index 00000000000..1d96872d445 --- /dev/null +++ b/lib/gitlab/background_migration/populate_topics_total_projects_count_cache.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Gitlab + module BackgroundMigration + SUB_BATCH_SIZE = 1_000 + + # The class to populates the total projects counter cache of topics + class PopulateTopicsTotalProjectsCountCache + # Temporary AR model for topics + class Topic < ActiveRecord::Base + include EachBatch + + self.table_name = 'topics' + end + + def perform(start_id, stop_id) + Topic.where(id: start_id..stop_id).each_batch(of: SUB_BATCH_SIZE) do |batch| + ActiveRecord::Base.connection.execute(<<~SQL) + WITH batched_relation AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} (#{batch.select(:id).limit(SUB_BATCH_SIZE).to_sql}) + UPDATE topics + SET total_projects_count = (SELECT COUNT(*) FROM project_topics WHERE topic_id = batched_relation.id) + FROM batched_relation + WHERE topics.id = batched_relation.id + SQL + end + end + end + end +end |