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

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

    Here are some bash scripts I made for this purpose:

    Postgres

    if echo "\c $PGDATABASE; \dt" | psql | grep schema_migrations 2>&1 >/dev/null
    then
       bundle exec rake db:migrate
    else
       bundle exec rake db:setup
    fi
    

    Mysql

     if echo "use $MYSQLDATABASE; show tables" | mysql | grep schema_migrations 2>&1 > /dev/null
     then
         bundle exec rake db:migrate
     else
         bundle exec rake db:setup
     fi
    

    These check for the presence of the schema_migrations table to determine whether rake db:setup has been run previously.

提交回复
热议问题