Guest User

Untitled

a guest
Mar 7th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. desc 'Creates the databases defined in your config/database.yml (unless they already exist)'
  2. task 'db:create' => [ 'environment' ] do
  3. ActiveRecord::Base.configurations.each_value do |config|
  4. begin
  5. ActiveRecord::Base.establish_connection(config)
  6. ActiveRecord::Base.connection
  7. rescue
  8. case config["adapter"]
  9. when "mysql" then
  10. ActiveRecord:: Base.establish_connection(config.merge({ "database" => nil }))
  11. ActiveRecord::Base.connection.create_database(config["database"])
  12. ActiveRecord::Base.establish_connection(config)
  13. when "postgresql" then
  14. `createdb \"#{config["database"]}\" -E utf8`
  15. else
  16. # do nothing
  17. end
  18. end
  19. end
  20. ActiveRecord::Base.establish_connection (ActiveRecord::Base.configurations[(RAILS_ENV or "development")])
  21. end
  22.  
  23. desc 'Drops the database for your currenet RAILS_ENV as defined in config/database.yml'
  24. task 'db:drop' => [ 'environment' ] do
  25. config = ActiveRecord::Base.configurations[(RAILS_ENV or "development")]
  26. case config["adapter"]
  27. when "mysql" then
  28. begin
  29. ActiveRecord::Base.establish_connection (config)
  30. ActiveRecord::Base.connection.current_database
  31. ActiveRecord::Base.connection.drop_database(config["database"])
  32. rescue
  33. # do nothing
  34. end
  35. when "sqlite3" then
  36. FileUtils.rm_f(File.join(RAILS_ROOT, config["database"]))
  37. when "postgresql" then
  38. `dropdb \"#{config["database"]}\"`
  39. else
  40. # do nothing
  41. end
  42. end
  43.  
  44. desc 'Drops, creates and then migrates the database for your current RAILS_ENV. Target specific version with VERSION=x'
  45. task 'db:reset' => [ 'db:drop', 'db:create', 'db:migrate' ] do
  46. # do nothing
  47. end
  48.  
  49. desc 'Launches the database shell using the values defined in config/database.yml'
  50. task 'db:shell' => [ 'environment' ] do
  51. config = ActiveRecord::Base.configurations [(RAILS_ENV or "development")]
  52. command = ""
  53. case config["adapter"]
  54. when "mysql" then
  55. (command << "mysql ")
  56. (command << "--host=#{(config["host"] or "localhost")} ")
  57. (command << "--port=#{(config["port"] or 3306)} ")
  58. (command << "--user=#{(config["username"] or "root")} ")
  59. (command << "--password=#{(config["password"] or "")} ")
  60. (command << config["database"])
  61. when "postgresql" then
  62. puts("You should consider switching to MySQL or get off your butt and submit a patch")
  63. else
  64. (command << "echo Unsupported database adapter: #{config["adapter"]}")
  65. end
  66. system(command)
  67. end
Add Comment
Please, Sign In to add comment