talira

Active Record Tenanted

Enable a Rails application to have separate databases for each tenant

open sourceMITself-hostablefree

Active Record Tenanted lets a Rails app give each tenant (usually a customer account) its own database, with every request, job and broadcast run inside an explicit tenant context, so code can't accidentally read or send one tenant's data to another. Rails routes connections to the current tenant's database and creates and migrates them as needed. Only SQLite is fully supported so far. 37signals wrote it to give every Fizzy customer a separate SQLite database.

Problems it solves

  • give every customer account its own SQLite database in our Rails app
  • make it hard for a developer to leak one customer's data to another by mistake
  • keep each tenant's data physically separate instead of filtering a shared table by account id
  • turn an existing single-database Rails app into one with a database per tenant

Features

  • separate database per tenant
  • tenant context for requests, jobs and Action Cable
  • safeguards against cross-tenant access
  • creates and migrates tenant databases

Limitations

  • Only SQLite is fully supported: not PostgreSQL or MySQL yet1