我应该使用多个数据库吗?

发布于 2024-08-24 12:07:37 字数 299 浏览 8 评论 0原文

我即将使用 Ruby on Rails 创建一个应用程序,我想使用多个数据库,基本上是一个会计应用程序,每个用户都有多个公司。我想为每个公司创建一个数据库

我发现这篇文章http://programmerassist .com/article/302 但我想阅读更多关于这个问题的想法。 我必须在 MySQL 和 PosgreSQL 之间做出选择,哪个数据库更适合我的问题。

I am about to create an application with Ruby on Rails and I would like to use multiple databases, basically is an accounting app that will have multiple companies for each user. I would like to create a database for each company

I found this post http://programmerassist.com/article/302
But I would like to read more thoughts about this issue.
I have to decide between MySQL and PosgreSQL, which database might fit better my problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

银河中√捞星星 2024-08-31 12:07:37

有多种选项可用于处理多租户应用程序。

首先,您可以向表中添加范围(按照 Chad Birch 的建议 - 使用 company_id)。对于大多数用例来说这都很好。如果您正在处理安全/私密的数据(例如会计信息),您需要非常小心地进行测试,以确保数据保持私密性。

您可以使用多个数据库来运行系统。您可以拥有一个为每个客户端使用数据库的应用程序,也可以为每个客户端拥有一个单独的应用程序。为每个客户端运行一个数据库会稍微影响 Rails 的性能,但它是可行的。根据您拥有的客户端数量和负载预期,我实际上建议看看运行单个应用程序。通过对部署设置(capistrano、chef、puppet 等)进行一些工作,您可以使其成为一个非常简化的过程。每个客户端都在完全独特的环境中运行,如果特定客户端负载较高,您可以将它们分派到自己的服务器。

如果使用 PostgreSQL,您可以使用模式执行类似的操作。
PostgresQL 架构 提供了一种非常方便的方法来将数据与不同的数据隔离开来。客户。数据库包含一个或多个命名模式,而这些模式又包含表。您需要在迁移和部署中添加一些智能功能,但它效果非常好。

在 Rails 应用程序中,您可以将过滤器附加到请求,以打开或关闭当前用户的架构。

像这样的东西:

before_filter :set_app

def set_app
  current_app = App.find_by_subdomain(...)
  schema = current_app.schema

  set_schema_path(schema)
end 


def set_schema_path(schema)
  connection = ActiveRecord::Base.connection
  connection.execute("SET search_path TO #{schema}, #{connection.schema_search_path}")
end

def  reset_schema_path
  connection = ActiveRecord::Base.connection
  connection.execute("SET search_path TO #{connection.schema_search_path}")
end

There are several options for handling a multi-tenant app.

Firstly, you can add a scope to your tables (as suggested by Chad Birch - using a company_id). For most use-cases this is fine. If you are handling data that is secure/private (such as accounting information) you need to be very careful about your testing to ensure data remains private.

You can run your system using multiple databases. You can have a single app that uses a database for each client, or you can have actually have a seperate app for each client. Running a database for each client cuts a little against the grain in rails, but it is doable. Depending on the number of clients you have, and the load expectations, I would actually suggest having a look at running individual apps. With some work on your deployment setup (capistrano, chef, puppet, etc) you can make this a very streamlined process. Each client runs in a completely unique environment, and if a particular client has high loads you can spin them out to their own server.

If using PostgreSQL, you can do something similar using schemas.
PostgresQL schemas provide a very handy way of islolating your data from different clients. A database contains one or more named schemas, which in turn contain tables. You need to add some smarts to your migrations and deployments, but it works really well.

Inside your Rails application, you attach filters to the request that switch the current user's schema on or off.

Something like:

before_filter :set_app

def set_app
  current_app = App.find_by_subdomain(...)
  schema = current_app.schema

  set_schema_path(schema)
end 


def set_schema_path(schema)
  connection = ActiveRecord::Base.connection
  connection.execute("SET search_path TO #{schema}, #{connection.schema_search_path}")
end

def  reset_schema_path
  connection = ActiveRecord::Base.connection
  connection.execute("SET search_path TO #{connection.schema_search_path}")
end
风筝在阴天搁浅。 2024-08-31 12:07:37

有关多个数据库的答案的问题在于,这些答案来自不需要多个数据库或没有多个数据库经验的人。第二个问题是,某些数据库不允许在多个数据库之间切换,包括允许用户进行自己的备份和恢复,以及扩展以将某些用户指向不同的数据服务器。这是一个有用视频的链接
http ://aac2009.confreaks.com/06-feb-2009-14-30-writing-multi-tenant-applications-in-rails-guy-naor.html

此链接将有助于使用 Ruby on Rails 和 Postgresql 。

我目前有一个多租户、多数据库、多用户(多次登录具有不同访问级别的同一租户),并且是一个在线 SaaS 应用程序。实际上有两种应用程序,一种是会计类别,另一种是银行业。这两个应用程序都建立在相同的结构和方法之上。客户端用户(租户)可以在该用户登录的情况下切换数据库。代理用户(例如税务会计师)只能为其客户在数据库之间进行切换。超级用户可以切换到任何数据库。有一个数据字典,即只有一处定义表和列。有全局数据和局部数据。全局数据,例如可供所有人使用的主会计科目表(只读)。本地数据是用户的数据库。新用户可以获得主数据库的克隆。有多个克隆可供选择。超级用户可以维护克隆数据库。

问题在于它是在COBOL中并使用ISAM文件并使用CGI方法。这样做的问题是 a) 人们认为 COBOL 已经过时,b) 需要经过培训的人员,c) 价格和 d) 在线帮助。否则它会起作用并且我对此很满意。

所以我正在研究用什么来取代它以及这是什么雷区。

The problem with answers about multiple databases is when they come from people who don't have a need or experience with multiple databases. The second problem is that some databases just don't allow for switching between multiple databases, including allowing users to do their own backup and recovery and including scaling to point some users to a different data server. Here is a link to a useful video
http://aac2009.confreaks.com/06-feb-2009-14-30-writing-multi-tenant-applications-in-rails-guy-naor.html

This link will help with Ruby on Rails with Postgresql.

I currently have a multi-tenant, multi-database, multi-user (many logons to the same tenant with different levels of access), and being an online SaaS application. There are actually two applications one is in the accounting category and the other is banking. Both Apps are built on the same structure and methods. A client-user (tenant) can switch databases under that user's logon. An agent-user such as a tax accountant can switch between databases for his clients only. A super-user can switch to any database. There is one data dictionary i.e. only one place where tables and columns are defined. There is global data and local data. Global data such as a master chart-of-accounts which is available to everyone (read only). Local data is the user's database. A new user can get a clone of a master database. There are multiple clones to choose from. A super-user can maintain the clone databases.

The problem is that it is in COBOL and uses ISAM files and uses the CGI method. The problem with this is a) there is a perception that COBOL is outdated, b) getting trained people, c) price and d) online help. Otherwise it works and I'm happy with it.

So I'm researching what to replace it with and what a minefield that is.

清音悠歌 2024-08-31 12:07:37

它已经过去了,为此决定使用 PostgreSQL 模式,制作多租户应用程序,我有一个名为 common 的模式,用于存储相关数据。

# app/models/organisation.rb
class Organisation < ActiveRecord::Base
  self.table_name = 'common.organisations'
  # set relationships as usual
end

# app/models/user.rb    
class User < ActiveRecord::Base
  self.table_name = 'common.users'
  # set relationships as usual
end

然后,对于迁移,我已经通过这个优秀的教程完成了这一点。 http://timnew. github.com/blog/2012/07/17/use-postgres-multiple-schema-database-in-rails/ 使用这个,这比我在其他地方看到的要好,甚至像 Ryan Bates 那样在铁路广播上。

创建新组织时,会使用该组织的子域名称创建一个新架构。我过去读过,使用不同的模式不是一个好主意,但这取决于您正在做的工作,这个应用程序几乎没有社交组件,所以它很适合。

It has past time and the decission for this has been to use PostgreSQL schemas, making multitenant applications, I have a schema called common where related data is stored.

# app/models/organisation.rb
class Organisation < ActiveRecord::Base
  self.table_name = 'common.organisations'
  # set relationships as usual
end

# app/models/user.rb    
class User < ActiveRecord::Base
  self.table_name = 'common.users'
  # set relationships as usual
end

Then for migrations I have done that with this excellent tutorial. http://timnew.github.com/blog/2012/07/17/use-postgres-multiple-schema-database-in-rails/ use this, this is way better than what I saw in other places even the way Ryan Bates did on railscasts.

When a new organisation is created then a new schema is created with the name of the subdomain the organisation. I have read in the past that it's not a good idea to use different schemas but it depends on the job you are doing, this app has almost no soccial component so it's a good fit.

女中豪杰 2024-08-31 12:07:37

不,您不应该使用多个数据库。

我不太确定要给你什么建议,看来你对数据库设计有一些非常基本的误解,你可能想先自学数据库的基础知识,然后再进一步。

您很可能只想在表中添加“公司 ID”类型列来标识特定记录属于哪个公司。

No, you shouldn't use multiple databases.

I'm not really sure what advice to give you though, it seems like you have some very basic misunderstandings about database design, you may want to educate yourself on the basics of databases first, before going further.

You most likely just want to add a "company id" type column to your tables to identify which company a particular record belongs to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文