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

后端 未结 7 1306
你的背包
你的背包 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:25

    This will return false if the DB doesn't exist or if the connection is not active (at least in Rails 4+).

    ::ActiveRecord::Base.connection_pool.with_connection(&:active?) rescue false
    

提交回复
热议问题