influitive / apartment

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

excluded_models does not works with db:setup #664

Open kuzukuzu opened 3 years ago

kuzukuzu commented 3 years ago

Steps to reproduce

Configure excluded models.

Apartment.configure do |config|
  config.excluded_models = ["Company"]
end

db/seeds.rb is as below.

Company.create

Apartment::Tenant.create('some_tenant') do
  puts "companies: #{Company.count}"
end

And call db:setup. (assume that 'public' tenant does not exist)

rails db:setup

Outputs this.

companies: 0

Expected behavior

excluded_models have effect also in db:setup.

Actual behavior

excluded_models have no effect in db:setup.

System configuration

kuzukuzu commented 3 years ago

In my project, this resolves the problem.

class SomeApplication < Rails::Application
  rake_tasks do
    Rake::Task['db:create'].enhance do
      Apartment.connection_class.connection_pool.with_connection do
        Apartment::Tenant.init
      end
    end
  end
end