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

后端 未结 5 1294
挽巷
挽巷 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:26

    There is an adapter_name in AbstractAdapter and that is there since Rails2.

    So it's easier to use in the migration like this:

    adapter_type = connection.adapter_name.downcase.to_sym
    case adapter_type
    when :mysql, :mysql2
      # do the MySQL part
    when :sqlite
      # do the SQLite3 part
    when :postgresql
      # etc.
    else
      raise NotImplementedError, "Unknown adapter type '#{adapter_type}'"
    end
    

提交回复
热议问题