summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2019-04-20 13:27:54 +0000
committerStan Hu <stanhu@gmail.com>2019-04-20 13:27:54 +0000
commit10bf3bbc904c96d5d7633f58cb93cf6927e18c8e (patch)
treee4b6118887e52c14e9c8847de7eb9b1cb38b523e
parent9c2b37d7c4621f8284530565fa1d361b12502715 (diff)
parenta02e22438de38e4eca64e45c324af10626b3975c (diff)
downloadgitlab-ce-10bf3bbc904c96d5d7633f58cb93cf6927e18c8e.tar.gz
Merge branch 'jv-dedup-activerecord' into 'master'
Fix wrong use of ActiveRecord in PoolRepository See merge request gitlab-org/gitlab-ce!27464
-rw-r--r--app/models/pool_repository.rb5
-rw-r--r--app/models/project.rb8
-rw-r--r--changelogs/unreleased/jv-dedup-activerecord.yml6
-rw-r--r--spec/factories/pool_repositories.rb1
-rw-r--r--spec/models/pool_repository_spec.rb2
-rw-r--r--spec/models/project_spec.rb2
6 files changed, 16 insertions, 8 deletions
diff --git a/app/models/pool_repository.rb b/app/models/pool_repository.rb
index 7934118761e..50eed7344bd 100644
--- a/app/models/pool_repository.rb
+++ b/app/models/pool_repository.rb
@@ -7,7 +7,7 @@ class PoolRepository < ApplicationRecord
include Shardable
include AfterCommitQueue
- has_one :source_project, class_name: 'Project'
+ belongs_to :source_project, class_name: 'Project'
validates :source_project, presence: true
has_many :member_projects, class_name: 'Project'
@@ -99,7 +99,8 @@ class PoolRepository < ApplicationRecord
end
def inspect
- "#<#{self.class.name} id:#{id} state:#{state} disk_path:#{disk_path} source_project: #{source_project.full_path}>"
+ source = source_project ? source_project.full_path : 'nil'
+ "#<#{self.class.name} id:#{id} state:#{state} disk_path:#{disk_path} source_project: #{source}>"
end
private
diff --git a/app/models/project.rb b/app/models/project.rb
index 2fb6f5cb6a7..b7a10b3547a 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -2131,13 +2131,11 @@ class Project < ApplicationRecord
end
def create_new_pool_repository
- pool = begin
- create_pool_repository!(shard: Shard.by_name(repository_storage), source_project: self)
- rescue ActiveRecord::RecordNotUnique
- pool_repository(true)
- end
+ pool = PoolRepository.safe_find_or_create_by!(shard: Shard.by_name(repository_storage), source_project: self)
+ update!(pool_repository: pool)
pool.schedule unless pool.scheduled?
+
pool
end
diff --git a/changelogs/unreleased/jv-dedup-activerecord.yml b/changelogs/unreleased/jv-dedup-activerecord.yml
new file mode 100644
index 00000000000..7b440c7c0db
--- /dev/null
+++ b/changelogs/unreleased/jv-dedup-activerecord.yml
@@ -0,0 +1,6 @@
+---
+title: Fix wrong use of ActiveRecord in PoolRepository
+merge_request: 27464
+author:
+type: fixed
+
diff --git a/spec/factories/pool_repositories.rb b/spec/factories/pool_repositories.rb
index 36e54cf44b4..8cac666069c 100644
--- a/spec/factories/pool_repositories.rb
+++ b/spec/factories/pool_repositories.rb
@@ -5,6 +5,7 @@ FactoryBot.define do
before(:create) do |pool|
pool.source_project = create(:project, :repository)
+ pool.source_project.update!(pool_repository: pool)
end
trait :scheduled do
diff --git a/spec/models/pool_repository_spec.rb b/spec/models/pool_repository_spec.rb
index e5a3a3ad66e..ae00f9df89e 100644
--- a/spec/models/pool_repository_spec.rb
+++ b/spec/models/pool_repository_spec.rb
@@ -5,7 +5,7 @@ require 'spec_helper'
describe PoolRepository do
describe 'associations' do
it { is_expected.to belong_to(:shard) }
- it { is_expected.to have_one(:source_project) }
+ it { is_expected.to belong_to(:source_project) }
it { is_expected.to have_many(:member_projects) }
end
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index 9f6a0b53281..bb0257e7456 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -4702,6 +4702,8 @@ describe Project do
it 'returns that pool repository' do
expect(subject).not_to be_empty
expect(subject[:pool_repository]).to be_persisted
+
+ expect(project.reload.pool_repository).to eq(subject[:pool_repository])
end
end
end