influitive / apartment

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

Is it possible to use the Apartment gem WITHOUT rails? #602

Open gmhawash opened 5 years ago

gmhawash commented 5 years ago

Hello,

I have a multi-tenant PG application that uses standalone ruby workers to access the database. I'd like to be able to use the Apartment gem to create schemas and switch to schemas from these standalone workers. How is this possible?

Thanks

tachyons commented 5 years ago

Difficult in current architecture as apartment relays on ActiveRecord

mikecmpbll commented 5 years ago

it requires activerecord

gmhawash commented 5 years ago

Active record is fine, but in the file https://github.com/influitive/apartment/blob/a4332c3222210b11f5e8c27fa4c1a18b492e9629/lib/apartment/adapters/abstract_adapter.rb, in the environmentify method, you refer to Rails.env which forces a dependency on Rails which is not necessary for this gem.

mikecmpbll commented 5 years ago

gotcha, thanks

arinhouck commented 4 years ago

@gmhawash Did you work out a solution to get the gem to work within a ruby project?

gmhawash commented 4 years ago

@arinhouck I did not, but I avoid using create_tenant_command, and it is not necessary for Postgresql. If you must, you could define a module in an initialization file as follows:

module Rails
  module_function :env
  def env
     ENV['RACK_ENV']  # or whatever you use to declare an env.
  end
end