Commit 6914cf3c authored by charlie ablett's avatar charlie ablett

Add ProjectNamespace model and DB relationships

- Add ProjectNamespace model in Namespaces
- Add foreign key

Changelog: added
parent 550065c4
......@@ -211,7 +211,6 @@ class Project < ApplicationRecord
has_one :unify_circuit_integration, class_name: 'Integrations::UnifyCircuit'
has_one :webex_teams_integration, class_name: 'Integrations::WebexTeams'
has_one :youtrack_integration, class_name: 'Integrations::Youtrack'
has_one :zentao_integration, class_name: 'Integrations::Zentao'
has_one :root_of_fork_network,
foreign_key: 'root_project_id',
......@@ -341,7 +340,6 @@ class Project < ApplicationRecord
# build traces. Currently there's no efficient way of removing this data in
# bulk that doesn't involve loading the rows into memory. As a result we're
# still using `dependent: :destroy` here.
has_many :pending_builds, class_name: 'Ci::PendingBuild'
has_many :builds, class_name: 'Ci::Build', inverse_of: :project, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :processables, class_name: 'Ci::Processable', inverse_of: :project
has_many :build_trace_chunks, class_name: 'Ci::BuildTraceChunk', through: :builds, source: :trace_chunks
......@@ -1460,7 +1458,7 @@ class Project < ApplicationRecord
end
def disabled_integrations
[:zentao]
[]
end
def find_or_initialize_integration(name)
......@@ -1679,10 +1677,6 @@ class Project < ApplicationRecord
end
end
def membership_locked?
false
end
def bots
users.project_bot
end
......@@ -1790,9 +1784,6 @@ class Project < ApplicationRecord
Ci::Runner.from_union([runners, group_runners, available_shared_runners])
end
# Once issue 339937 is fixed, please search for all mentioned of
# https://gitlab.com/gitlab-org/gitlab/-/issues/339937,
# and remove the allow_cross_joins_across_databases.
def active_runners
strong_memoize(:active_runners) do
all_available_runners.active
......@@ -1800,9 +1791,7 @@ class Project < ApplicationRecord
end
def any_online_runners?(&block)
::Gitlab::Database.allow_cross_joins_across_databases(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/339937') do
online_runners_with_tags.any?(&block)
end
online_runners_with_tags.any?(&block)
end
def valid_runners_token?(token)
......@@ -1811,15 +1800,7 @@ class Project < ApplicationRecord
# rubocop: disable CodeReuse/ServiceClass
def open_issues_count(current_user = nil)
return Projects::OpenIssuesCountService.new(self, current_user).count unless current_user.nil?
BatchLoader.for(self).batch(replace_methods: false) do |projects, loader|
issues_count_per_project = ::Projects::BatchOpenIssuesCountService.new(projects).refresh_cache_and_retrieve_data
issues_count_per_project.each do |project, count|
loader.call(project, count)
end
end
Projects::OpenIssuesCountService.new(self, current_user).count
end
# rubocop: enable CodeReuse/ServiceClass
......@@ -2278,7 +2259,7 @@ class Project < ApplicationRecord
# rubocop: disable CodeReuse/ServiceClass
def forks_count
BatchLoader.for(self).batch(replace_methods: false) do |projects, loader|
BatchLoader.for(self).batch do |projects, loader|
fork_count_per_project = ::Projects::BatchForksCountService.new(projects).refresh_cache_and_retrieve_data
fork_count_per_project.each do |project, count|
......@@ -2530,10 +2511,6 @@ class Project < ApplicationRecord
ci_config_path.blank? || ci_config_path == Gitlab::FileDetector::PATTERNS[:gitlab_ci]
end
def uses_external_project_ci_config?
!!(ci_config_path =~ %r{@.+/.+})
end
def limited_protected_branches(limit)
protected_branches.limit(limit)
end
......@@ -2642,10 +2619,6 @@ class Project < ApplicationRecord
repository.gitlab_ci_yml_for(sha, ci_config_path_or_default)
end
def ci_config_external_project
Project.find_by_full_path(ci_config_path.split('@', 2).last)
end
def enabled_group_deploy_keys
return GroupDeployKey.none unless group
......@@ -2908,8 +2881,12 @@ class Project < ApplicationRecord
update_column(:has_external_issue_tracker, integrations.external_issue_trackers.any?) if Gitlab::Database.read_write?
end
def active_runners_with_tags
@active_runners_with_tags ||= active_runners.with_tags
end
def online_runners_with_tags
@online_runners_with_tags ||= active_runners.with_tags.online
@online_runners_with_tags ||= active_runners_with_tags.online
end
end
......
# frozen_string_literal: true
class AddProjectNamespaceIndexToProject < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
INDEX_NAME = 'index_projects_on_project_namespace_id'
def up
add_concurrent_index :projects, :project_namespace_id, name: INDEX_NAME, unique: true
end
def down
remove_concurrent_index_by_name :projects, INDEX_NAME
end
end
# frozen_string_literal: true
class AddProjectNamespaceForeignKeyToProject < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
TARGET_COLUMN = :project_namespace_id
def up
add_concurrent_foreign_key :projects, :namespaces, column: TARGET_COLUMN, on_delete: :cascade
end
def down
with_lock_retries do
remove_foreign_key_if_exists(:projects, column: TARGET_COLUMN)
end
end
end
......@@ -138,7 +138,6 @@ RSpec.describe Project, factory_default: :keep do
it { is_expected.to have_many(:timelogs) }
it { is_expected.to have_many(:error_tracking_errors).class_name('ErrorTracking::Error') }
it { is_expected.to have_many(:error_tracking_client_keys).class_name('ErrorTracking::ClientKey') }
it { is_expected.to have_many(:pending_builds).class_name('Ci::PendingBuild') }
# GitLab Pages
it { is_expected.to have_many(:pages_domains) }
......@@ -618,12 +617,6 @@ RSpec.describe Project, factory_default: :keep do
end
end
describe '#membership_locked?' do
it 'returns false' do
expect(build(:project)).not_to be_membership_locked
end
end
describe '#autoclose_referenced_issues' do
context 'when DB entry is nil' do
let(:project) { build(:project, autoclose_referenced_issues: nil) }
......@@ -1073,12 +1066,12 @@ RSpec.describe Project, factory_default: :keep do
project.open_issues_count(user)
end
it 'invokes the batch count service with no current_user' do
count_service = instance_double(Projects::BatchOpenIssuesCountService)
expect(Projects::BatchOpenIssuesCountService).to receive(:new).with([project]).and_return(count_service)
expect(count_service).to receive(:refresh_cache_and_retrieve_data).and_return({})
it 'invokes the count service with no current_user' do
count_service = instance_double(Projects::OpenIssuesCountService)
expect(Projects::OpenIssuesCountService).to receive(:new).with(project, nil).and_return(count_service)
expect(count_service).to receive(:count)
project.open_issues_count.to_s
project.open_issues_count
end
end
......@@ -2562,7 +2555,7 @@ RSpec.describe Project, factory_default: :keep do
end
describe '#uses_default_ci_config?' do
let(:project) { build(:project) }
let(:project) { build(:project)}
it 'has a custom ci config path' do
project.ci_config_path = 'something_custom'
......@@ -2583,44 +2576,6 @@ RSpec.describe Project, factory_default: :keep do
end
end
describe '#uses_external_project_ci_config?' do
subject(:uses_external_project_ci_config) { project.uses_external_project_ci_config? }
let(:project) { build(:project) }
context 'when ci_config_path is configured with external project' do
before do
project.ci_config_path = '.gitlab-ci.yml@hello/world'
end
it { is_expected.to eq(true) }
end
context 'when ci_config_path is nil' do
before do
project.ci_config_path = nil
end
it { is_expected.to eq(false) }
end
context 'when ci_config_path is configured with a file in the project' do
before do
project.ci_config_path = 'hello/world/gitlab-ci.yml'
end
it { is_expected.to eq(false) }
end
context 'when ci_config_path is configured with remote file' do
before do
project.ci_config_path = 'https://example.org/file.yml'
end
it { is_expected.to eq(false) }
end
end
describe '#latest_successful_build_for_ref' do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:pipeline) { create_pipeline(project) }
......@@ -7073,15 +7028,6 @@ RSpec.describe Project, factory_default: :keep do
end
end
describe '#ci_config_external_project' do
subject(:ci_config_external_project) { project.ci_config_external_project }
let(:other_project) { create(:project) }
let(:project) { build(:project, ci_config_path: ".gitlab-ci.yml@#{other_project.full_path}") }
it { is_expected.to eq(other_project) }
end
describe '#enabled_group_deploy_keys' do
let_it_be(:project) { create(:project) }
......
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