Use BulkInsertSafe to bulk insert

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