Commit d43a08aa authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '292927-add-sec-setting-to-project' into 'master'

Add SecuritySetting to Projects

See merge request gitlab-org/gitlab!67254
parents 5dbc1c26 863b7c32
# frozen_string_literal: true
class ScheduleSecuritySettingCreation < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
MIGRATION = 'CreateSecuritySetting'
BATCH_SIZE = 1000
INTERVAL = 5.minutes.to_i
disable_ddl_transaction!
def up
return unless Gitlab.ee? # Security Settings available only in EE version
queue_background_migration_jobs_by_range_at_intervals(
define_batchable_model('projects'),
MIGRATION,
INTERVAL,
batch_size: BATCH_SIZE
)
end
# We're adding data so no need for rollback
def down
end
end
33b260626d65347a80240ffdce5f9e2abfc578e8151ed41f1ca9b16ef2654853
\ No newline at end of file
# frozen_string_literal: true
module EE
module Gitlab
module BackgroundMigration
module CreateSecuritySetting
extend ::Gitlab::Utils::Override
class Project < ActiveRecord::Base
self.table_name = 'projects'
has_one :security_setting, class_name: 'ProjectSecuritySetting'
scope :without_security_settings, -> { left_joins(:security_setting).where(project_security_settings: { project_id: nil }) }
end
class ProjectSecuritySetting < ActiveRecord::Base
self.table_name = 'project_security_settings'
belongs_to :project, inverse_of: :security_setting
end
override :perform
def perform(from_id, to_id)
select_from = Project.without_security_settings.where(id: from_id..to_id).select('id, NOW(), NOW()').to_sql
ActiveRecord::Base.connection_pool.with_connection do |connection|
connection.execute <<~SQL
INSERT INTO project_security_settings (project_id, created_at, updated_at)
#{select_from}
ON CONFLICT (project_id) DO NOTHING
SQL
end
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::CreateSecuritySetting do
let(:projects) { table(:projects) }
let(:settings) { table(:project_security_settings) }
let(:namespaces) { table(:namespaces) }
describe '#perform' do
it 'adds setting object for existing projects' do
namespace = namespaces.create!(name: 'test', path: 'test')
projects.create!(id: 12, namespace_id: namespace.id, name: 'gitlab', path: 'gitlab')
projects.create!(id: 13, namespace_id: namespace.id, name: 'sec_gitlab', path: 'sec_gitlab')
projects.create!(id: 14, namespace_id: namespace.id, name: 'my_gitlab', path: 'my_gitlab')
settings.create!(project_id: 13)
expect { described_class.new.perform(12, 14) }.to change { settings.count }.from(1).to(3)
end
end
end
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# This class doesn't create SecuritySetting
# as this feature exists only in EE
class CreateSecuritySetting
def perform(_from_id, _to_id)
end
end
end
end
Gitlab::BackgroundMigration::CreateSecuritySetting.prepend_mod_with('Gitlab::BackgroundMigration::CreateSecuritySetting')
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe ScheduleSecuritySettingCreation, :sidekiq do
describe '#up' do
let(:projects) { table(:projects) }
let(:namespaces) { table(:namespaces) }
context 'for EE version' do
before do
stub_const("#{described_class.name}::BATCH_SIZE", 2)
allow(Gitlab).to receive(:ee?).and_return(true)
end
it 'schedules background migration job' do
namespace = namespaces.create!(name: 'test', path: 'test')
projects.create!(id: 12, namespace_id: namespace.id, name: 'red', path: 'red')
projects.create!(id: 13, namespace_id: namespace.id, name: 'green', path: 'green')
projects.create!(id: 14, namespace_id: namespace.id, name: 'blue', path: 'blue')
Sidekiq::Testing.fake! do
freeze_time do
migrate!
expect(described_class::MIGRATION)
.to be_scheduled_delayed_migration(5.minutes, 12, 13)
expect(described_class::MIGRATION)
.to be_scheduled_delayed_migration(10.minutes, 14, 14)
expect(BackgroundMigrationWorker.jobs.size).to eq(2)
end
end
end
end
context 'for FOSS version' do
before do
allow(Gitlab).to receive(:ee?).and_return(false)
end
it 'does not schedule any jobs' do
namespace = namespaces.create!(name: 'test', path: 'test')
projects.create!(id: 12, namespace_id: namespace.id, name: 'red', path: 'red')
Sidekiq::Testing.fake! do
freeze_time do
migrate!
expect(BackgroundMigrationWorker.jobs.size).to eq(0)
end
end
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