2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe BulkInsertSafe do
|
2020-04-08 14:13:33 +05:30
|
|
|
before(:all) do
|
|
|
|
ActiveRecord::Schema.define do
|
2021-06-08 01:23:25 +05:30
|
|
|
create_table :bulk_insert_parent_items, force: true do |t|
|
|
|
|
t.string :name, null: false
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
create_table :bulk_insert_items, force: true do |t|
|
|
|
|
t.string :name, null: true
|
|
|
|
t.integer :enum_value, null: false
|
|
|
|
t.text :encrypted_secret_value, null: false
|
|
|
|
t.string :encrypted_secret_value_iv, null: false
|
|
|
|
t.binary :sha_value, null: false, limit: 20
|
2020-04-22 19:07:51 +05:30
|
|
|
t.jsonb :jsonb_value, null: false
|
2021-06-08 01:23:25 +05:30
|
|
|
t.belongs_to :bulk_insert_parent_item, foreign_key: true, null: true
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
t.index :name, unique: true
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
create_table :bulk_insert_items_with_composite_pk, id: false, force: true do |t|
|
|
|
|
t.integer :id, null: true
|
|
|
|
t.string :name, null: true
|
|
|
|
end
|
|
|
|
|
|
|
|
execute("ALTER TABLE bulk_insert_items_with_composite_pk ADD PRIMARY KEY (id,name);")
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) do
|
|
|
|
ActiveRecord::Schema.define do
|
|
|
|
drop_table :bulk_insert_items, force: true
|
2021-06-08 01:23:25 +05:30
|
|
|
drop_table :bulk_insert_parent_items, force: true
|
2021-09-04 01:27:46 +05:30
|
|
|
drop_table :bulk_insert_items_with_composite_pk, force: true
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
BulkInsertParentItem = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = :bulk_insert_parent_items
|
|
|
|
self.inheritance_column = :_type_disabled
|
|
|
|
|
|
|
|
def self.name
|
|
|
|
table_name.singularize.camelcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let_it_be(:bulk_insert_parent_item) do
|
|
|
|
BulkInsertParentItem.create!(name: 'parent')
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
let_it_be(:bulk_insert_item_class) do
|
|
|
|
Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = 'bulk_insert_items'
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
include BulkInsertSafe
|
|
|
|
include ShaAttribute
|
|
|
|
|
|
|
|
validates :name, :enum_value, :secret_value, :sha_value, :jsonb_value, presence: true
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
belongs_to :bulk_insert_parent_item
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
sha_attribute :sha_value
|
|
|
|
|
|
|
|
enum enum_value: { case_1: 1 }
|
|
|
|
|
|
|
|
attr_encrypted :secret_value,
|
|
|
|
mode: :per_attribute_iv,
|
|
|
|
algorithm: 'aes-256-gcm',
|
|
|
|
key: Settings.attr_encrypted_db_key_base_32,
|
|
|
|
insecure_mode: false
|
|
|
|
|
|
|
|
default_value_for :enum_value, 'case_1'
|
|
|
|
default_value_for :sha_value, '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12'
|
|
|
|
default_value_for :jsonb_value, { "key" => "value" }
|
|
|
|
|
|
|
|
def self.name
|
|
|
|
'BulkInsertItem'
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def self.valid_list(count, bulk_insert_parent_item: nil)
|
|
|
|
Array.new(count) { |n| new(name: "item-#{n}", secret_value: 'my-secret', bulk_insert_parent_item: bulk_insert_parent_item) }
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.invalid_list(count)
|
2021-03-11 19:13:27 +05:30
|
|
|
Array.new(count) { new(secret_value: 'my-secret') }
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
describe 'BulkInsertItem' do
|
|
|
|
it_behaves_like 'a BulkInsertSafe model' do
|
|
|
|
let(:target_class) { bulk_insert_item_class.dup }
|
|
|
|
let(:valid_items_for_bulk_insertion) { target_class.valid_list(10) }
|
|
|
|
let(:invalid_items_for_bulk_insertion) { target_class.invalid_list(10) }
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when inheriting class methods' do
|
2020-07-28 23:09:34 +05:30
|
|
|
let(:inherited_unsafe_methods_module) do
|
|
|
|
Module.new do
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
after_save -> { "unsafe" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:inherited_safe_methods_module) do
|
|
|
|
Module.new do
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
after_initialize -> { "safe" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it 'raises an error when method is not bulk-insert safe' do
|
2020-07-28 23:09:34 +05:30
|
|
|
expect { bulk_insert_item_class.include(inherited_unsafe_methods_module) }
|
|
|
|
.to raise_error(bulk_insert_item_class::MethodNotAllowedError)
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it 'does not raise an error when method is bulk-insert safe' do
|
2020-07-28 23:09:34 +05:30
|
|
|
expect { bulk_insert_item_class.include(inherited_safe_methods_module) }.not_to raise_error
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
context 'primary keys' do
|
|
|
|
it 'raises error if primary keys are set prior to insertion' do
|
2021-03-11 19:13:27 +05:30
|
|
|
item = bulk_insert_item_class.new(name: 'valid', id: 10, secret_value: 'my-secret')
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect { bulk_insert_item_class.bulk_insert!([item]) }
|
|
|
|
.to raise_error(bulk_insert_item_class::PrimaryKeySetError)
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.bulk_insert!' do
|
|
|
|
it 'inserts items in the given number of batches' do
|
2020-07-28 23:09:34 +05:30
|
|
|
items = bulk_insert_item_class.valid_list(10)
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
expect(ActiveRecord::InsertAll).to receive(:new).twice.and_call_original
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
bulk_insert_item_class.bulk_insert!(items, batch_size: 5)
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'inserts items with belongs_to association' do
|
|
|
|
items = bulk_insert_item_class.valid_list(10, bulk_insert_parent_item: bulk_insert_parent_item)
|
|
|
|
|
|
|
|
bulk_insert_item_class.bulk_insert!(items, batch_size: 5)
|
|
|
|
|
|
|
|
expect(bulk_insert_item_class.last(items.size).map(&:bulk_insert_parent_item)).to eq([bulk_insert_parent_item] * 10)
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
it 'items can be properly fetched from database' do
|
2020-07-28 23:09:34 +05:30
|
|
|
items = bulk_insert_item_class.valid_list(10)
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
bulk_insert_item_class.bulk_insert!(items)
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
attribute_names = bulk_insert_item_class.attribute_names - %w[id created_at updated_at]
|
|
|
|
expect(bulk_insert_item_class.last(items.size).pluck(*attribute_names)).to eq(
|
2020-04-08 14:13:33 +05:30
|
|
|
items.pluck(*attribute_names))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rolls back the transaction when any item is invalid' do
|
|
|
|
# second batch is bad
|
2021-06-08 01:23:25 +05:30
|
|
|
all_items = bulk_insert_item_class.valid_list(10) + bulk_insert_item_class.invalid_list(10)
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
expect do
|
2020-07-28 23:09:34 +05:30
|
|
|
bulk_insert_item_class.bulk_insert!(all_items, batch_size: 2) rescue nil
|
|
|
|
end.not_to change { bulk_insert_item_class.count }
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it 'does nothing and returns an empty array when items are empty' do
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(bulk_insert_item_class.bulk_insert!([])).to eq([])
|
|
|
|
expect(bulk_insert_item_class.count).to eq(0)
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
context 'with returns option set' do
|
|
|
|
context 'when is set to :ids' do
|
|
|
|
it 'return an array with the primary key values for all inserted records' do
|
2020-07-28 23:09:34 +05:30
|
|
|
items = bulk_insert_item_class.valid_list(1)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(bulk_insert_item_class.bulk_insert!(items, returns: :ids)).to contain_exactly(a_kind_of(Integer))
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when is set to nil' do
|
|
|
|
it 'returns an empty array' do
|
2020-07-28 23:09:34 +05:30
|
|
|
items = bulk_insert_item_class.valid_list(1)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect(bulk_insert_item_class.bulk_insert!(items, returns: nil)).to eq([])
|
2020-04-22 19:07:51 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when is set to anything else' do
|
|
|
|
it 'raises an error' do
|
2020-07-28 23:09:34 +05:30
|
|
|
items = bulk_insert_item_class.valid_list(1)
|
2020-04-22 19:07:51 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expect { bulk_insert_item_class.bulk_insert!([items], returns: [:id, :name]) }
|
2020-04-22 19:07:51 +05:30
|
|
|
.to raise_error(ArgumentError, "returns needs to be :ids or nil")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when duplicate items are to be inserted' do
|
2020-07-28 23:09:34 +05:30
|
|
|
let!(:existing_object) { bulk_insert_item_class.create!(name: 'duplicate', secret_value: 'old value') }
|
|
|
|
let(:new_object) { bulk_insert_item_class.new(name: 'duplicate', secret_value: 'new value') }
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
describe '.bulk_insert!' do
|
|
|
|
context 'when skip_duplicates is set to false' do
|
|
|
|
it 'raises an exception' do
|
2020-07-28 23:09:34 +05:30
|
|
|
expect { bulk_insert_item_class.bulk_insert!([new_object], skip_duplicates: false) }
|
2020-04-08 14:13:33 +05:30
|
|
|
.to raise_error(ActiveRecord::RecordNotUnique)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when skip_duplicates is set to true' do
|
|
|
|
it 'does not update existing object' do
|
2020-07-28 23:09:34 +05:30
|
|
|
bulk_insert_item_class.bulk_insert!([new_object], skip_duplicates: true)
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
expect(existing_object.reload.secret_value).to eq('old value')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.bulk_upsert!' do
|
|
|
|
it 'updates existing object' do
|
2020-07-28 23:09:34 +05:30
|
|
|
bulk_insert_item_class.bulk_upsert!([new_object], unique_by: %w[name])
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
expect(existing_object.reload.secret_value).to eq('new value')
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
context 'when a model with composite primary key is inserted' do
|
|
|
|
let_it_be(:bulk_insert_items_with_composite_pk_class) do
|
|
|
|
Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = 'bulk_insert_items_with_composite_pk'
|
|
|
|
|
|
|
|
include BulkInsertSafe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:new_object) { bulk_insert_items_with_composite_pk_class.new(id: 1, name: 'composite') }
|
|
|
|
|
|
|
|
it 'successfully inserts an item' do
|
|
|
|
expect(ActiveRecord::InsertAll).to receive(:new)
|
|
|
|
.with(
|
|
|
|
bulk_insert_items_with_composite_pk_class, [new_object.as_json], on_duplicate: :raise, returning: false, unique_by: %w[id name]
|
|
|
|
).and_call_original
|
|
|
|
|
|
|
|
expect { bulk_insert_items_with_composite_pk_class.bulk_insert!([new_object]) }.to(
|
|
|
|
change(bulk_insert_items_with_composite_pk_class, :count).from(0).to(1)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|