Any thoughts on Multi-tenant versus Multi-database apps in Rails

后端 未结 5 1209
情歌与酒
情歌与酒 2021-01-31 00:29

Our app currently spawns a new database for each client. We\'re starting to wonder whether we should consider refactoring this to a multi-tenant system.

What benefits /

5条回答
  •  伪装坚强ぢ
    2021-01-31 01:01

    It really depends upon what you're doing.

    We are making a MIS program for the print industry that tracks inventory, employees, customers, equipment, and does some serious calculations to estimate costs of performing jobs based on a lot of input variables.

    We are anticipating very large databases for each customer, and we currently have 170 tables. Adding another column to almost every table just to store the client_id hurts my brain.

    We are currently in the beta stage of our program, and here are some things that we have encountered:

    • Migrations: A Rails assumption is that you will only have 1 database. You can adapt it for multiple databases, and migrations is one of them. You need a custom rake task to apply migrations to all existing databases. Be prepared to do a lot of trouble shooting because a migration may succeed on one DB, but fail on another.
    • Spawning Databases: How do you create a new db? From a SQL file, copying an existing db, or running all migrations? How do you keep you schema consistent between your table creation system, and your live databases?
    • Connecting to the appropriate database: We use a cookie to store a unique value that maps to the correct DB. We use a before filter in an Authorized controller that inheirits from ActionController that gets the db from that unique value and uses the establish_connection method on a Subclass of ActiveRecord::Base. This allows us to have some models pull from a common db and others from the client's specific db.

    If you have specific questions about any of these, I can help.

提交回复
热议问题