+echo "Checking database status"
+# If we use `grep -q`, rake will write a backtrace on EPIPE.
+if bundle exec rake db:migrate:status | grep '^database: ' >/dev/null; then
+ echo "Starting db:migrate"
+ bundle exec rake db:migrate
+elif [ 0 -eq ${PIPESTATUS[0]} ]; then
+ # The database exists, but the migrations table doesn't.
+ echo "Setting up database"
+ bundle exec rake db:structure:load db:seed
+else
+ echo "Error: Database is not ready to set up. Aborting." >&2
+ exit 1
+fi