File: background_migrations_controller.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (73 lines) | stat: -rw-r--r-- 2,222 bytes parent folder | download
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
73
# frozen_string_literal: true

module Admin
  class BackgroundMigrationsController < ApplicationController
    feature_category :database
    urgency :low

    around_action :support_multiple_databases

    def index
      @relations_by_tab = {
        'queued' => batched_migration_class.queued.queue_order,
        'finalizing' => batched_migration_class.finalizing.queue_order,
        'failed' => batched_migration_class.with_status(:failed).queue_order,
        'finished' => batched_migration_class.with_status(:finished).queue_order.reverse_order
      }

      @current_tab = @relations_by_tab.key?(safe_params[:tab]) ? safe_params[:tab] : 'queued'
      @migrations = @relations_by_tab[@current_tab].page(pagination_params[:page])
      @successful_rows_counts = batched_migration_class.successful_rows_counts(@migrations.map(&:id))
      @databases = Gitlab::Database.db_config_names(with_schema: :gitlab_shared)
    end

    def show
      @migration = batched_migration_class.find(safe_params[:id])

      @failed_jobs = @migration.batched_jobs.with_status(:failed).page(pagination_params[:page])
    end

    def pause
      migration = batched_migration_class.find(safe_params[:id])
      migration.pause!

      redirect_back fallback_location: { action: 'index' }
    end

    def resume
      migration = batched_migration_class.find(safe_params[:id])
      migration.execute!

      redirect_back fallback_location: { action: 'index' }
    end

    def retry
      migration = batched_migration_class.find(safe_params[:id])
      migration.retry_failed_jobs! if migration.failed?

      redirect_back fallback_location: { action: 'index' }
    end

    private

    def support_multiple_databases
      Gitlab::Database::SharedModel.using_connection(base_model.connection) do
        yield
      end
    end

    def base_model
      @selected_database = safe_params[:database] || Gitlab::Database::MAIN_DATABASE_NAME

      Gitlab::Database.database_base_models[@selected_database]
    end

    def batched_migration_class
      @batched_migration_class ||= Gitlab::Database::BackgroundMigration::BatchedMigration
    end

    def safe_params
      params.permit(:id, :database, :tab)
    end
  end
end