简体   繁体   English

具有DB2和多个模式的Rails

[英]Rails with DB2 and multiple schemas

I have a 'legacy' DB2 database that has many other applications and users. 我有一个“传统” DB2数据库,其中包含许多其他应用程序和用户。 Trying to experiment with a rails app. 尝试尝试使用Rails应用程序。 Got everything working great with the ibm_db driver. 使用ibm_db驱动程序,一切正常。

Problem is that I have some tables like schema1.products, schema1.sales and other tables like schema2.employees and schema2.payroll. 问题是我有一些表,例如schema1.products,schema1.sales和其他表,例如schema2.employees和schema2.payroll。

In the ibm_db adapter connection, I specify a schema, like schema1 or schema2, and I can work within that one schema, but I need to be able to easily (and transparently) reference both schemas basically interchangeably. 在ibm_db适配器连接中,我指定了一个模式(例如schema1或schema2),并且可以在该模式下工作,但是我需要能够轻松(透明)地基本上互换地引用这两个模式。 I don't want to break the other apps, and the SQL I would normally write against DB2 doesn't have any of these restrictions (schemas can be mixed in SQL against DB2 without any trouble at all). 我不想破坏其他应用程序,而且我通常针对DB2编写的SQL没有任何这些限制(方案可以与DB2混合在SQL中,完全没有任何麻烦)。

I would like to just specify table names as "schema1.products" for example and be done with it, but that doesn't seem to jive with the "rails way" of going about it. 我只想将表名指定为“ schema1.products”并完成该操作,但这似乎与“轨道方式”无关。

Suggestions? 有什么建议吗?

Schemas in DB2 are a very handy way to provide separate namespace to different applications. DB2中的模式是为不同的应用程序提供单独的名称空间的一种非常方便的方法。 For example, you can separate all database objects for an application called "recruiting" from say application called "payroll". 例如,您可以将名为“招聘”的应用程序的所有数据库对象与名为“工资”的应用程序分开。 You can have objects (tables, views, procedures etc.) with the same name reside in multiple schemas and not colide with one another. 您可以使具有相同名称的对象(表,视图,过程等)驻留在多个模式中,而不相互冲突。 Having your application set a schema is a handy way for it to say "hey, I am a payroll and I only want to work with my objects". 让您的应用程序设置模式是一种方便的方式,它可以说“嘿,我是一个工资单,我只想使用我的对象”。 So, what happens when you want to work with objects owned by another application? 因此,当您要使用另一个应用程序拥有的对象时会发生什么? Well, in traditional procedural programming languages your application code would explicitly specify the schema when referencing an object in another schema or you would just do a SET CURRENT SCHEMA to switch to another schema. 嗯,在传统的过程编程语言中,当您在另一个模式中引用一个对象时,您的应用程序代码会明确指定该模式,或者您只需执行SET CURRENT SCHEMA即可切换到另一个模式。 The problem with ORMs like ActiveRecord in Ruby on Rails is that you are not supposed to use SQL ie you don't have a good "Rails way" to specify schema when referencing an object. 像Ruby on Rails中的ActiveRecord这样的ORM的问题在于,您不应该使用SQL,即在引用对象时没有好的“ Rails方式”来指定架构。 You can use find_by_sql and qualify your objects in the SQL statement but this is not what RoR people will consider to be good Rails. 您可以使用find_by_sql并在SQL语句中限定对象,但这不是RoR人员认为的好Rails。 You can fix things on the DB2 side. 您可以在DB2方面进行修复。 You can define a view per table in the "foreign" schema but you will have to take care to name the view so that it does not colide with what you already have in your primary schema. 您可以在“外部”架构中为每个表定义一个视图,但是您必须小心命名该视图,以免与主架构中已有的视图冲突。 And, when you do that, you will undoubtedly create names that are not true Rails names. 并且,当您这样做时,无疑将创建不是真正的Rails名称的名称。 Rails people are very proud of the "Rails way". Rails的人为“铁路方式”感到非常自豪。 It makes it very easy to create new applications. 这使得创建新应用程序非常容易。 Rails is really awesome in this space. Rails在这个领域确实很棒。 However, when it comes to integration with what is already out there Rails ... how do we say it ... sucks. 但是,当与现有的Rails集成时……我们怎么说呢……很糟糕。 I suggest you will have to accept to do things that are not the best examples of the Rails Way if you want to work with existing database structures. 如果您要使用现有的数据库结构,我建议您必须接受做不是Rails Way最好例子的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM