数据库特定的迁移代码 [英] Database Specific Migration Code

查看:81
本文介绍了数据库特定的迁移代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个需要在多个数据库下运行的应用程序.我目前在迁移中有一些代码,只希望在特定的数据库(PostgreSQL和mysql)下运行.有什么办法设置吗?谢谢.

I'm creating an application that needs to run under multiple databases. I currently have some code in a migration that I only want run under specific databases (postgresql and mysql). Any way of setting this up? Thanks.

推荐答案

您的迁移可以访问

Your migration has access to a database connection in connection and the connection has an adapter_name method so you can just ask it what sort of connection it is:

def self.up
    case connection.adapter_name
    when 'PostgreSQL'
        # Do PostgreSQL stuff
    when 'MySQL'
        # Do MySQL stuff
    else
        # Blow up and catch on fire. Or silently ignore it depending on your needs.
    end
end

我不确定我是否正确使用了MySQL适配器名称,但是该技术是否合理,您可以自己轻松地检查MySQL适配器名称.

I'm not sure if I have the MySQL adapter name right but the technique is sound and you can easily check the MySQL adapter name yourself.

这篇关于数据库特定的迁移代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆