How do I check the Database type in a Rails Migration?

后端 未结 5 1304
挽巷
挽巷 2020-12-09 01:20

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

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 01:48

    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.

提交回复
热议问题