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
Here are some bash scripts I made for this purpose:
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
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.