Commit 32a7487c authored by Imre Farkas's avatar Imre Farkas

Merge branch 'add-namespace-settings-table' into 'master'

Add namespace settings table

See merge request gitlab-org/gitlab!36321
parents 690bb7fe 56f62330
......@@ -22,6 +22,7 @@ class Namespace < ApplicationRecord
has_many :projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :project_statistics
has_one :namespace_settings, inverse_of: :namespace, class_name: 'NamespaceSetting', autosave: true
has_many :runner_namespaces, inverse_of: :namespace, class_name: 'Ci::RunnerNamespace'
has_many :runners, through: :runner_namespaces, source: :runner, class_name: 'Ci::Runner'
......
# frozen_string_literal: true
class NamespaceSetting < ApplicationRecord
belongs_to :namespace, inverse_of: :namespace_settings
self.primary_key = :namespace_id
end
NamespaceSetting.prepend_if_ee('EE::NamespaceSetting')
......@@ -1270,7 +1270,8 @@ class User < ApplicationRecord
namespace.path = username if username_changed?
namespace.name = name if name_changed?
else
build_namespace(path: username, name: name)
namespace = build_namespace(path: username, name: name)
namespace.build_namespace_settings
end
end
......
......@@ -28,7 +28,11 @@ module Groups
@group.build_chat_team(name: response['name'], team_id: response['id'])
end
@group.add_owner(current_user) if @group.save
if @group.save
@group.add_owner(current_user)
add_settings_record
end
@group
end
......@@ -79,6 +83,10 @@ module Groups
params[:visibility_level] = Gitlab::CurrentSettings.current_application_settings.default_group_visibility
end
def add_settings_record
@group.create_namespace_settings
end
end
end
......
---
title: Add namespace settings table
merge_request: 36321
author:
type: added
# frozen_string_literal: true
class CreateNamespaceSettings < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
with_lock_retries do
create_table :namespace_settings, id: false do |t|
t.timestamps_with_timezone null: false
t.references :namespace, primary_key: true, default: nil, type: :integer, index: false, foreign_key: { on_delete: :cascade }
end
end
end
def down
drop_table :namespace_settings
end
end
# frozen_string_literal: true
class BackfillNamespaceSettings < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
MIGRATION = 'BackfillNamespaceSettings'
DELAY_INTERVAL = 2.minutes
BATCH_SIZE = 10_000
disable_ddl_transaction!
class Namespace < ActiveRecord::Base
include EachBatch
self.table_name = 'namespaces'
end
def up
say "Scheduling `#{MIGRATION}` jobs"
queue_background_migration_jobs_by_range_at_intervals(Namespace, MIGRATION, DELAY_INTERVAL, batch_size: BATCH_SIZE)
end
def down
# NOOP
end
end
......@@ -13118,6 +13118,12 @@ CREATE TABLE public.namespace_root_storage_statistics (
snippets_size bigint DEFAULT 0 NOT NULL
);
CREATE TABLE public.namespace_settings (
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL,
namespace_id integer NOT NULL
);
CREATE TABLE public.namespace_statistics (
id integer NOT NULL,
namespace_id integer NOT NULL,
......@@ -17894,6 +17900,9 @@ ALTER TABLE ONLY public.namespace_limits
ALTER TABLE ONLY public.namespace_root_storage_statistics
ADD CONSTRAINT namespace_root_storage_statistics_pkey PRIMARY KEY (namespace_id);
ALTER TABLE ONLY public.namespace_settings
ADD CONSTRAINT namespace_settings_pkey PRIMARY KEY (namespace_id);
ALTER TABLE ONLY public.namespace_statistics
ADD CONSTRAINT namespace_statistics_pkey PRIMARY KEY (id);
......@@ -21776,6 +21785,9 @@ ALTER TABLE ONLY public.analytics_cycle_analytics_project_stages
ALTER TABLE ONLY public.issue_user_mentions
ADD CONSTRAINT fk_rails_3861d9fefa FOREIGN KEY (note_id) REFERENCES public.notes(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.namespace_settings
ADD CONSTRAINT fk_rails_3896d4fae5 FOREIGN KEY (namespace_id) REFERENCES public.namespaces(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.self_managed_prometheus_alert_events
ADD CONSTRAINT fk_rails_3936dadc62 FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE;
......@@ -23825,6 +23837,8 @@ COPY "schema_migrations" (version) FROM STDIN;
20200702201039
20200703064117
20200703121557
20200703124823
20200703125016
20200703154822
20200704143633
20200704161600
......
# frozen_string_literal: true
module EE
module NamespaceSetting
extend ActiveSupport::Concern
end
end
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
# Backfillnamespace_settings for a range of namespaces
class BackfillNamespaceSettings
def perform(start_id, end_id)
ActiveRecord::Base.connection.execute <<~SQL
INSERT INTO namespace_settings (namespace_id, created_at, updated_at)
SELECT namespaces.id, now(), now()
FROM namespaces
WHERE namespaces.id BETWEEN #{start_id} AND #{end_id}
ON CONFLICT (namespace_id) DO NOTHING;
SQL
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::BackfillNamespaceSettings, schema: 20200703125016 do
let(:namespaces) { table(:namespaces) }
let(:namespace_settings) { table(:namespace_settings) }
let(:namespace) { table(:namespaces).create!(name: 'user', path: 'user') }
subject { described_class.new }
describe '#perform' do
it 'creates settings for all projects in range' do
namespaces.create!(id: 5, name: 'test1', path: 'test1')
namespaces.create!(id: 7, name: 'test2', path: 'test2')
namespaces.create!(id: 8, name: 'test3', path: 'test3')
subject.perform(5, 7)
expect(namespace_settings.all.pluck(:namespace_id)).to contain_exactly(5, 7)
end
end
end
......@@ -24,7 +24,15 @@ RSpec.describe Gitlab::BackgroundMigration::BackfillSnippetRepositories, :migrat
confirmed_at: 1.day.ago)
end
let(:migration_bot) { User.migration_bot }
let!(:migration_bot) do
users.create(id: 100,
email: "noreply+gitlab-migration-bot%s@#{Settings.gitlab.host}",
user_type: HasUserType::USER_TYPES[:migration_bot],
name: 'GitLab Migration Bot',
projects_limit: 10,
username: 'bot')
end
let!(:snippet_with_repo) { snippets.create(id: 1, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) }
let!(:snippet_with_empty_repo) { snippets.create(id: 2, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) }
let!(:snippet_without_repo) { snippets.create(id: 3, type: 'PersonalSnippet', author_id: user.id, file_name: file_name, content: content) }
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20200703125016_backfill_namespace_settings.rb')
RSpec.describe BackfillNamespaceSettings, :sidekiq, schema: 20200703124823 do
let(:namespaces) { table(:namespaces) }
describe '#up' do
before do
stub_const("#{described_class}::BATCH_SIZE", 2)
namespaces.create!(id: 1, name: 'test1', path: 'test1')
namespaces.create!(id: 2, name: 'test2', path: 'test2')
namespaces.create!(id: 3, name: 'test3', path: 'test3')
end
it 'schedules BackfillNamespaceSettings background jobs' do
Sidekiq::Testing.fake! do
Timecop.freeze do
migrate!
expect(described_class::MIGRATION).to be_scheduled_delayed_migration(2.minutes, 1, 2)
expect(described_class::MIGRATION).to be_scheduled_delayed_migration(4.minutes, 3, 3)
expect(BackgroundMigrationWorker.jobs.size).to eq(2)
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe NamespaceSetting, type: :model do
it { is_expected.to belong_to(:namespace) }
end
......@@ -17,6 +17,7 @@ RSpec.describe Namespace do
it { is_expected.to have_many :children }
it { is_expected.to have_one :root_storage_statistics }
it { is_expected.to have_one :aggregation_schedule }
it { is_expected.to have_one :namespace_settings }
it { is_expected.to have_many :custom_emoji }
end
......
......@@ -3711,6 +3711,12 @@ RSpec.describe User do
expect(user.namespace).not_to be_nil
end
it 'creates the namespace setting' do
user.save!
expect(user.namespace.namespace_settings).to be_persisted
end
end
context 'for an existing user' do
......
......@@ -122,6 +122,7 @@ RSpec.describe API::GroupImport do
before do
allow_next_instance_of(Group) do |group|
allow(group).to receive(:persisted?).and_return(false)
allow(group).to receive(:save).and_return(false)
end
end
......
......@@ -129,4 +129,13 @@ RSpec.describe Groups::CreateService, '#execute' do
expect { subject }.to change { ChatTeam.count }.from(0).to(1)
end
end
describe 'creating a setting record' do
let(:service) { described_class.new(user, group_params) }
it 'create the settings record connected to the group' do
group = subject
expect(group.namespace_settings).to be_persisted
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