Commit 7a70b2e2 authored by Mikolaj Wawrzyniak's avatar Mikolaj Wawrzyniak

Implement revet action for prometheus migration

parent 5fc71952
# frozen_string_literal: true # frozen_string_literal: true
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migration[5.2] class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
...@@ -19,6 +16,7 @@ class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migrat ...@@ -19,6 +16,7 @@ class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migrat
self.table_name = 'clusters_applications_prometheus' self.table_name = 'clusters_applications_prometheus'
enum status: { enum status: {
errored: -1,
installed: 3, installed: 3,
updated: 5 updated: 5
} }
...@@ -56,6 +54,25 @@ class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migrat ...@@ -56,6 +54,25 @@ class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migrat
AND clusters_applications_prometheus.status IN (#{Applications::Prometheus.statuses[:installed]}, #{Applications::Prometheus.statuses[:updated]})").exists? AND clusters_applications_prometheus.status IN (#{Applications::Prometheus.statuses[:installed]}, #{Applications::Prometheus.statuses[:updated]})").exists?
end end
end end
class PrometheusService < ActiveRecord::Base
self.inheritance_column = :_type_disabled
self.table_name = 'services'
default_scope { where("services.type = 'PrometheusService'") }
scope :managed, -> { where("services.properties = '{}'") }
scope :not_active, -> { where.not(active: true) }
scope :not_template, -> { where.not('services.template') }
scope :join_applications, -> {
joins('LEFT JOIN projects ON projects.id = services.project_id')
.joins('LEFT JOIN namespaces ON namespaces.id = projects.namespace_id')
.joins('LEFT JOIN cluster_groups ON cluster_groups.group_id = namespaces.id')
.joins("LEFT JOIN clusters ON clusters.cluster_type = #{Cluster.cluster_types['instance_type']} OR
clusters.id = cluster_groups.cluster_id AND clusters.cluster_type = #{Cluster.cluster_types['group_type']}")
.joins('LEFT JOIN clusters_applications_prometheus ON clusters_applications_prometheus.cluster_id = clusters.id')
}
end
end end
def up def up
...@@ -67,7 +84,9 @@ class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migrat ...@@ -67,7 +84,9 @@ class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migrat
end end
def down def down
# no-op Migratable::PrometheusService.managed.not_template.not_active.delete_all
Migratable::PrometheusService.managed.not_template.where(project_id: services_without_active_application.select('project_id')).delete_all
clear_duplicates
end end
private private
...@@ -88,4 +107,20 @@ class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migrat ...@@ -88,4 +107,20 @@ class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migrat
@migrate_instance_cluster = Migratable::Cluster.instance_type.has_prometheus_application? @migrate_instance_cluster = Migratable::Cluster.instance_type.has_prometheus_application?
end end
end end
def services_without_active_application
Migratable::PrometheusService
.join_applications
.managed
.not_template
.group('project_id')
.having("NOT bool_or(COALESCE(clusters_applications_prometheus.status, #{Migratable::Applications::Prometheus.statuses[:errored]})
IN (#{Migratable::Applications::Prometheus.statuses[:installed]}, #{Migratable::Applications::Prometheus.statuses[:updated]}))")
end
def clear_duplicates
subquery = Migratable::PrometheusService.managed.not_template.select("id, ROW_NUMBER() OVER(PARTITION BY project_id ORDER BY project_id) AS row_num").to_sql
duplicates_filter = "id in (SELECT id FROM (#{subquery}) t WHERE t.row_num > 1)"
Migratable::PrometheusService.where(duplicates_filter).delete_all
end
end end
...@@ -25,5 +25,11 @@ module MigrationHelpers ...@@ -25,5 +25,11 @@ module MigrationHelpers
deployment_events: false deployment_events: false
}.merge(params) }.merge(params)
end end
def row_attributes(entity)
entity.attributes.with_indifferent_access.tap do |hash|
hash.merge!(hash.slice(:created_at, :updated_at).transform_values { |v| v.to_s(:db) })
end
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment