Database Specific Migration Code [duplicate]

萝らか妹 提交于 2019-12-01 01:13:39

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!