8000 GitHub - AbleHealth/ros-apartment: Database multi-tenancy for Rack (and Rails) applications
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

AbleHealth/ros-apartment

 
 

Repository files navigation

Apartment

Gem Version Code Climate Build Status

Multitenancy for Rails and ActiveRecord

Apartment provides tools to help you deal with multiple tenants in your Rails application. If you need to have certain data sequestered based on account or company, but still allow some data to exist in a common tenant, Apartment can help.

Apartment drop in replacement gem

After having reached out via github issues and email directly, no replies ever came. Since we wanted to upgrade our application to Rails 6 we decided to fork and start some development to support Rails 6. Because we don't have access to the apartment gem itself, the solution was to release it under a different name but providing the exact same API as it was before.

Help wanted

We were never involved with the development of Apartment gem in the first place and this project started out of our own needs. We will be more than happy to collaborate to maintain the gem alive and supporting the latest versions of ruby and rails, but your help is appreciated. Either by reporting bugs you may find or proposing improvements to the gem itself. Feel free to reach out.

Installation

Rails

Add the following to your Gemfile:

gem 'ros-apartment', require: 'apartment'

Then generate your Apartment config file using

bundle exec rails generate apartment:install

This will create a config/initializers/apartment.rb initializer file. Configure as needed using the docs below.

That's all you need to set up the Apartment libraries. If you want to switch tenants on a per-user basis, look under "Usage - Switching tenants per request", below.

NOTE: If using postgresql schemas you must use:

  • for Rails 3.1.x: Rails ~> 3.1.2, it contains a patch that makes prepared statements work with multiple schemas

Usage

Video Tutorial

How to separate your application data into different accounts or companies. GoRails #47

Creating new Tenants

Before you can switch to a new apartment tenant, you will need to create it. Whenever you need to create a new tenant, you can run the following command:

Apartment::Tenant.create('tenant_name')

If you're using the prepend environment config option or you AREN'T using Postgresql Schemas, this will create a tenant in the following format: "#{environment}_tenant_name". In the case of a sqlite database, this will be created in your 'db/' folder. With other databases, the tenant will be created as a new DB within the system.

When you create a new tenant, all migrations will be run against that tenant, so it will be up to date when create returns.

Notes on PostgreSQL

PostgreSQL works slightly differently than other databases when creating a new tenant. If you are using PostgreSQL, Apartment by default will set up a new schema and migrate into there. This provides better performance, and allows Apartment to work on systems like Heroku, which would not allow a full new database to be created.

One can optionally use the full database creation instead if they want, though this is not recommended

Switching Tenants