Skip to content

Commit 0f6261d

Browse files
authored
fix(multidb): allow associations across databases to work properly (#572)
1 parent 7047f0c commit 0f6261d

File tree

9 files changed

+50
-4
lines changed

9 files changed

+50
-4
lines changed

app/services/forest_liana/base_getter.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,23 @@ def compute_includes
3232

3333
def optimize_record_loading(resource, records)
3434
instance_dependent_associations = instance_dependent_associations(resource)
35-
eager_loads = @includes - instance_dependent_associations
3635

37-
result = records.eager_load(eager_loads)
36+
preload_loads = @includes.select do |name|
37+
targetModelConnection = resource.reflect_on_association(name).inverse_of&.active_record&.connection
38+
targetModelDatabase = targetModelConnection.current_database if targetModelConnection.respond_to? :current_database
39+
resourceConnection = resource.connection
40+
resourceDatabase = resourceConnection if resourceConnection.respond_to? :current_database
41+
42+
targetModelDatabase != resourceDatabase
43+
end + instance_dependent_associations
44+
45+
result = records.eager_load(@includes - preload_loads)
3846

3947
# Rails 7 can mix `eager_load` and `preload` in the same scope
4048
# Rails 6 cannot mix `eager_load` and `preload` in the same scope
4149
# Rails 6 and 7 cannot mix `eager_load` and `includes` in the same scope
4250
if Rails::VERSION::MAJOR >= 7
43-
result = result.preload(instance_dependent_associations)
51+
result = result.preload(preload_loads)
4452
end
4553

4654
result

spec/dummy/app/models/car.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Car < GarageRecord
2+
belongs_to :driver
3+
end

spec/dummy/app/models/driver.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Driver < UserRecord
2+
has_one :car
3+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class GarageRecord < ApplicationRecord
2+
self.abstract_class = true
3+
connects_to database: { writing: :garage, reading: :garage }
4+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class UserRecord < ApplicationRecord
2+
self.abstract_class = true
3+
connects_to database: { writing: :user, reading: :user }
4+
end

spec/dummy/config/database.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ development:
1919
test:
2020
<<: *default
2121
database: db/test.sqlite3
22+
23+
garage:
24+
<<: *default
25+
database: db/test2.sqlite3
26+
user:
27+
<<: *default
28+
database: db/test3.sqlite3
2229

2330
production:
2431
<<: *default
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class CreateCars < ActiveRecord::Migration[6.0]
2+
def change
3+
Car.connection.create_table :cars do |t|
4+
t.string :model
5+
t.references :driver, index: true
6+
end
7+
end
8+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class CreateDrivers < ActiveRecord::Migration[6.0]
2+
def change
3+
Driver.connection.create_table :drivers do |t|
4+
t.string :firstname
5+
end
6+
end
7+
end

spec/lib/forest_liana/bootstrapper_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ module ForestLiana
3030
Reference,
3131
Town,
3232
Tree,
33-
User
33+
User,
34+
Driver,
35+
Car,
3436
]
3537
end
3638

0 commit comments

Comments
 (0)