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

编程入门 行业动态 更新时间:2024-10-27 11:15:43
本文介绍了如何检查Rails迁移中的数据库类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我进行了以下迁移,我希望能够检查与该环境相关的当前数据库是否是mysql数据库.如果是mysql,则我要执行特定于数据库的SQL.

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.

我该怎么办?

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

推荐答案

ActiveRecord::Base.connection将为您提供有关boot.rb和environment.rb

ActiveRecord::Base.connection返回很多信息.因此,您必须确切了解要查找的内容.

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

马塞尔指出:

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

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

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

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

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"

更多推荐

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

本文发布于:2023-10-26 23:04:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   数据库   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!