Commit b1316e76 authored by Patrick Bair's avatar Patrick Bair

Merge branch 'jprovazn-second-source-model' into 'master'

Add namespace_id reference to routes

See merge request gitlab-org/gitlab!77571
parents 40f1a4b2 b8eb9f1d
......@@ -43,6 +43,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_one :namespace_route, foreign_key: :namespace_id, autosave: true, inverse_of: :namespace, class_name: 'Route'
has_many :runner_namespaces, inverse_of: :namespace, class_name: 'Ci::RunnerNamespace'
has_many :runners, through: :runner_namespaces, source: :runner, class_name: 'Ci::Runner'
......
......@@ -5,6 +5,7 @@ class Route < ApplicationRecord
include Gitlab::SQL::Pattern
belongs_to :source, polymorphic: true, inverse_of: :route # rubocop:disable Cop/PolymorphicAssociations
belongs_to :namespace, inverse_of: :namespace_route
validates :source, presence: true
validates :path,
......
# frozen_string_literal: true
class AddRouteNamespaceReference < Gitlab::Database::Migration[1.0]
enable_lock_retries!
def up
add_column :routes, :namespace_id, :bigint unless column_exists?(:routes, :namespace_id)
end
def down
remove_column :routes, :namespace_id if column_exists?(:routes, :namespace_id)
end
end
# frozen_string_literal: true
class AddRouteNamespaceIndex < Gitlab::Database::Migration[1.0]
disable_ddl_transaction!
INDEX_NAME = 'index_routes_on_namespace_id'
def up
add_concurrent_index :routes, :namespace_id, unique: true, name: INDEX_NAME
add_concurrent_foreign_key :routes, :namespaces, column: :namespace_id, on_delete: :nullify, reverse_lock_order: true
end
def down
with_lock_retries do
remove_foreign_key_if_exists :routes, column: :namespace_id
end
remove_concurrent_index_by_name :routes, INDEX_NAME
end
end
27ca3977a7569e98271eeb2bd224be1cfe5452ab3778665325b89bf966e07942
\ No newline at end of file
ef1a7c5f7b10640a0ddc669528dcdb02fd2525d716562f928578e8902a07a832
\ No newline at end of file
......@@ -19236,7 +19236,8 @@ CREATE TABLE routes (
path character varying NOT NULL,
created_at timestamp without time zone,
updated_at timestamp without time zone,
name character varying
name character varying,
namespace_id bigint
);
CREATE SEQUENCE routes_id_seq
......@@ -27396,6 +27397,8 @@ CREATE INDEX index_reviews_on_project_id ON reviews USING btree (project_id);
CREATE INDEX index_route_on_name_trigram ON routes USING gin (name gin_trgm_ops);
CREATE UNIQUE INDEX index_routes_on_namespace_id ON routes USING btree (namespace_id);
CREATE UNIQUE INDEX index_routes_on_path ON routes USING btree (path);
CREATE INDEX index_routes_on_path_text_pattern_ops ON routes USING btree (path varchar_pattern_ops);
......@@ -29305,6 +29308,9 @@ ALTER TABLE ONLY merge_requests
ALTER TABLE ONLY ci_builds
ADD CONSTRAINT fk_6661f4f0e8 FOREIGN KEY (resource_group_id) REFERENCES ci_resource_groups(id) ON DELETE SET NULL;
ALTER TABLE ONLY routes
ADD CONSTRAINT fk_679ff8213d FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE SET NULL;
ALTER TABLE ONLY application_settings
ADD CONSTRAINT fk_693b8795e4 FOREIGN KEY (push_rule_id) REFERENCES push_rules(id) ON DELETE SET NULL;
......@@ -28,6 +28,7 @@ RSpec.describe Namespace do
it { is_expected.to have_one :onboarding_progress }
it { is_expected.to have_one :admin_note }
it { is_expected.to have_many :pending_builds }
it { is_expected.to have_one :namespace_route }
describe '#children' do
let_it_be(:group) { create(:group) }
......
......@@ -8,6 +8,7 @@ RSpec.describe Route do
describe 'relationships' do
it { is_expected.to belong_to(:source) }
it { is_expected.to belong_to(:namespace) }
end
describe 'validations' do
......
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