Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20221006172302_adjust_task_note_rename_background_migration_values.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2af16fb6d3c072f41a6f740e455566261bc5516a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

class AdjustTaskNoteRenameBackgroundMigrationValues < Gitlab::Database::Migration[2.0]
  restrict_gitlab_migration gitlab_schema: :gitlab_main

  JOB_CLASS_NAME = 'RenameTaskSystemNoteToChecklistItem'
  MIGRATION_FAILED_STATUS = 4
  MIGRATION_FINISHED_STATUS = 3
  MIGRATION_ACTIVE_STATUS = 1
  JOB_FAILED_STATUS = 2

  OLD_BATCH_SIZE = 10_000
  NEW_BATCH_SIZE = 5_000

  OLD_SUB_BATCH_SIZE = 100
  NEW_SUB_BATCH_SIZE = 10

  class InlineBatchedMigration < MigrationRecord
    self.table_name = :batched_background_migrations

    scope :for_configuration, ->(job_class_name, table_name, column_name, job_arguments) do
      where(job_class_name: job_class_name, table_name: table_name, column_name: column_name)
        .where("job_arguments = ?", job_arguments.to_json) # rubocop:disable Rails/WhereEquals
    end
  end

  class InlineBatchedJob < MigrationRecord
    include EachBatch
    self.table_name = :batched_background_migration_jobs
  end

  def up
    migration = InlineBatchedMigration.for_configuration(
      JOB_CLASS_NAME,
      :system_note_metadata,
      :id,
      []
    ).first
    return if migration.blank? || migration.status == MIGRATION_FINISHED_STATUS

    InlineBatchedJob.where(
      batched_background_migration_id: migration.id,
      status: JOB_FAILED_STATUS
    ).each_batch(of: 100) do |batch|
      batch.update_all(attempts: 0, sub_batch_size: NEW_SUB_BATCH_SIZE)
    end

    update_params = { batch_size: NEW_BATCH_SIZE, sub_batch_size: NEW_SUB_BATCH_SIZE }

    if migration.status == MIGRATION_FAILED_STATUS
      update_params[:status] = MIGRATION_ACTIVE_STATUS
      update_params[:started_at] = Time.zone.now if migration.respond_to?(:started_at)
    end

    migration.update!(**update_params)
  end

  def down
    migration = InlineBatchedMigration.for_configuration(
      JOB_CLASS_NAME,
      :system_note_metadata,
      :id,
      []
    ).first
    return if migration.blank?

    migration.update!(
      batch_size: OLD_BATCH_SIZE,
      sub_batch_size: OLD_SUB_BATCH_SIZE
    )
  end
end