Commit a3688fcc authored by Andreas Brandl  |  OOO from Wed's avatar Andreas Brandl | OOO from Wed

Merge branch '31867-remove-alerting-token-migration' into 'master'

Remove self monitoring project alerting token migration

See merge request gitlab-org/gitlab!17049
parents 3271ff96 95a787c2
......@@ -3,71 +3,17 @@
class SetSelfMonitoringProjectAlertingToken < ActiveRecord::Migration[5.2]
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
Gitlab.ee do
project = Migratable::ApplicationSetting.current_without_cache&.instance_administration_project
# no-op
# Converted to no-op in https://gitlab.com/gitlab-org/gitlab/merge_requests/17049.
if project
setup_alertmanager_token(project)
end
end
# This migration has been made a no-op because the pre-requisite migration
# which creates the self-monitoring project has already been removed in
# https://gitlab.com/gitlab-org/gitlab/merge_requests/16864. As
# such, this migration would do nothing.
end
def down
Gitlab.ee do
Migratable::ApplicationSetting.current_without_cache
&.instance_administration_project
&.alerting_setting
&.destroy!
end
# no-op
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