namespace :db do
desc "Checks to see if the database exists"
task :exists do
begin
Rake::Task['environment'].invoke
ActiveRecord::Base.connection
rescue
exit 1
else
exit 0
end
end
end
Check if Ruby On Rails Database Exists Using Rake
cpcwood | Last updated:
Rake task which uses ActiveRecord to check if Ruby on Rails database exists.
Can be useful for performing database creations conditionally on docker container creation.
Usage
Add snippet to Ruby on Rails project lib/tasks/db_exists.rake
and ensure the Rakefile
is loading the Rails project environment.
Run using bundle exec rake db:exists