summaryrefslogtreecommitdiff
path: root/db/migrate/20201027002551_migrate_services_to_http_integrations.rb
blob: 5fe053208624a80416d5cce43758b6bf4b411066 (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
# frozen_string_literal: true

class MigrateServicesToHttpIntegrations < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  ALERT_SERVICE_TYPE = 'AlertsService'
  SERVICE_NAMES_IDENTIFIER = {
    name: 'HTTP endpoint',
    identifier: 'legacy'
  }

  class HttpIntegration < ActiveRecord::Base
    self.table_name = 'alert_management_http_integrations'
  end

  # For each Alerts service,
  # Create the matching HttpIntegration
  def up
    HttpIntegration.reset_column_information

    sql = <<~SQL
      SELECT * FROM services
      JOIN alerts_service_data
      ON (services.id = alerts_service_data.service_id)
      WHERE type = '#{ALERT_SERVICE_TYPE}'
      AND active = TRUE
    SQL

    current_time = Time.current

    values = select_all(sql).map do |alerts_service|
      {
        project_id: alerts_service['project_id'],
        name: SERVICE_NAMES_IDENTIFIER[:name],
        endpoint_identifier: SERVICE_NAMES_IDENTIFIER[:identifier],
        encrypted_token: alerts_service['encrypted_token'],
        encrypted_token_iv: alerts_service['encrypted_token_iv'],
        active: alerts_service['active'],
        updated_at: current_time,
        created_at: current_time
      }
    end

    HttpIntegration.insert_all(values) if values.present?
  end

  def down
    # no-op
  end
end