I am building an app where every company has it own private schema(Postgresql).
For every requisition, I set the Postgres search path in a before_action like this:
ActiveRecord::Base.connection.schema_search_path = 'company_id, public'
My doubt is, if I have multiple Unicorn workers, and one worker 'A' set the path, while another work 'B' set the path before worker A has finish, I think it will generate some conflict and 'A' worker could accidently save/read models from the wrong schema, right?
Is there another solution that could work better with Unicorn design?
Edit, schema details:
Each company has many users. Both users and companies tables live in the public schema, the rest( products, clients...) live in private schemas
Edit, more research:
After some research, I found that each database client connection has it own search path. Hence, if I change the search path using one connection, the others won't be affected, so this could work with Unicorn because each request has it own connection, but it will not work with multi-threaded servers like Puma.
But, there are still some problems cited in the answers, like ActiveRecord reloading the schema for each request. I would like to hear the experience of someone who is using this approach in production.