Database Specific Migration Code [duplicate]

流过昼夜 提交于 2019-11-30 20:45:33

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6178303/database-specific-migration-code

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