Commit bb0df486 authored by Lee Tickett's avatar Lee Tickett Committed by Steve Abrams

Backfill / update timelogs.spent_at where NULL

parent 7499e720
# frozen_string_literal: true
class ScheduleUpdateTimelogsNullSpentAt < Gitlab::Database::Migration[1.0]
DOWNTIME = false
BATCH_SIZE = 5_000
DELAY_INTERVAL = 2.minutes
MIGRATION = 'UpdateTimelogsNullSpentAt'
disable_ddl_transaction!
def up
queue_background_migration_jobs_by_range_at_intervals(
define_batchable_model('timelogs').where(spent_at: nil),
MIGRATION,
DELAY_INTERVAL,
batch_size: BATCH_SIZE
)
end
def down
# no-op
end
end
43ae6290e11e3944b23ce2865b5c466a29c9ba3cfd2e0b58bd834568414b5bf2
\ No newline at end of file
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# Class to populate spent_at for timelogs
class UpdateTimelogsNullSpentAt
include Gitlab::Database::DynamicModelHelpers
BATCH_SIZE = 100
def perform(start_id, stop_id)
define_batchable_model('timelogs').where(spent_at: nil, id: start_id..stop_id).each_batch(of: 100) do |subbatch|
batch_start, batch_end = subbatch.pluck('min(id), max(id)').first
update_timelogs(batch_start, batch_end)
end
end
def update_timelogs(batch_start, batch_stop)
execute(<<~SQL)
UPDATE timelogs
SET spent_at = created_at
WHERE spent_at IS NULL
AND timelogs.id BETWEEN #{batch_start} AND #{batch_stop};
SQL
end
def execute(sql)
@connection ||= ::ActiveRecord::Base.connection
@connection.execute(sql)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::UpdateTimelogsNullSpentAt, schema: 20211215090620 do
let_it_be(:previous_time) { 10.days.ago }
let_it_be(:namespace) { table(:namespaces).create!(name: 'namespace', path: 'namespace') }
let_it_be(:project) { table(:projects).create!(namespace_id: namespace.id) }
let_it_be(:issue) { table(:issues).create!(project_id: project.id) }
let_it_be(:merge_request) { table(:merge_requests).create!(target_project_id: project.id, source_branch: 'master', target_branch: 'feature') }
let_it_be(:timelog1) { create_timelog!(issue_id: issue.id) }
let_it_be(:timelog2) { create_timelog!(merge_request_id: merge_request.id) }
let_it_be(:timelog3) { create_timelog!(issue_id: issue.id, spent_at: previous_time) }
let_it_be(:timelog4) { create_timelog!(merge_request_id: merge_request.id, spent_at: previous_time) }
subject(:background_migration) { described_class.new }
before_all do
table(:timelogs).where.not(id: [timelog3.id, timelog4.id]).update_all(spent_at: nil)
end
describe '#perform' do
it 'sets correct spent_at' do
background_migration.perform(timelog1.id, timelog4.id)
expect(timelog1.reload.spent_at).to be_like_time(timelog1.created_at)
expect(timelog2.reload.spent_at).to be_like_time(timelog2.created_at)
expect(timelog3.reload.spent_at).to be_like_time(previous_time)
expect(timelog4.reload.spent_at).to be_like_time(previous_time)
expect(timelog3.reload.spent_at).not_to be_like_time(timelog3.created_at)
expect(timelog4.reload.spent_at).not_to be_like_time(timelog4.created_at)
end
end
private
def create_timelog!(**args)
table(:timelogs).create!(**args, time_spent: 1)
end
end
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe ScheduleUpdateTimelogsNullSpentAt do
let_it_be(:namespace) { table(:namespaces).create!(name: 'namespace', path: 'namespace') }
let_it_be(:project) { table(:projects).create!(namespace_id: namespace.id) }
let_it_be(:issue) { table(:issues).create!(project_id: project.id) }
let_it_be(:merge_request) { table(:merge_requests).create!(target_project_id: project.id, source_branch: 'master', target_branch: 'feature') }
let_it_be(:timelog1) { create_timelog!(merge_request_id: merge_request.id) }
let_it_be(:timelog2) { create_timelog!(merge_request_id: merge_request.id) }
let_it_be(:timelog3) { create_timelog!(merge_request_id: merge_request.id) }
let_it_be(:timelog4) { create_timelog!(issue_id: issue.id) }
let_it_be(:timelog5) { create_timelog!(issue_id: issue.id) }
before_all do
table(:timelogs).where.not(id: timelog3.id).update_all(spent_at: nil)
end
it 'correctly schedules background migrations' do
stub_const("#{described_class}::BATCH_SIZE", 2)
Sidekiq::Testing.fake! do
freeze_time do
migrate!
expect(described_class::MIGRATION)
.to be_scheduled_delayed_migration(2.minutes, timelog1.id, timelog2.id)
expect(described_class::MIGRATION)
.to be_scheduled_delayed_migration(4.minutes, timelog4.id, timelog5.id)
expect(BackgroundMigrationWorker.jobs.size).to eq(2)
end
end
end
private
def create_timelog!(**args)
table(:timelogs).create!(**args, time_spent: 1)
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