How to check if the database exists or not in rails before doing a rake db:setup

后端 未结 7 1318
你的背包
你的背包 2020-12-05 23:58

How to check if the database exists or not in rails before doing a rake db:setup?

I would like to check if a database already exists before a db:create is being done

7条回答
  •  生来不讨喜
    2020-12-06 00:15

    Here is a method that checks if the database already exists:

    def database_exists?
      ActiveRecord::Base.connection
    rescue ActiveRecord::NoDatabaseError
      false
    else
      true
    end
    

    References

    • ActiveRecord::Base.connection
    • ActiveRecord::NoDatabaseError

提交回复
热议问题