influitive / apartment

Database multi-tenancy for Rack (and Rails) applications
2.66k stars 464 forks source link

Active Record Relation delaying query till after switching to default shard #647

Open maxstudener opened 4 years ago

maxstudener commented 4 years ago

Steps to reproduce

Apartment::Tenant.switch('tenant-a-production') do
   Term.all
end
   (0.4ms)  use `tenant-a-production`
   (0.4ms)  use `tenant-default-production`
  Term Load (1.2ms)  SELECT `terms`.* FROM `terms`
Apartment::Tenant.switch('tenant-a-production') do
   Term.all.to_a
end
   (0.3ms)  use `tenant-a-production`
  Term Load (1.0ms)  SELECT `terms`.* FROM `terms`
   (0.3ms)  use `tenant-default-production`

Expected behavior

Call something in active record that triggers https://apidock.com/rails/ActiveRecord/Relation/load on models before switching to default database

Actual behavior

Database switches to default tenant then executes query.

  config.use_schemas = true
  config.seed_after_create = true