Guest User

Untitled

a guest
Feb 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. ## lib/git_conf.rb
  2. if 'development' == RAILS_ENV
  3. require 'rubygems'
  4. require 'grit' # use "mojombo-grit" from github
  5.  
  6. ## uncomment if your application is running in an environment
  7. ## where `git` command cannot be found in the PATH (i.e. Passenger)
  8. # Grit::Git.git_binary = '/usr/local/git/bin/git'
  9. end
  10.  
  11. def Rails.repo
  12. @@repository ||= Grit::Repo.new(Rails.root)
  13. end
  14.  
  15. # Adjust your environment.rb to use this subclass:
  16. # Rails::Initializer.run(:process, GitConf.new) do |config|
  17. # ...
  18. # end
  19. class GitConf < Rails::Configuration
  20. def initialize
  21. super
  22. @branched_database = false
  23. end
  24.  
  25. def branched_database?() @branched_database end
  26.  
  27. # agument the original method in order to append
  28. # the branch name suffix in certain conditions
  29. def database_configuration
  30. @database_configuration ||= begin
  31. config = super
  32. if Rails.env == "development"
  33. head = Rails.repo.head
  34. branch = head && head.name
  35. # check if this branch has a special database
  36. if branch and branch != "master" and branch !~ /\W/ and branch_has_database?(branch)
  37. development = config["development"]
  38. # save original configuration
  39. development["master-database"] = development["database"]
  40. # append branch name suffix
  41. base_name, extension = development["database"].split(".", 2)
  42. development["database"] = "#{base_name}_#{branch}"
  43. development["database"] += ".#{extension}" if extension
  44. # finally, indicate that this database has branched
  45. @branched_database = true
  46. end
  47. end
  48. config
  49. end
  50. end
  51.  
  52. protected
  53.  
  54. def branch_has_database?(branch)
  55. Rails.repo.config["branch.#{branch}.database"] == "true"
  56. end
  57. end
Add Comment
Please, Sign In to add comment