{"id":13879883,"url":"https://github.com/citusdata/activerecord-multi-tenant","last_synced_at":"2025-05-13T20:17:15.671Z","repository":{"id":10971704,"uuid":"67840337","full_name":"citusdata/activerecord-multi-tenant","owner":"citusdata","description":"Rails/ActiveRecord support for distributed multi-tenant databases like Postgres+Citus","archived":false,"fork":false,"pushed_at":"2024-11-11T09:56:11.000Z","size":702,"stargazers_count":741,"open_issues_count":39,"forks_count":105,"subscribers_count":48,"default_branch":"master","last_synced_at":"2025-05-13T09:06:38.076Z","etag":null,"topics":["activerecord","citus","multi-tenant","rails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/citusdata.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-10T00:05:34.000Z","updated_at":"2025-05-11T09:17:36.000Z","dependencies_parsed_at":"2023-07-12T16:23:58.341Z","dependency_job_id":"a1cb73b6-d05a-4dc5-b187-88a85d2d4220","html_url":"https://github.com/citusdata/activerecord-multi-tenant","commit_stats":{"total_commits":343,"total_committers":58,"mean_commits":5.913793103448276,"dds":0.6997084548104957,"last_synced_commit":"0ac43aa98334a29ed3e5b33e0bcc5ee281f97254"},"previous_names":["citusdata/citus-rails"],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/citusdata%2Factiverecord-multi-tenant","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/citusdata%2Factiverecord-multi-tenant/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/citusdata%2Factiverecord-multi-tenant/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/citusdata%2Factiverecord-multi-tenant/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/citusdata","download_url":"https://codeload.github.com/citusdata/activerecord-multi-tenant/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254020653,"owners_count":22000757,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","citus","multi-tenant","rails"],"created_at":"2024-08-06T08:02:37.458Z","updated_at":"2025-05-13T20:17:10.661Z","avatar_url":"https://github.com/citusdata.png","language":"Ruby","readme":"# activerecord-multi-tenant \n[![Build Status](https://github.com/citusdata/activerecord-multi-tenant/actions/workflows/active-record-multi-tenant-tests.yml/badge.svg)](https://github.com/citusdata/activerecord-multi-tenant/actions/workflows/active-record-multi-tenant-tests.yml) [![codecov](https://codecov.io/gh/citusdata/activerecord-multi-tenant/branch/master/graph/badge.svg?token=rw0TsEk4Ld)](https://codecov.io/gh/citusdata/activerecord-multi-tenant) [ ![Gems Version](https://img.shields.io/gem/v/activerecord-multi-tenant.svg)](https://rubygems.org/gems/activerecord-multi-tenant)[ ![Gem Download Count](https://img.shields.io/gem/dt/activerecord-multi-tenant.svg)](https://rubygems.org/gems/activerecord-multi-tenant) [![Documentation Status](https://readthedocs.org/projects/activerecord-multi-tenant/badge/?version=latest)](https://activerecord-multi-tenant.readthedocs.io/en/latest/?badge=latest) \n\nIntroduction Post: https://www.citusdata.com/blog/2017/01/05/easily-scale-out-multi-tenant-apps/\n\nActiveRecord/Rails integration for multi-tenant databases, in particular the open-source [Citus](https://github.com/citusdata/citus) extension for PostgreSQL.\n\nEnables easy scale-out by adding the tenant context to your queries, enabling the database (e.g. Citus) to efficiently route queries to the right database node.\n\n## Installation\n\nAdd the following to your Gemfile:\n\n```ruby\ngem 'activerecord-multi-tenant'\n```\n\n## Supported Rails versions\n\nAll Ruby on Rails versions starting with 6.0 or newer (up to 7.0) are supported.\n\nThis gem only supports ActiveRecord (the Rails default ORM), and not alternative ORMs like Sequel.\n\n## Usage\n\nIt is required that you add `multi_tenant` definitions to your model in order to have full support for Citus, in particular when updating records.\n\nIn the example of an analytics application, sharding on `customer_id`, annotate your models like this:\n\n```ruby\nclass PageView \u003c ActiveRecord::Base\n  multi_tenant :customer\n  belongs_to :site\n\n  # ...\nend\n\nclass Site \u003c ActiveRecord::Base\n  multi_tenant :customer\n  has_many :page_views\n\n  # ...\nend\n```\n\nand then wrap all code that runs queries/modifications in blocks like this:\n\n```ruby\ncustomer = Customer.find(session[:current_customer_id])\n# ...\nMultiTenant.with(customer) do\n  site = Site.find(params[:site_id])\n  site.update! last_accessed_at: Time.now\n  site.page_views.count\nend\n```\n\nInside controllers you can use a before_action together with set_current_tenant, to set the tenant for the current request:\n\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  set_current_tenant_through_filter # Required to opt into this behavior\n  before_action :set_customer_as_tenant\n\n  def set_customer_as_tenant\n    customer = Customer.find(session[:current_customer_id])\n    set_current_tenant(customer)\n  end\nend\n```\n\n## Rolling out activerecord-multi-tenant for your application (write-only mode)\n\nThe library relies on tenant_id to be present and NOT NULL for all rows. However,\nits often useful to have the library set the tenant_id for new records, and then backfilling\ntenant_id for existing records as a background task.\n\nTo support this, there is a write-only mode, in which tenant_id is not included in queries,\nbut only set for new records. Include the following in an initializer to enable it:\n\n```ruby\nMultiTenant.enable_write_only_mode\n```\n\nOnce you are ready to enforce tenancy, make your tenant_id column NOT NULL and simply remove that line.\n\n## Frequently Asked Questions\n\n* **What if I have a table that doesn't relate to my tenant?** (e.g. templates that are the same in every account)\n\n  We recommend not using activerecord-multi-tenant on these tables. In case only some records in a table are not associated to a tenant (i.e. your templates are in the same table as actual objects), we recommend setting the tenant_id to 0, and then using MultiTenant.with(0) to access these objects.\n\n* **What if my tenant model is not defined in my application?**\n\n  The tenant model does not have to be defined. Use the gem as if the model was present. `MultiTenant.with` accepts either a tenant id or model instance.\n\n## Credits\n\nThis gem was initially based on [acts_as_tenant](https://github.com/ErwinM/acts_as_tenant), and still shares some code. We thank the authors for their efforts.\n\n## License\n\nCopyright (c) 2018, Citus Data Inc.\u003cbr\u003e\nLicensed under the MIT license, see LICENSE file for details.\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcitusdata%2Factiverecord-multi-tenant","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcitusdata%2Factiverecord-multi-tenant","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcitusdata%2Factiverecord-multi-tenant/lists"}