Commit f3687d1d authored by Reuben Pereira's avatar Reuben Pereira Committed by Mayra Cabrera

Add migration and spec

Add migration to save Instance Administrators group ID in
application_settings table.
parent cd4a5674
---
title: Add migration to save Instance Administrators group ID in application_settings table
merge_request: 24796
author:
type: changed
# frozen_string_literal: true
class SaveInstanceAdministratorsGroupId < ActiveRecord::Migration[6.0]
DOWNTIME = false
def up
execute(
<<-SQL
UPDATE
application_settings
SET
instance_administrators_group_id = (
SELECT
namespace_id
FROM
projects
WHERE
id = application_settings.instance_administration_project_id
)
WHERE
instance_administrators_group_id IS NULL
AND
instance_administration_project_id IS NOT NULL
AND
ID in (
SELECT
max(id)
FROM
application_settings
)
SQL
)
end
def down
# no-op
# The change performed by `up` cannot be reversed because once the migration runs,
# we do not know what value application_settings.instance_administrators_group_id
# had before the migration was run.
end
end
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200210092405_save_instance_administrators_group_id')
describe SaveInstanceAdministratorsGroupId, :migration do
let(:application_settings_table) { table(:application_settings) }
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 project ID is saved but group ID is not' do
let(:application_settings) do
application_settings_table.create!(instance_administration_project_id: self_monitoring_project.id)
end
it 'saves instance administrators group ID' do
expect(application_settings.instance_administration_project_id).to eq(self_monitoring_project.id)
expect(application_settings.instance_administrators_group_id).to be_nil
migrate!
expect(application_settings.reload.instance_administrators_group_id).to eq(instance_administrators_group.id)
expect(application_settings.instance_administration_project_id).to eq(self_monitoring_project.id)
end
end
context 'when group ID is saved but project ID is not' do
let(:application_settings) do
application_settings_table.create!(instance_administrators_group_id: instance_administrators_group.id)
end
it 'does not make changes' do
expect(application_settings.instance_administrators_group_id).to eq(instance_administrators_group.id)
expect(application_settings.instance_administration_project_id).to be_nil
migrate!
expect(application_settings.reload.instance_administrators_group_id).to eq(instance_administrators_group.id)
expect(application_settings.instance_administration_project_id).to be_nil
end
end
context 'when group ID and project ID are both saved' do
let(:application_settings) do
application_settings_table.create!(
instance_administrators_group_id: instance_administrators_group.id,
instance_administration_project_id: self_monitoring_project.id
)
end
it 'does not make changes' do
expect(application_settings.instance_administrators_group_id).to eq(instance_administrators_group.id)
expect(application_settings.instance_administration_project_id).to eq(self_monitoring_project.id)
migrate!
expect(application_settings.reload.instance_administrators_group_id).to eq(instance_administrators_group.id)
expect(application_settings.instance_administration_project_id).to eq(self_monitoring_project.id)
end
end
context 'when neither group ID nor project ID is saved' do
let(:application_settings) do
application_settings_table.create!
end
it 'does not make changes' do
expect(application_settings.instance_administrators_group_id).to be_nil
expect(application_settings.instance_administration_project_id).to be_nil
migrate!
expect(application_settings.reload.instance_administrators_group_id).to be_nil
expect(application_settings.instance_administration_project_id).to be_nil
end
end
context 'when application_settings table has no rows' do
it 'does not fail' do
migrate!
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