Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created January 18, 2011 15:43
Show Gist options
  • Select an option

  • Save ahoward/784611 to your computer and use it in GitHub Desktop.

Select an option

Save ahoward/784611 to your computer and use it in GitHub Desktop.
desc "a task which creates the database user"
task(:createuser) do
require 'yaml'
require 'erb'
path = File.join(Rails.root, 'config', 'database.yml')
config = YAML::load(ERB.new((IO.read(path))).result)
config = config[Rails.env] || config
adapter = config['adapter']
username = config['username']
password = config['password']
case adapter
when /postgres/
require 'pty'
require 'expect'
command = "createuser --superuser #{ username.inspect }"
command += " --password" if password
PTY.spawn(command) do |r,w,pid|
w.sync = true
if password
r.expect(/^\s*password:\s*/i)
w.puts(password)
end
while r.gets
end
end
when /mysql/
warn "you should be using postgresql!"
end
end
task('db:create' => %w( db:createuser )){}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment