Skip to content

Instantly share code, notes, and snippets.

@moniyax
Last active December 21, 2015 15:49
Show Gist options
  • Save moniyax/6328796 to your computer and use it in GitHub Desktop.
Save moniyax/6328796 to your computer and use it in GitHub Desktop.
Script for setting up postgres and configs in a new rails apps.
#!/usr/bin/env ruby
error_message = "Must be run from the root directory of a rails app."
abort(error_message) unless File.exist?("config/database.yml")
app_name = Dir.pwd.split("/")[-1]
dev_db = app_name + "_dev"
test_db = app_name + "_test"
if ARGV[0] == '--delete'
`dropdb #{dev_db} && dropdb #{test_db}`
exit
end
`createdb -E UTF8 -T template1 #{dev_db} \
&& createdb -E UTF8 -T template1 #{test_db}`
db_yml = File.read File.expand_path("~/database.yml.sample")
db_yml.sub!(/\<dev_db\>/, dev_db).sub!(/\<test_db\>/, test_db)
File.open("config/database.yml", "w") { |f| f.puts db_yml }
File.open("Gemfile", "a") { |f| f.puts "gem 'pg'" }
`bundle --local`
`rake db:migrate`
puts "Done Cap'n!"
# TODO:
# - Error handling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment