Skip to content

Commit f39ab8e

Browse files
author
dmitrii.buk
committed
each_record primary_key fix
1 parent c2516eb commit f39ab8e

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
umbrellio-utils (1.6.0)
4+
umbrellio-utils (1.6.1)
55
memery (~> 1)
66

77
GEM

lib/umbrellio_utils/database.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_violated_constraint_name(exception)
2525
def each_record(dataset, primary_key: nil, **options, &block)
2626
primary_key = primary_key_from(dataset, primary_key:)
2727

28-
with_temp_table(dataset, **options) do |ids|
28+
with_temp_table(dataset, primary_key:, **options) do |ids|
2929
rows = ids.map { |id| row(id.is_a?(Hash) ? id.values : [id]) }
3030
dataset.model.where(row(primary_key) => rows).reverse(row(primary_key)).each(&block)
3131
end

lib/umbrellio_utils/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module UmbrellioUtils
4-
VERSION = "1.6.0"
4+
VERSION = "1.6.1"
55
end

spec/support/database.rb

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
column :email, :text
2424
end
2525

26+
DB.drop_table? :users_without_pk
27+
DB.create_table :users_without_pk do
28+
column :id, :integer
29+
column :email, :text
30+
end
31+
2632
DB.drop_table? :complex_users
2733
DB.create_table :complex_users do
2834
column :geo, :text
@@ -37,6 +43,12 @@ def skip_table_sync?
3743
end
3844
end
3945

46+
class UserWithoutPk < Sequel::Model(:users_without_pk)
47+
def skip_table_sync?
48+
false
49+
end
50+
end
51+
4052
class ComplexUser < Sequel::Model(:complex_users)
4153
def skip_table_sync?
4254
false

spec/umbrellio_utils/database_spec.rb

+25
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,31 @@
136136
end
137137
end
138138

139+
context "when primary key does not exist in table" do
140+
before do
141+
UserWithoutPk.multi_insert(users_data)
142+
end
143+
144+
let(:users_data) do
145+
Array.new(10) { |index| Hash[id: index + 1, email: "user#{index + 1}@email.com"] }
146+
end
147+
let(:options) { Hash[primary_key: :id] }
148+
149+
subject(:result_emails) do
150+
users = []
151+
152+
described_class.each_record(UserWithoutPk.dataset, **options) do |user|
153+
users << user
154+
end
155+
156+
users.map(&:email)
157+
end
158+
159+
it "runs correctly with explicit primary_key param" do
160+
expect(result_emails).to eq(reversed_emails)
161+
end
162+
end
163+
139164
context "some invalid option provided" do
140165
let(:options) { Hash[invalid: 1] }
141166

0 commit comments

Comments
 (0)