Created
January 18, 2011 15:43
-
-
Save ahoward/784611 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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