summaryrefslogtreecommitdiff
path: root/db/migrate/20201027002551_migrate_services_to_http_integrations.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrate/20201027002551_migrate_services_to_http_integrations.rb')
-rw-r--r--db/migrate/20201027002551_migrate_services_to_http_integrations.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/db/migrate/20201027002551_migrate_services_to_http_integrations.rb b/db/migrate/20201027002551_migrate_services_to_http_integrations.rb
new file mode 100644
index 00000000000..5fe05320862
--- /dev/null
+++ b/db/migrate/20201027002551_migrate_services_to_http_integrations.rb
@@ -0,0 +1,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