如何检查 Rails 迁移中的数据库类型?

发布于 2024-08-08 13:48:39 字数 431 浏览 6 评论 0原文

我有以下迁移,我希望能够检查当前与环境相关的数据库是否是mysql数据库。如果是mysql,那么我想执行特定于数据库的SQL。

我该怎么办?

class AddUsersFb < ActiveRecord::Migration

  def self.up
    add_column :users, :fb_user_id, :integer
    add_column :users, :email_hash, :string
    #if mysql
    #execute("alter table users modify fb_user_id bigint")
  end

  def self.down
    remove_column :users, :fb_user_id
    remove_column :users, :email_hash
  end

end

I have the following migration and I want to be able to check if the current database related to the environment is a mysql database. If it's mysql then I want to execute the SQL that is specific to the database.

How do I go about this?

class AddUsersFb < ActiveRecord::Migration

  def self.up
    add_column :users, :fb_user_id, :integer
    add_column :users, :email_hash, :string
    #if mysql
    #execute("alter table users modify fb_user_id bigint")
  end

  def self.down
    remove_column :users, :fb_user_id
    remove_column :users, :email_hash
  end

end

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

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

发布评论

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

评论(5

温柔一刀 2024-08-15 13:48:39

更短的通话

ActiveRecord::Base.connection.adapter_name == 'MySQL'

Even more shorter call

ActiveRecord::Base.connection.adapter_name == 'MySQL'
羞稚 2024-08-15 13:48:39

ActiveRecord::Base.connection 将为您提供有关由 boot.rbenvironment.rb 建立的数据库连接的所有信息

ActiveRecord::Base.connection 返回大量信息。所以你必须确切地知道你在寻找什么。

正如 Marcel 指出的:

ActiveRecord::Base.connection.instance_of? 
  ActiveRecord::ConnectionAdapters::MysqlAdapter 

可能是确定您的数据库是否为 MySQL 的最佳方法。

尽管依赖的内部信息可能会在 ActiveRecord 版本之间发生变化,但我更喜欢这样做:

ActiveRecord::Base.connection.instance_values["config"][:adapter] == "mysql"

ActiveRecord::Base.connection will provide you with everything you ever wanted to know about the database connection established by boot.rb and environment.rb

ActiveRecord::Base.connection returns a lot of information. So you've got to know exactly what you're looking for.

As Marcel points out:

ActiveRecord::Base.connection.instance_of? 
  ActiveRecord::ConnectionAdapters::MysqlAdapter 

is probably the best method of determining if your database MySQL.

Despite relying on internal information that could change between ActiveRecord release, I prefer doing it this way:

ActiveRecord::Base.connection.instance_values["config"][:adapter] == "mysql"
最近可好 2024-08-15 13:48:39

有一个 adapter_name< /a> 在 AbstractAdapter 中从 Rails2 开始就存在了。

因此在迁移中使用起来更容易,如下所示:

adapter_type = connection.adapter_name.downcase.to_sym
case adapter_type
when :mysql, :mysql2
  # do the MySQL part
when :sqlite
  # do the SQLite3 part
when :postgresql
  # etc.
else
  raise NotImplementedError, "Unknown adapter type '#{adapter_type}'"
end

There is an adapter_name in AbstractAdapter and that is there since Rails2.

So it's easier to use in the migration like this:

adapter_type = connection.adapter_name.downcase.to_sym
case adapter_type
when :mysql, :mysql2
  # do the MySQL part
when :sqlite
  # do the SQLite3 part
when :postgresql
  # etc.
else
  raise NotImplementedError, "Unknown adapter type '#{adapter_type}'"
end
筑梦 2024-08-15 13:48:39

在 Rails 3 中(也许更早,但我目前正在使用 Rails 3),使用 ActiveRecord::ConnectionAdapters::MysqlAdapter 是一个糟糕的方法,因为它仅在使用的数据库适配器是 MySQL 时才进行初始化。即使您安装了 MySQL gem,如果它不是您的连接类型,该调用也会失败:

Loading development environment (Rails 3.0.3)
>> ActiveRecord::Base.connection.instance_of? ActiveRecord::ConnectionAdapters::MysqlAdapter
NameError: uninitialized constant ActiveRecord::ConnectionAdapters::MysqlAdapter
from (irb):1

因此,我建议 stasl 的答案并使用连接的adapter_name 属性。

In Rails 3, (maybe earlier, but I'm using Rails 3 currently) using ActiveRecord::ConnectionAdapters::MysqlAdapter is a poor way to go about it, as it's only initialized if the database adapter in use is MySQL. Even if you have the MySQL gem installed, if it's not your connection type, that call wil fail:

Loading development environment (Rails 3.0.3)
>> ActiveRecord::Base.connection.instance_of? ActiveRecord::ConnectionAdapters::MysqlAdapter
NameError: uninitialized constant ActiveRecord::ConnectionAdapters::MysqlAdapter
from (irb):1

So, I'd recommend stasl's answer and use the adapter_name property of the connection.

固执像三岁 2024-08-15 13:48:39

这可能会有所帮助:

执行'alter table users修改fb_user_id bigint WHERE USER() = "mysqluser";'

This might help:

execute 'alter table users modify fb_user_id bigint WHERE USER() = "mysqluser";'

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