Rails - Multi tenant application with customization framework

前端 未结 2 1677
日久生厌
日久生厌 2021-01-06 18:52

I am organizing a multi tenant application with a single code base/application using subdomains to detect the tenant, then runs a SET SCHEMA on postgres to do the fun stuff.

2条回答
  •  Happy的楠姐
    2021-01-06 19:48

    If you're talking about switching databases on the fly between requests then I think you're in for a world of hurt, at least when it comes to using ActiveRecord. This will mess up the caching system severely as it remembers things based on ID, not ID + Schema, which could result in cross-contamination.

    Generally, from an architectural perspective, it's best to partition your database internally and have every record scoped accordingly. For instance:

    class Site < ActiveRecord::Base
      # Represents a site or installation of the application
    end
    
    class User < ActiveRecord::Base
      belongs_to :site
    end
    

    Link everything up to the main Site record, or whatever term would best describe the method you use to partition. Maintaining a consistent scope is far easier than switching schema.

    If, for reasons of scaling, you want to split the database in the future, if you've been careful to tag all of a site's associated records with a consistent site_id column, you can easily transpose all of these records to a new database.

提交回复
热议问题