Advertisement
Guest User

Untitled

a guest
Nov 25th, 2011
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. require 'yaml'
  2. require 'capistrano'
  3. require 'capistrano/cli'
  4.  
  5. module PostgreSQLMethods
  6.  
  7. def createdb(db, user)
  8. run "/path/to/createdb -O #{user} #{db}"
  9. end
  10.  
  11. def createuser(user, password)
  12. cmd = "/path/to/createuser -P -D -A -E #{user}"
  13. run cmd do |channel, stream, data|
  14. if data =~ /^Enter password for new user:/
  15. channel.send_data "#{password}\n"
  16. end
  17. if data =~ /^Enter it again:/
  18. channel.send_data "#{password}\n"
  19. end
  20. end
  21. end
  22.  
  23. def command(sql, database)
  24. run "psql --command=\"#{sql}\" #{database}"
  25. end
  26.  
  27. end
  28.  
  29. Capistrano.plugin :pgsql, PostgreSQLMethods
  30.  
  31. Capistrano.configuration(:must_exist).load do
  32.  
  33. desc "Create PosgreSQL database and user based on config/database.yml"
  34. task :setup_pgsql, :roles => :db, :only => { :primary => true } do
  35. # on_rollback {} TODO
  36. read_config
  37. pgsql.createuser db_user, db_password
  38. pgsql.createdb db_name, db_user
  39. end
  40.  
  41. desc "Setup database"
  42. task :setup_db, :roles => :db, :only => { :primary => true } do
  43. setup_pgsql
  44. end
  45.  
  46. def read_config
  47. db_config = YAML.load_file('config/database.yml')
  48. set :db_user, db_config[rails_env]["username"]
  49. set :db_password, db_config[rails_env]["password"]
  50. set :db_name, db_config[rails_env]["database"]
  51. end
  52.  
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement