Commit 63213051 authored by Mikolaj Wawrzyniak's avatar Mikolaj Wawrzyniak

Make patch prometheus migration no-op

Due to lack of right revert strategy for
background migrations it was decided to
turn this migrations in to no-op
parent 7a70b2e2
......@@ -54,25 +54,6 @@ class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migrat
AND clusters_applications_prometheus.status IN (#{Applications::Prometheus.statuses[:installed]}, #{Applications::Prometheus.statuses[:updated]})").exists?
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
def up
......@@ -84,16 +65,13 @@ class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migrat
end
def down
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
# no-op
end
private
def projects_without_active_prometheus_service
scope = Migratable::Project
.without_active_prometheus_services
scope = Migratable::Project.without_active_prometheus_services
return scope if migrate_instance_cluster?
......@@ -107,20 +85,4 @@ class PatchPrometheusServicesForSharedClusterApplications < ActiveRecord::Migrat
@migrate_instance_cluster = Migratable::Cluster.instance_type.has_prometheus_application?
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
......@@ -131,42 +131,4 @@ describe PatchPrometheusServicesForSharedClusterApplications, :migration, :sidek
it_behaves_like 'patch prometheus services post migration'
end
end
describe '#down' do
subject(:migration) { described_class.new }
let(:group) { namespaces.create!(name: 'group', path: 'gitlab-org') }
let(:project) { projects.create!(name: 'gitlab', path: 'gitlab-ee', namespace_id: namespace.id) }
let(:project_with_group_application) { projects.create!(name: 'gitlab', path: 'gitlab-ee', namespace_id: group.id) }
let!(:active_service_without_application_on_group_cluster) { services.create(service_params_for(project.id, active: true, title: 'to remove')) }
let!(:duplicated_active_service) { services.create(service_params_for(project_with_group_application.id, active: true)) }
let!(:inactive_service) { services.create(service_params_for(project_with_group_application.id, active: false)) }
let!(:manual_inactive_service) { services.create(service_params_for(project.id, active: false, properties: '{\'some\':\'param\'}')) }
let!(:active_service_group_application) { services.create(service_params_for(project_with_group_application.id, active: true)) }
before do
group_cluster = clusters.create(name: 'cluster', cluster_type: cluster_types[:group_type])
cluster_groups.create(group_id: group.id, cluster_id: group_cluster.id)
clusters_applications_prometheus.create(cluster_id: group_cluster.id, status: application_statuses[:installed], version: '123')
end
context 'application on group cluster' do
it 'removes only redundant and inconsistent entries' do
migration.down
expect(services.all.map(&method(:row_attributes))).to match_array([manual_inactive_service, active_service_group_application].map(&method(:row_attributes)))
end
end
context 'application on instance cluster' do
it 'removes only redundant and inconsistent entries' do
instance_cluster = clusters.create(name: 'cluster', cluster_type: cluster_types[:instance_type])
clusters_applications_prometheus.create(cluster_id: instance_cluster.id, status: application_statuses[:updated], version: '123')
migration.down
expected_rows = [active_service_without_application_on_group_cluster, manual_inactive_service, active_service_group_application].map(&method(:row_attributes))
expect(services.all.map(&method(:row_attributes))).to match_array(expected_rows)
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