Class: Branchbot::BranchSwitcher
- Inherits:
-
Object
- Object
- Branchbot::BranchSwitcher
- Defined in:
- lib/branchbot/branch_switcher.rb
Instance Method Summary collapse
-
#initialize(app_root: '.', erb_in_database_yml: true) ⇒ BranchSwitcher
constructor
A new instance of BranchSwitcher.
- #switch_from(prev_ref) ⇒ Object
Constructor Details
#initialize(app_root: '.', erb_in_database_yml: true) ⇒ BranchSwitcher
Returns a new instance of BranchSwitcher.
3 4 5 6 7 8 |
# File 'lib/branchbot/branch_switcher.rb', line 3 def initialize(app_root: '.', erb_in_database_yml: true) @app_root = app_root @project_root = %x[git rev-parse --show-toplevel].strip @dump_folder = "#{@project_root}/.db_branch_dumps" @erb_in_database_yml = erb_in_database_yml end |
Instance Method Details
#switch_from(prev_ref) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/branchbot/branch_switcher.rb', line 10 def switch_from(prev_ref) # Get the current (destination) branch @destination_branch = `git rev-parse --abbrev-ref HEAD`.strip # Since we're just given a commit ID referencing the branch head we're coming from, # it could be at the head of multiple branches. We can assume the source isn't the same as the # destination branch, so we can remove that immediately. @source_branches = branches_from_refhead(prev_ref).reject{ |b| b == @destination_branch } # Load Rails DB config and grab database name @rails_db_config = YAML.load(db_config_yml) dev_database_name = @rails_db_config['development']['database'] begin @adapter = Adapters::Abstract.build(@rails_db_config['development'], @dump_folder) rescue Adapters::UnsupportedDatabase => e puts "\nERROR: #{e.}" exit end # Ensure dump directory exists unless Dir.exists?(@dump_folder) Dir.mkdir @dump_folder end # Don't do anything if the source and destination branches are the same or nonexistent unless @source_branches.include?(@destination_branch) || @source_branches.empty? || (@source_branches | [@destination_branch]).any?{ |b| b == '' } # Dump database for source branches if @source_branches.all? { |branch| @adapter.dump(branch) } # Restore dump from this branch, if it exists if @adapter.dump_exists?(@destination_branch) if @adapter.restore(@destination_branch) prepare_test_database end else print "No DB dump for #{dev_database_name} on the '#{@destination_branch}' branch was found!\n" print "The state of your database has been saved for when you return to the '#{@source_branches.join('\' or \'')}' branch, but its current state has been left unchanged. You are now free to make changes to it that are specific to this branch, and they will be saved when you checkout a different branch, then restored when you checkout this one again.\n" end else print "Failed to dump database. Halting.\n" end end end |