summaryrefslogtreecommitdiff
path: root/lib/api/admin/batched_background_migrations.rb
blob: e8cc08a23be86bb678f70c7ee36f62f5e45a0ff5 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# frozen_string_literal: true

module API
  module Admin
    class BatchedBackgroundMigrations < ::API::Base
      feature_category :database
      urgency :low

      before do
        authenticated_as_admin!
      end

      namespace 'admin' do
        resources 'batched_background_migrations/:id' do
          desc 'Retrieve a batched background migration'
          params do
            optional :database,
              type: String,
              values: Gitlab::Database.all_database_names,
              desc: 'The name of the database',
              default: 'main'
            requires :id,
              type: Integer,
              desc: 'The batched background migration id'
          end
          get do
            Gitlab::Database::SharedModel.using_connection(base_model.connection) do
              present_entity(batched_background_migration)
            end
          end
        end

        resources 'batched_background_migrations' do
          desc 'Get the list of the batched background migrations'
          params do
            optional :database,
              type: String,
              values: Gitlab::Database.all_database_names,
              desc: 'The name of the database, the default `main`',
              default: 'main'
          end
          get do
            Gitlab::Database::SharedModel.using_connection(base_model.connection) do
              migrations = Database::BatchedBackgroundMigrationsFinder.new(connection: base_model.connection).execute
              present_entity(migrations)
            end
          end
        end

        resources 'batched_background_migrations/:id/resume' do
          desc 'Resume a batched background migration'
          params do
            optional :database,
              type: String,
              values: Gitlab::Database.all_database_names,
              desc: 'The name of the database',
              default: 'main'
            requires :id,
              type: Integer,
              desc: 'The batched background migration id'
          end
          put do
            Gitlab::Database::SharedModel.using_connection(base_model.connection) do
              unless batched_background_migration.paused?
                msg = 'You can resume only `paused` batched background migrations.'
                render_api_error!(msg, 422)
              end

              batched_background_migration.execute!
              present_entity(batched_background_migration)
            end
          end
        end

        resources 'batched_background_migrations/:id/pause' do
          desc 'Pause a batched background migration'
          params do
            optional :database,
              type: String,
              values: Gitlab::Database.all_database_names,
              desc: 'The name of the database',
              default: 'main'
            requires :id,
              type: Integer,
              desc: 'The batched background migration id'
          end
          put do
            Gitlab::Database::SharedModel.using_connection(base_model.connection) do
              unless batched_background_migration.active?
                msg = 'You can pause only `active` batched background migrations.'
                render_api_error!(msg, 422)
              end

              batched_background_migration.pause!
              present_entity(batched_background_migration)
            end
          end
        end
      end

      helpers do
        def batched_background_migration
          @batched_background_migration ||= Gitlab::Database::BackgroundMigration::BatchedMigration.find(params[:id])
        end

        def base_model
          database = params[:database] || Gitlab::Database::MAIN_DATABASE_NAME
          @base_model ||= Gitlab::Database.database_base_models[database]
        end

        def present_entity(result)
          present result,
            with: ::API::Entities::BatchedBackgroundMigration
        end
      end
    end
  end
end