Ruby/Rails - 同时使用 ruby-mysql 和 mysql2
我正在将一个非常大的 Rails 2.3 应用程序从 ruby 1.8 迁移到 1.9。在此过程中,我遇到了一些数据库编码问题,似乎只能通过从旧的 ruby-mysql
gem 迁移到 mysql2
来解决。
这对于所有 ActiveRecord::Base
ORM 之类的查询(@users = User.find(:all, :conditions => {...})
)都效果很好,等),但应用程序还严重依赖于直接查询数据库来解决一些性能相关的问题。看到这样的东西是很常见的:
ActiveRecord::Base.connection.execute(optimized_sql).each_hash do |row|
# do some stuff with row
end
OR
# for specific connections (different servers, etc)
client = Mysql.real_connect(host, username, password, schema)
client.query(tweaked_sql).each_hash do |row|
# do some stuff with row
end
OR
# for batch inserts
client.autocommit(false)
insert_list.each { |insert| client.query(insert) }
client.commit
我应该注意,这个查询主要是在为此编写的指定类文件中完成的,所以不是在控制器、模型等中完成的- 但主要针对 app_root/lib/
下的内容。我似乎也找不到我在旧宝石中使用的东西的许多等效功能。一个很好的例子是类似#autocommit
的方法(用于启用批量查询,如多个 INSERT)。
我想通过使用 gems - mysql2
来平滑过渡,用于所有 ActiveRecord
内容,并使用 ruby-mysql
来直接客户端连接到数据库。然而,当我将两者都包含在应用程序的 Gemfile 中时,Rails 似乎默认使用其中之一。有没有办法将 Gemfile 配置为仅包含 ruby-mysql
但在应用程序加载时不会自动需要它?
如何确保两者都存在,并且仅在我严格想要使用旧 gem 的文件中使用 require 'mysql'
?我还应该采取其他方法吗?一次性转换整个应用程序是一个相当大的风险,我想让我的团队有一些时间来适应并将旧代码从 Mysql 转换到 Mysql2。
谢谢。
I'm migrating a very big Rails 2.3 application from ruby 1.8 to 1.9. Along the way, I've had some database encoding issues that, it seemed, could only be resolved by moving from the old ruby-mysql
gem to mysql2
.
This has worked fine for all ActiveRecord::Base
ORM like queries (@users = User.find(:all, :conditions => {...})
, etc), but the application also relies heavily on querying the DB directly for some performance related issues. It's quite common to see stuff like this:
ActiveRecord::Base.connection.execute(optimized_sql).each_hash do |row|
# do some stuff with row
end
OR
# for specific connections (different servers, etc)
client = Mysql.real_connect(host, username, password, schema)
client.query(tweaked_sql).each_hash do |row|
# do some stuff with row
end
OR
# for batch inserts
client.autocommit(false)
insert_list.each { |insert| client.query(insert) }
client.commit
I should note that this querying is done mostly in designated class files written for that, so not in controllers, models and such - but mostly for stuff under app_root/lib/
. I also cannot seem to find many equivalent features of stuff I used in the old gem. A good example is the #autocommit
like method (to enable batch queries, like multiple INSERTs).
I would like to smooth the transition by using both gems - mysql2
for all ActiveRecord
stuff, and ruby-mysql
for direct client connection to the database. However, when I include both in my app's Gemfile, Rails seems to default to one or the other. Is there a way to configure the Gemfile to only include ruby-mysql
but not automatically require it when the app loads?
How can I make sure both are present, and only use require 'mysql'
in the files where I strictly want to use the old gem? Is there any other approach I should be taking? Converting the entire app in one stroke from is a pretty big risk, and I would like to enable my team some time to adapt and transition old code from Mysql to Mysql2.
thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管 Rails 并不是真正为此而设计的,但您绝对可以
http://robbyonrails.com/articles/2007/10/05/multiple-database-connections-in-ruby-on-rails
Although rails isn't really made for that, you definitely can
http://robbyonrails.com/articles/2007/10/05/multiple-database-connections-in-ruby-on-rails