IMHO, we should really careful when deciding to use schemas fo multi-tenants. It is a one-trip ticket, you need to pay so much if you regrets.
I'm working on a app that uses postgres schemas for multi-tenants. And one of the most important requirement in this new release, is to share data between schema.
And for some reason, we cannot get rid of the schemas.
As a result, we need to switch between schemas dynamically according to the context. In some special cases, we need switch between schemas for several times in one request.
This really causes a lot of pain for us.
We try our best to move the common data into public schema, and leave the rest in schemas. Then we can use the search_path to enable us make query across schemas.
But the really pain is that, ActiveRecord caches the query result in its DbAdapter(aka ActiveRecord::Base.connection).
When schema switched, the cached is not cleared. Then you will get wrong data.
If you manually clear the cached, it hurts performance, and you might the association of your model works improperly, which strongly depends on the caches.
And another issue is that the schemas.rb, totally not work, since it cannot distinguish the schemas. So the gems depends on it, won't work properly, e.g. annotate gem.
At last, I wrote an utility called MultiSchema to help us to switch between schemas, hope it helps. You can find it in this post
IMHO, we should really careful when deciding to use schemas fo multi-tenants. It is a one-trip ticket, you need to pay so much if you regrets.
I'm working on a app that uses postgres schemas for multi-tenants. And one of the most important requirement in this new release, is to share data between schema.
And for some reason, we cannot get rid of the schemas.
As a result, we need to switch between schemas dynamically according to the context. In some special cases, we need switch between schemas for several times in one request.
This really causes a lot of pain for us.
We try our best to move the common data into public schema, and leave the rest in schemas. Then we can use the search_path to enable us make query across schemas.
But the really pain is that, ActiveRecord caches the query result in its DbAdapter(aka ActiveRecord::Base.connection).
When schema switched, the cached is not cleared. Then you will get wrong data.
If you manually clear the cached, it hurts performance, and you might the association of your model works improperly, which strongly depends on the caches.
And another issue is that the schemas.rb, totally not work, since it cannot distinguish the schemas. So the gems depends on it, won't work properly, e.g. annotate gem.
At last, I wrote an utility called MultiSchema to help us to switch between schemas, hope it helps. You can find it in this post