influitive / apartment

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

`seeds.rb` does not use schemas in SQL queries #594

Open rstrobl opened 5 years ago

rstrobl commented 5 years ago

Steps to reproduce

Let's say the seed is currently running for the tenant 'foobar'. These are the commands in seeds.rb and their outcome

> Apartment::Tenant.current
=> "foobar"
> Tenant.all
=> []
>  Apartment::Tenant.switch!('public')
> Tenant.all
=> [#<Tenant:0x007f914815fa50  id: 1, domain: "foobar">]

Expected behavior

The tenant context switches work fine, however in seeds.rb there are no schemas being used for some reason. Expected behaviour should be:

Tenant.all.to_sql => "SELECT \"tenants\".* FROM \"public\".\"tenants\""

Actual behavior

Tenant.all.to_sql => "SELECT \"tenants\".* FROM \"tenants\""

System configuration

Apartment.configure do |config|
  config.excluded_models = %w{ Tenant }
  config.tenant_names = lambda { Tenant.pluck :domain }
  config.use_schemas = true
end
mikecmpbll commented 5 years ago

what's the output of Tenant.table_name?

rakeshh-idexcel commented 4 years ago

Ran into this issue while upgrading my app to rails 5.2, any solutions?

Database: Mysql Apartment version: 2.2.1 Active Record version: 5.2.3 Rails version: 5.2.3 Ruby version: 2.6.0p0