A Ruby gem for implementing database multi-tenancy in Rails applications using PostgreSQL schemas or separate databases.
Apartment is a Ruby gem that provides database multi-tenancy for Rails applications, enabling data to be partitioned by tenant (e.g., account or company) while allowing some data to remain in a common shared tenant. It solves the problem of isolating tenant data in SaaS applications, ensuring data separation and security. The gem supports PostgreSQL schemas or separate databases for tenant isolation.
Rails developers building multi-tenant SaaS applications, such as platforms serving multiple companies or accounts with isolated data. It's also suitable for teams needing to scale data partitioning across different databases or servers.
Developers choose Apartment for its seamless integration with Rails, support for PostgreSQL schemas (which improves performance and Heroku compatibility), and flexible tenant-switching mechanisms via Rack middleware. Its active maintenance and extensibility (e.g., custom elevators, parallel migrations) make it a robust choice over simpler multi-tenancy solutions.
Database multi-tenancy for Rack (and Rails) applications
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Leverages PostgreSQL schemas for tenant isolation, offering better performance and seamless compatibility with platforms like Heroku, as highlighted in the README for scalable deployments.
Provides multiple built-in elevators (e.g., subdomain, domain) and custom options via Rack middleware, enabling dynamic tenant routing per request without manual code changes.
Includes tools for programmatically creating, dropping, and migrating tenants, along with rake tasks for automation, simplifying lifecycle management in multi-tenant apps.
Allows specific models like User or Company to remain in the global namespace, ensuring shared data accessibility across all tenants while maintaining isolation for others.
Requires careful Rack middleware ordering to avoid issues with gems like Devise or better_errors, adding configuration overhead and potential debugging headaches.
Admits to excessive memory growth on ActiveRecord 4.x due to PostgreSQL data type mapping bugs, forcing upgrades or patches that complicate maintenance.
Optimized for PostgreSQL schemas; other databases (e.g., MySQL) lack equivalent features, limiting flexibility and requiring workarounds for non-PostgreSQL setups.