Commit e30842f1 authored by Yannis Roussos's avatar Yannis Roussos

Merge branch '343694-fix-issue-metrics-index-creation-error' into 'master'

Fix issue_metrics index creation error

See merge request gitlab-org/gitlab!72928
parents 50e1ace0 22366a0c
......@@ -6,7 +6,9 @@ class AddTemporaryIndexToIssueMetrics < Gitlab::Database::Migration[1.0]
INDEX_NAME = 'index_issue_metrics_first_mentioned_in_commit'
def up
add_concurrent_index :issue_metrics, :issue_id, where: 'EXTRACT(YEAR FROM first_mentioned_in_commit_at) > 2019', name: INDEX_NAME
condition = Gitlab::BackgroundMigration::FixFirstMentionedInCommitAt::TmpIssueMetrics
.first_mentioned_in_commit_at_condition
add_concurrent_index :issue_metrics, :issue_id, where: condition, name: INDEX_NAME
end
def down
......
......@@ -8,8 +8,8 @@ class ScheduleFixFirstMentionedInCommitAtJob < Gitlab::Database::Migration[1.0]
disable_ddl_transaction!
def up
scope = define_batchable_model('issue_metrics')
.where('EXTRACT(YEAR FROM first_mentioned_in_commit_at) > 2019')
scope = Gitlab::BackgroundMigration::FixFirstMentionedInCommitAt::TmpIssueMetrics
.from_2020
queue_background_migration_jobs_by_range_at_intervals(
scope,
......
......@@ -14,7 +14,15 @@ module Gitlab
self.table_name = 'issue_metrics'
def self.from_2020
where('EXTRACT(YEAR FROM first_mentioned_in_commit_at) > 2019')
where(first_mentioned_in_commit_at_condition)
end
def self.first_mentioned_in_commit_at_condition
if columns_hash['first_mentioned_in_commit_at'].sql_type == 'timestamp without time zone'
'EXTRACT(YEAR FROM first_mentioned_in_commit_at) > 2019'
else
"EXTRACT(YEAR FROM first_mentioned_in_commit_at at time zone 'UTC') > 2019"
end
end
end
# rubocop: enable Style/Documentation
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20211004110500_add_temporary_index_to_issue_metrics.rb')
RSpec.describe Gitlab::BackgroundMigration::FixFirstMentionedInCommitAt, :migration, schema: 20211004110500 do
let(:namespaces) { table(:namespaces) }
......@@ -99,6 +100,7 @@ RSpec.describe Gitlab::BackgroundMigration::FixFirstMentionedInCommitAt, :migrat
.perform(issue_metrics.minimum(:issue_id), issue_metrics.maximum(:issue_id))
end
shared_examples 'fixes first_mentioned_in_commit_at' do
it "marks successful slices as completed" do
min_issue_id = issue_metrics.minimum(:issue_id)
max_issue_id = issue_metrics.maximum(:issue_id)
......@@ -137,4 +139,28 @@ RSpec.describe Gitlab::BackgroundMigration::FixFirstMentionedInCommitAt, :migrat
expect { run_migration }.not_to change { record.reload.first_mentioned_in_commit_at }
end
end
end
describe 'running the migration when first_mentioned_in_commit_at is timestamp without time zone' do
it_behaves_like 'fixes first_mentioned_in_commit_at'
end
describe 'running the migration when first_mentioned_in_commit_at is timestamp with time zone' do
around do |example|
AddTemporaryIndexToIssueMetrics.new.down
ActiveRecord::Base.connection.execute "ALTER TABLE issue_metrics ALTER first_mentioned_in_commit_at type timestamp with time zone"
Gitlab::BackgroundMigration::FixFirstMentionedInCommitAt::TmpIssueMetrics.reset_column_information
AddTemporaryIndexToIssueMetrics.new.up
example.run
AddTemporaryIndexToIssueMetrics.new.down
ActiveRecord::Base.connection.execute "ALTER TABLE issue_metrics ALTER first_mentioned_in_commit_at type timestamp without time zone"
Gitlab::BackgroundMigration::FixFirstMentionedInCommitAt::TmpIssueMetrics.reset_column_information
AddTemporaryIndexToIssueMetrics.new.up
end
it_behaves_like 'fixes first_mentioned_in_commit_at'
end
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