Commit be1c5e9d authored by Imre Farkas's avatar Imre Farkas

Merge branch '204858-update-self-monitor-environments' into 'master'

Add migration to create self monitoring project environment

Closes #204858

See merge request gitlab-org/gitlab!25289
parents e9b8f607 656ab734
---
title: Add migration to create self monitoring project environment
merge_request: 25289
author:
type: added
# frozen_string_literal: true
class CreateEnvironmentForSelfMonitoringProject < ActiveRecord::Migration[6.0]
DOWNTIME = false
def up
execute <<~SQL
INSERT INTO environments (project_id, name, slug, created_at, updated_at)
SELECT instance_administration_project_id, 'production', 'production', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
FROM application_settings
WHERE instance_administration_project_id IS NOT NULL
AND NOT EXISTS (
SELECT 1
FROM environments
INNER JOIN application_settings
ON application_settings.instance_administration_project_id = environments.project_id
)
SQL
end
def down
# no-op
# This migration cannot be reversed because it cannot be ensured that the environment for the Self Monitoring Project
# did not already exist before the migration ran - in that case, the migration does nothing, and it would be unexpected
# behavior for that environment to be deleted by reversing this migration.
end
end
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200214214934_create_environment_for_self_monitoring_project')
describe CreateEnvironmentForSelfMonitoringProject, :migration do
let(:application_settings_table) { table(:application_settings) }
let(:environments) { table(:environments) }
let(:instance_administrators_group) do
table(:namespaces).create!(
id: 1,
name: 'GitLab Instance Administrators',
path: 'gitlab-instance-administrators-random',
type: 'Group'
)
end
let(:self_monitoring_project) do
table(:projects).create!(
id: 2,
name: 'Self Monitoring',
path: 'self_monitoring',
namespace_id: instance_administrators_group.id
)
end
context 'when the self monitoring project ID is not set' do
it 'does not make changes' do
expect(environments.find_by(project_id: self_monitoring_project.id)).to be_nil
migrate!
expect(environments.find_by(project_id: self_monitoring_project.id)).to be_nil
end
end
context 'when the self monitoring project ID is set' do
before do
application_settings_table.create!(instance_administration_project_id: self_monitoring_project.id)
end
context 'when the environment already exists' do
let!(:environment) do
environments.create!(project_id: self_monitoring_project.id, name: 'production', slug: 'production')
end
it 'does not make changes' do
expect(environments.find_by(project_id: self_monitoring_project.id)).to eq(environment)
migrate!
expect(environments.find_by(project_id: self_monitoring_project.id)).to eq(environment)
end
end
context 'when the environment does not exist' do
it 'creates the environment' do
expect(environments.find_by(project_id: self_monitoring_project.id)).to be_nil
migrate!
expect(environments.find_by(project_id: self_monitoring_project.id)).to be
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