Multitenancy gem nicely plugs in to activerecord to provide multitenant support within a shared schema as well dedicated schema. It allows multitenancy at two levels, tenant and sub-tenant. For instance you can have SAAS application where the primary tenant could be an organization and sub-tenant will be users in that organization.
- Shared Schema - All tennats will be part of the same instance. Additional columns (tenant_id and sub_tenant_id) will be added to your tables to support multitenancy
- Dedicated Schema - Every tenant will have its own instance of db
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install multitenancy
This gem expects the tenant and sub-tenant values passed in the request header. But you can enhace this to support other logic as well.
You use it in your padrino/sinatra application, add the below lines to the config.ru
Multitenancy.init(:tenant_header => 'X_COMPANY_ID', :sub_tenant_header => 'X_USER_ID') Padrino.use Multitenancy::Filter
You can also outside of a filter or in a standalone application,
tenant = Multitenancy::Tenant.new('flipkart', 'ganeshs') Multitenancy.with_tenant(tenant) do # Your code here end
Any active record query executed within the tenant block, will be tenant/sub-tenant scoped. New records will persist the tenant and sub-tenant ids, find queries will be scoped to teanant and sub-tenant ids. If the sub-tenant id is not specified, it will be de-scoped.
You can pass on the db_type as shared while initializing multitenancy, which is by default set when db_type is not.
Multitenancy.init(:tenant_header => 'X_COMPANY_ID', :sub_tenant_header => 'X_USER_ID', :db_type => :shared)
In your active record models, add the below line specifying the tenant id column and sub-tenant id column. The sub tenant column is optional
This will ensure that all queries performed on this model will be scoped on tenant and sub-tenant.
Dedicated instance config takes in additional optional params,
Multitenancy.init(:tenant_header => 'X_COMPANY_ID', :sub_tenant_header => 'X_USER_ID', :db_type => :shared, :db_config_prefix => 'some_prefix_', :db_config_suffix => 'production')
- Fork it
- Create your feature branch (
git checkout -b my-new-feature)
- Commit your changes (
git commit -am 'Added some feature')
- Push to the branch (
git push origin my-new-feature)
- Create new Pull Request
Multitenancy is released under the MIT license: