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)
@destination_branch = `git rev-parse --abbrev-ref HEAD`.strip
@source_branches = branches_from_refhead(prev_ref).reject{ |b| b == @destination_branch }
@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.message}"
exit
end
unless Dir.exists?(@dump_folder)
Dir.mkdir @dump_folder
end
unless @source_branches.include?(@destination_branch) || @source_branches.empty? || (@source_branches | [@destination_branch]).any?{ |b| b == '' }
if @source_branches.all? { |branch| @adapter.dump(branch) }
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
|