Ruby/Rails - 同时使用 ruby​​-mysql 和 mysql2

发布于 2025-01-05 10:08:05 字数 1421 浏览 2 评论 0原文

我正在将一个非常大的 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 技术交流群。

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

发布评论

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

评论(1

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