Use BulkInsertSafe to bulk insert

We should use BulkInsertSafe where possible.
parent 63d766a6
# frozen_string_literal: true # frozen_string_literal: true
class Geo::BaseRegistry < Geo::TrackingBase class Geo::BaseRegistry < Geo::TrackingBase
include BulkInsertSafe
self.abstract_class = true self.abstract_class = true
def self.pluck_model_ids_in_range(range) def self.pluck_model_ids_in_range(range)
...@@ -16,12 +18,10 @@ class Geo::BaseRegistry < Geo::TrackingBase ...@@ -16,12 +18,10 @@ class Geo::BaseRegistry < Geo::TrackingBase
end end
def self.insert_for_model_ids(ids) def self.insert_for_model_ids(ids)
inserts = ids.map do |id| records = ids.map do |id|
{ self::MODEL_FOREIGN_KEY => id, created_at: Time.zone.now } new(self::MODEL_FOREIGN_KEY => id, created_at: Time.zone.now)
end end
ActiveRecord::InsertAll bulk_insert!(records, returns: :ids)
.new(self, inserts, on_duplicate: :skip, returning: [:id])
.execute
end end
end end
...@@ -29,14 +29,11 @@ class Geo::UploadRegistry < Geo::BaseRegistry ...@@ -29,14 +29,11 @@ class Geo::UploadRegistry < Geo::BaseRegistry
end end
def self.insert_for_model_ids(attrs) def self.insert_for_model_ids(attrs)
inserts = attrs.map do |file_id, file_type| records = attrs.map do |file_id, file_type|
{ file_id: file_id, file_type: file_type, created_at: Time.zone.now } new(file_id: file_id, file_type: file_type, created_at: Time.zone.now)
end end
ActiveRecord::InsertAll bulk_insert!(records, returns: :ids)
.new(self, inserts, on_duplicate: :skip, returning: [:id])
.execute
.pluck('id')
end end
def self.with_search(query) def self.with_search(query)
......
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
require 'spec_helper' require 'spec_helper'
describe Geo::ContainerRepositoryRegistry, :geo do describe Geo::ContainerRepositoryRegistry, :geo do
it_behaves_like 'a BulkInsertSafe model', Geo::ContainerRepositoryRegistry do
let(:valid_items_for_bulk_insertion) { build_list(:container_repository_registry, 10, created_at: Time.zone.now) }
let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined
end
let_it_be(:registry) { create(:container_repository_registry) } let_it_be(:registry) { create(:container_repository_registry) }
describe 'relationships' do describe 'relationships' do
......
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
require 'spec_helper' require 'spec_helper'
describe Geo::DesignRegistry, :geo do describe Geo::DesignRegistry, :geo do
it_behaves_like 'a BulkInsertSafe model', Geo::DesignRegistry do
let(:valid_items_for_bulk_insertion) { build_list(:geo_design_registry, 10, created_at: Time.zone.now) }
let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined
end
let!(:design_registry) { create(:geo_design_registry) } let!(:design_registry) { create(:geo_design_registry) }
describe 'relationships' do describe 'relationships' do
......
# frozen_string_literal: true
require 'spec_helper'
describe Geo::JobArtifactRegistry, :geo do
it_behaves_like 'a BulkInsertSafe model', Geo::JobArtifactRegistry do
let(:valid_items_for_bulk_insertion) { build_list(:geo_job_artifact_registry, 10) }
let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined
end
end
...@@ -6,4 +6,9 @@ describe Geo::LfsObjectRegistry, :geo do ...@@ -6,4 +6,9 @@ describe Geo::LfsObjectRegistry, :geo do
describe 'relationships' do describe 'relationships' do
it { is_expected.to belong_to(:lfs_object).class_name('LfsObject') } it { is_expected.to belong_to(:lfs_object).class_name('LfsObject') }
end end
it_behaves_like 'a BulkInsertSafe model', Geo::LfsObjectRegistry do
let(:valid_items_for_bulk_insertion) { build_list(:geo_lfs_object_registry, 10) }
let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined
end
end end
...@@ -3,9 +3,8 @@ ...@@ -3,9 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe Geo::PackageFileRegistry, :geo, type: :model do describe Geo::PackageFileRegistry, :geo, type: :model do
let_it_be(:registry) { create(:package_file_registry) } it_behaves_like 'a BulkInsertSafe model', Geo::PackageFileRegistry do
let(:valid_items_for_bulk_insertion) { build_list(:package_file_registry, 10, created_at: Time.zone.now) }
specify 'factory is valid' do let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined
expect(registry).to be_valid
end end
end end
...@@ -11,6 +11,11 @@ describe Geo::ProjectRegistry do ...@@ -11,6 +11,11 @@ describe Geo::ProjectRegistry do
subject { registry } subject { registry }
it_behaves_like 'a BulkInsertSafe model', Geo::ProjectRegistry do
let(:valid_items_for_bulk_insertion) { build_list(:geo_project_registry, 10, created_at: Time.zone.now) }
let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined
end
describe 'relationships' do describe 'relationships' do
it { is_expected.to belong_to(:project) } it { is_expected.to belong_to(:project) }
end end
......
...@@ -6,6 +6,11 @@ describe Geo::UploadRegistry, :geo, :geo_fdw do ...@@ -6,6 +6,11 @@ describe Geo::UploadRegistry, :geo, :geo_fdw do
let!(:failed) { create(:geo_upload_registry, :failed) } let!(:failed) { create(:geo_upload_registry, :failed) }
let!(:synced) { create(:geo_upload_registry) } let!(:synced) { create(:geo_upload_registry) }
it_behaves_like 'a BulkInsertSafe model', Geo::UploadRegistry do
let(:valid_items_for_bulk_insertion) { build_list(:geo_upload_registry, 10, created_at: Time.zone.now) }
let(:invalid_items_for_bulk_insertion) { [] } # class does not have any validations defined
end
it 'finds associated Upload record' do it 'finds associated Upload record' do
registry = create(:geo_upload_registry, :attachment, :with_file) registry = create(:geo_upload_registry, :attachment, :with_file)
......
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