Commit 95a787c2 authored by Reuben Pereira's avatar Reuben Pereira Committed by Andreas Brandl | OOO from Wed

Make self monitoring alerting token migration no-op

- Remove it since it will not do anything since the migration that adds
the self-monitoring project itself has been removed.
- The migration does not fail since it checks for the existence of the
self-monitoring project.
parent 3271ff96
...@@ -3,71 +3,17 @@ ...@@ -3,71 +3,17 @@
class SetSelfMonitoringProjectAlertingToken < ActiveRecord::Migration[5.2] class SetSelfMonitoringProjectAlertingToken < ActiveRecord::Migration[5.2]
DOWNTIME = false DOWNTIME = false
module Migratable
module Alerting
class ProjectAlertingSetting < ApplicationRecord
self.table_name = 'project_alerting_settings'
belongs_to :project
validates :token, presence: true
attr_encrypted :token,
mode: :per_attribute_iv,
key: Settings.attr_encrypted_db_key_base_truncated,
algorithm: 'aes-256-gcm'
before_validation :ensure_token
private
def ensure_token
self.token ||= generate_token
end
def generate_token
SecureRandom.hex
end
end
end
class Project < ApplicationRecord
has_one :alerting_setting, inverse_of: :project, class_name: 'Alerting::ProjectAlertingSetting'
end
class ApplicationSetting < ApplicationRecord
self.table_name = 'application_settings'
belongs_to :instance_administration_project, class_name: 'Project'
def self.current_without_cache
last
end
end
end
def setup_alertmanager_token(project)
return unless License.feature_available?(:prometheus_alerts)
project.create_alerting_setting!
end
def up def up
Gitlab.ee do # no-op
project = Migratable::ApplicationSetting.current_without_cache&.instance_administration_project # Converted to no-op in https://gitlab.com/gitlab-org/gitlab/merge_requests/17049.
if project # This migration has been made a no-op because the pre-requisite migration
setup_alertmanager_token(project) # which creates the self-monitoring project has already been removed in
end # https://gitlab.com/gitlab-org/gitlab/merge_requests/16864. As
end # such, this migration would do nothing.
end end
def down def down
Gitlab.ee do # no-op
Migratable::ApplicationSetting.current_without_cache
&.instance_administration_project
&.alerting_setting
&.destroy!
end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20190809072552_set_self_monitoring_project_alerting_token.rb')
describe SetSelfMonitoringProjectAlertingToken, :migration do
let(:application_settings) { table(:application_settings) }
let(:projects) { table(:projects) }
let(:namespaces) { table(:namespaces) }
let(:namespace) do
namespaces.create!(
path: 'gitlab-instance-administrators',
name: 'GitLab Instance Administrators'
)
end
let(:project) do
projects.create!(
namespace_id: namespace.id,
name: 'GitLab Instance Administration'
)
end
describe 'down' do
before do
application_settings.create!(instance_administration_project_id: project.id)
stub_licensed_features(prometheus_alerts: true)
end
it 'destroys token' do
migrate!
token = Alerting::ProjectAlertingSetting.where(project_id: project.id).first!.token
expect(token).to be_present
schema_migrate_down!
expect(Alerting::ProjectAlertingSetting.count).to eq(0)
end
end
describe 'up' do
context 'when instance administration project present' do
before do
application_settings.create!(instance_administration_project_id: project.id)
stub_licensed_features(prometheus_alerts: true)
end
it 'sets the alerting token' do
migrate!
token = Alerting::ProjectAlertingSetting.where(project_id: project.id).first!.token
expect(token).to be_present
end
end
context 'when instance administration project not present' do
it 'does not raise error' do
migrate!
expect(Alerting::ProjectAlertingSetting.count).to eq(0)
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