Skip to content

Commit aebd554

Browse files
authored
Possibility to eager load associations in each_record method (#39)
1 parent 298f475 commit aebd554

File tree

6 files changed

+66
-10
lines changed

6 files changed

+66
-10
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
- name: Run specs
5555
run: bundle exec ci-helper RunSpecs
5656

57-
- name: Coveralls
58-
uses: coverallsapp/github-action@master
59-
with:
60-
github-token: ${{ secrets.GITHUB_TOKEN }}
57+
# - name: Coveralls
58+
# uses: coverallsapp/github-action@master
59+
# with:
60+
# github-token: ${{ secrets.GITHUB_TOKEN }}

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
umbrellio-utils (1.7.1)
4+
umbrellio-utils (1.8.0)
55
memery (~> 1)
66

77
GEM
@@ -382,4 +382,4 @@ DEPENDENCIES
382382
yard
383383

384384
BUNDLED WITH
385-
2.5.18
385+
2.7.1

lib/umbrellio_utils/database.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,15 @@ def get_violated_constraint_name(exception)
2525

2626
def each_record(dataset, primary_key: nil, **options, &block)
2727
primary_key = primary_key_from(dataset, primary_key:)
28+
eager_tables = Array(options.delete(:eager_load))
2829

2930
with_temp_table(dataset, primary_key:, **options) do |ids|
3031
rows = ids.map { |id| row(id.is_a?(Hash) ? id.values : [id]) }
31-
dataset.model.where(row(primary_key) => rows).reverse(row(primary_key)).each(&block)
32+
records = dataset.model
33+
.eager(eager_tables)
34+
.where(row(primary_key) => rows)
35+
.reverse(row(primary_key)).all
36+
records.each(&block)
3237
end
3338
end
3439

lib/umbrellio_utils/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module UmbrellioUtils
4-
VERSION = "1.7.1"
4+
VERSION = "1.8.0"
55
end

spec/support/database.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
begin
66
db_name = "umbrellio_utils_test"
7-
DB = Sequel.connect(ENV.fetch("DB_URL", "postgres://localhost/#{db_name}"))
7+
DB = Sequel.connect(ENV.fetch("DB_URL", "postgres:///#{db_name}"))
88
rescue Sequel::DatabaseConnectionError => error
99
puts error
1010
abort "You probably need to create a test database. " \
@@ -17,7 +17,7 @@
1717

1818
DB.extension :batches
1919

20-
DB.drop_table? :users
20+
DB.drop_table? :users, cascade: true
2121
DB.create_table :users do
2222
primary_key :id
2323
column :email, :text
@@ -37,6 +37,12 @@
3737
primary_key %i[geo nick]
3838
end
3939

40+
DB.drop_table? :user_tokens
41+
DB.create_table :user_tokens do
42+
primary_key :id
43+
foreign_key :user_id, :users
44+
end
45+
4046
class User < Sequel::Model(:users)
4147
def skip_table_sync?
4248
false
@@ -54,3 +60,11 @@ def skip_table_sync?
5460
false
5561
end
5662
end
63+
64+
class UserToken < Sequel::Model(:user_tokens)
65+
many_to_one :user, class: "User", key: :user_id
66+
67+
def skip_table_sync?
68+
false
69+
end
70+
end

spec/umbrellio_utils/database_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,42 @@
176176
expect(result_emails).to eq(reversed_emails)
177177
end
178178
end
179+
180+
context "with eager_load" do
181+
let(:options) { Hash[eager_load: [:user]] }
182+
183+
let!(:users) { User.all }
184+
let(:tokens_data) { Array.new(10) { |i| { user_id: users[i].id } } }
185+
186+
let!(:db_logger) do
187+
logger = Class.new(Logger) do
188+
attr_accessor :queries
189+
190+
def add(_, _, msg)
191+
self.queries ||= []
192+
queries << msg if msg.include?('FROM "users"')
193+
end
194+
end.new(nil)
195+
logger.tap { |x| DB.loggers << x }
196+
end
197+
198+
subject(:result_users) do
199+
tokens = []
200+
201+
described_class.each_record(UserToken.dataset, **options) do |token|
202+
tokens << token
203+
end
204+
205+
tokens.map(&:user)
206+
end
207+
208+
before { UserToken.multi_insert(tokens_data) }
209+
after { DB.loggers.pop }
210+
211+
it "preloads users" do
212+
expect(result_users.map(&:id)).to eq(users.map(&:id).reverse)
213+
expect(db_logger.queries.size).to eq(1)
214+
end
215+
end
179216
end
180217
end

0 commit comments

Comments
 (0)