Commit 722f8c94 authored by Adam Hegyi's avatar Adam Hegyi

Merge branch '337099-cablett-add-project-namespace-model' into 'master'

Add relationship between project and project namespace

See merge request gitlab-org/gitlab!68825
parents 37ec7b18 75efa8f4
# frozen_string_literal: true
module Namespaces
class ProjectNamespace < Namespace
has_one :project, foreign_key: :project_namespace_id, inverse_of: :project_namespace
end
end
...@@ -162,6 +162,7 @@ class Project < ApplicationRecord ...@@ -162,6 +162,7 @@ class Project < ApplicationRecord
belongs_to :creator, class_name: 'User' belongs_to :creator, class_name: 'User'
belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'namespace_id' belongs_to :group, -> { where(type: 'Group') }, foreign_key: 'namespace_id'
belongs_to :namespace belongs_to :namespace
belongs_to :project_namespace, class_name: 'Namespaces::ProjectNamespace', foreign_key: 'project_namespace_id', inverse_of: :project
alias_method :parent, :namespace alias_method :parent, :namespace
alias_attribute :parent_id, :namespace_id alias_attribute :parent_id, :namespace_id
......
# frozen_string_literal: true
class AddProjectNamespaceIdToProject < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
def up
with_lock_retries do
# This is being added to Projects as a replacement for Namespace
# See https://gitlab.com/gitlab-org/gitlab/-/issues/337099
add_column :projects, :project_namespace_id, :bigint # rubocop: disable Migration/AddColumnsToWideTables
end
end
def down
with_lock_retries do
remove_column :projects, :project_namespace_id
end
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
abd298ec9e6d9016c05032504d9ff0de7af9c6a031e0eacb041f29e59e82f289
\ No newline at end of file
f1fc9e062f5100db6a549fffa2fcd78d8eb6854cea388a6ac7addf4f6f232920
\ No newline at end of file
cc99eb2b40ee88d4d6df07253f599deb26be2fca7b941c5cecb2f8fb7ff3641d
\ No newline at end of file
...@@ -18269,7 +18269,8 @@ CREATE TABLE projects ( ...@@ -18269,7 +18269,8 @@ CREATE TABLE projects (
marked_for_deletion_at date, marked_for_deletion_at date,
marked_for_deletion_by_user_id integer, marked_for_deletion_by_user_id integer,
autoclose_referenced_issues boolean, autoclose_referenced_issues boolean,
suggestion_commit_message character varying(255) suggestion_commit_message character varying(255),
project_namespace_id bigint
); );
CREATE SEQUENCE projects_id_seq CREATE SEQUENCE projects_id_seq
...@@ -26148,6 +26149,8 @@ CREATE INDEX index_projects_on_pending_delete ON projects USING btree (pending_d ...@@ -26148,6 +26149,8 @@ CREATE INDEX index_projects_on_pending_delete ON projects USING btree (pending_d
CREATE INDEX index_projects_on_pool_repository_id ON projects USING btree (pool_repository_id) WHERE (pool_repository_id IS NOT NULL); CREATE INDEX index_projects_on_pool_repository_id ON projects USING btree (pool_repository_id) WHERE (pool_repository_id IS NOT NULL);
CREATE UNIQUE INDEX index_projects_on_project_namespace_id ON projects USING btree (project_namespace_id);
CREATE INDEX index_projects_on_repository_storage ON projects USING btree (repository_storage); CREATE INDEX index_projects_on_repository_storage ON projects USING btree (repository_storage);
CREATE INDEX index_projects_on_runners_token ON projects USING btree (runners_token); CREATE INDEX index_projects_on_runners_token ON projects USING btree (runners_token);
...@@ -27662,6 +27665,9 @@ ALTER TABLE ONLY terraform_state_versions ...@@ -27662,6 +27665,9 @@ ALTER TABLE ONLY terraform_state_versions
ALTER TABLE ONLY protected_branch_push_access_levels ALTER TABLE ONLY protected_branch_push_access_levels
ADD CONSTRAINT fk_7111b68cdb FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE; ADD CONSTRAINT fk_7111b68cdb FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE;
ALTER TABLE ONLY projects
ADD CONSTRAINT fk_71625606ac FOREIGN KEY (project_namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE;
ALTER TABLE ONLY integrations ALTER TABLE ONLY integrations
ADD CONSTRAINT fk_71cce407f9 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; ADD CONSTRAINT fk_71cce407f9 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
...@@ -128,6 +128,7 @@ excluded_attributes: ...@@ -128,6 +128,7 @@ excluded_attributes:
- :name - :name
- :path - :path
- :namespace_id - :namespace_id
- :project_namespace_id
- :creator_id - :creator_id
- :pool_repository_id - :pool_repository_id
- :import_url - :import_url
......
...@@ -370,6 +370,7 @@ project: ...@@ -370,6 +370,7 @@ project:
- value_streams - value_streams
- group - group
- namespace - namespace
- project_namespace
- management_clusters - management_clusters
- boards - boards
- last_event - last_event
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Namespaces::ProjectNamespace, type: :model do
describe 'relationships' do
it { is_expected.to have_one(:project).with_foreign_key(:project_namespace_id).inverse_of(:project_namespace) }
end
end
...@@ -16,6 +16,7 @@ RSpec.describe Project, factory_default: :keep do ...@@ -16,6 +16,7 @@ RSpec.describe Project, factory_default: :keep do
describe 'associations' do describe 'associations' do
it { is_expected.to belong_to(:group) } it { is_expected.to belong_to(:group) }
it { is_expected.to belong_to(:namespace) } it { is_expected.to belong_to(:namespace) }
it { is_expected.to belong_to(:project_namespace).class_name('Namespaces::ProjectNamespace').with_foreign_key('project_namespace_id').inverse_of(:project) }
it { is_expected.to belong_to(:creator).class_name('User') } it { is_expected.to belong_to(:creator).class_name('User') }
it { is_expected.to belong_to(:pool_repository) } it { is_expected.to belong_to(:pool_repository) }
it { is_expected.to have_many(:users) } it { is_expected.to have_many(:users) }
......
...@@ -32,6 +32,7 @@ itself: # project ...@@ -32,6 +32,7 @@ itself: # project
- pages_https_only - pages_https_only
- pending_delete - pending_delete
- pool_repository_id - pool_repository_id
- project_namespace_id
- pull_mirror_available_overridden - pull_mirror_available_overridden
- pull_mirror_branch_prefix - pull_mirror_branch_prefix
- remote_mirror_available_overridden - remote_mirror_available_overridden
......
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