Skip to content

Instantly share code, notes, and snippets.

@alaxalves
Created May 5, 2018 16:06
Show Gist options
  • Save alaxalves/b6001d1624c2701cf718e7cecfb1db6e to your computer and use it in GitHub Desktop.
Save alaxalves/b6001d1624c2701cf718e7cecfb1db6e to your computer and use it in GitHub Desktop.
Rake task to check if your Rails database exist or not

Usage

  1. Create a file named "db.rake" under rails' "lib/tasks/db.rake"

  2. Place the code there and done!

It's great to use along with docker, since "A started container is not a ready container".

Sample usage can be found in: https://github.com/falko-org/Falko-API

namespace :db do
desc "Checks whether the database exists or not"
task :exists do
begin
# Tries to initialize the application.
# It will fail if the database does not exist
Rake::Task['environment'].invoke
ActiveRecord::Base.connection
rescue
exit 1
else
exit 0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment