Guest User

Untitled

a guest
Feb 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. ## lib/tasks/git.thor
  3. #
  4. # Thor task to fully copy your master database into a new database for current branch.
  5. # Sample usage:
  6. #
  7. # thor git:db:clone
  8. #
  9. # What gets run:
  10. #
  11. # mysqladmin -u #{user} create #{target}
  12. # mysqldump -u #{user} #{from} | mysql -u #{user} #{target}
  13. #
  14. # Limitation: only works if password authentication isn't required.
  15. #
  16. class Git < Thor
  17. module Command
  18. protected
  19.  
  20. def command(com, capture = false)
  21. unless capture
  22. system(com)
  23. else
  24. @output = %x(#{com}).chomp
  25. end
  26. end
  27.  
  28. def last_command_was_success?
  29. $?.success?
  30. end
  31. end
  32.  
  33. class Db < Thor
  34. include Command
  35.  
  36. desc "clone", "Branch your development database"
  37. method_options :force => :boolean
  38. def clone
  39. require 'config/boot'
  40. require 'git_conf'
  41. config = GitConf.new
  42. dbconfig = config.database_configuration["development"]
  43.  
  44. if config.branched_database?
  45. user = dbconfig["username"]
  46. from = dbconfig["master-database"]
  47. to = dbconfig["database"]
  48.  
  49. # create the new database
  50. command "mysqladmin -u #{user} create #{to} 2>&1", true
  51.  
  52. unless last_command_was_success?
  53. if @output.index('database exists')
  54. warn %(Database "#{to}" already exists)
  55. return unless options[:force]
  56. else
  57. warn @output
  58. return
  59. end
  60. end
  61.  
  62. # clone development database
  63. command "mysqldump -u #{user} #{from} | mysql -u #{user} #{to}"
  64. puts %(Finished cloning "#{from}" to "#{to}".) if last_command_was_success?
  65. else
  66. warn "branch isn't configured for a separate database"
  67. end
  68. end
  69. end
  70. end
Add Comment
Please, Sign In to add comment