mschuerig-branch_db 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -39,7 +39,7 @@ databases specific to current branch
39
39
 
40
40
  To your Rakefile or a file in lib/tasks add
41
41
 
42
- require 'tasks/db_branches.rb'
42
+ require 'tasks/db_branches'
43
43
 
44
44
  Then you can use these rake tasks to manage your databases.
45
45
 
data/branch_db.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{branch_db}
5
- s.version = "0.0.3"
5
+ s.version = "0.0.5"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Michael Schuerig"]
@@ -23,6 +23,7 @@ module BranchDb # :nodoc:
23
23
  end
24
24
 
25
25
  def create_database(branch)
26
+ ### TODO when copying a database, determine charset and collation from original
26
27
  config = branch_config(branch)
27
28
  charset = ENV['CHARSET'] || 'utf8'
28
29
  collation = ENV['COLLATION'] || 'utf8_general_ci'
@@ -42,7 +43,7 @@ module BranchDb # :nodoc:
42
43
  def existing_databases
43
44
  @existing_databases ||=
44
45
  begin
45
- raw_dbs = `mysql -e 'SHOW DATABASES'`
46
+ raw_dbs = `mysql #{command_options(@config)} -e 'SHOW DATABASES'`
46
47
  if $? == 0
47
48
  existing_dbs = raw_dbs.split("\n").drop(1)
48
49
  existing_dbs -= %w( information_schema )
@@ -53,11 +54,19 @@ module BranchDb # :nodoc:
53
54
  end
54
55
 
55
56
  def dump_command(config, dump_file)
56
- %{mysqldump --user "#{config["username"]}" --host "#{config["host"]}" #{config["database"]} > #{dump_file}}
57
+ %{mysqldump #{command_options(config)} #{config["database"]} > #{dump_file}}
57
58
  end
58
59
 
59
60
  def load_command(config, dump_file)
60
- %{mysql --user "#{config["username"]}" --host "#{config["host"]}" #{config["database"]} < #{dump_file}}
61
+ %{mysql #{command_options(config)} #{config["database"]} < #{dump_file}}
62
+ end
63
+
64
+ def command_options(config)
65
+ returning opts = '' do
66
+ %w( user username password password host host).each_slice(2) do |o, k|
67
+ opts << " --#{o} #{config[k]}" if config[k]
68
+ end
69
+ end
61
70
  end
62
71
  end
63
72
  end
@@ -53,11 +53,19 @@ module BranchDb # :nodoc:
53
53
  end
54
54
 
55
55
  def dump_command(config, dump_file)
56
- %{pg_dump --clean -U "#{config['username']}" --host="#{config['host']}" --port=#{config['port']} #{config['database']} > #{dump_file}}
56
+ %{pg_dump #{command_options(config)} --clean #{config['database']} > #{dump_file}}
57
57
  end
58
58
 
59
59
  def load_command(config, dump_file)
60
- %{psql -U "#{config['username']}" -f "#{dump_file}" --host="#{config['host']}" --port=#{config['port']} #{config['database']}}
60
+ %{psql #{command_options(config)} -f "#{dump_file}" #{config['database']}}
61
+ end
62
+
63
+ def command_options(config)
64
+ returning opts = '' do
65
+ %w( username host port ).each do |o|
66
+ opts << " --#{o}=#{config[o]}" if config[o]
67
+ end
68
+ end
61
69
  end
62
70
  end
63
71
  end
@@ -25,7 +25,9 @@ module BranchDb # :nodoc:
25
25
  config = branch_config(branch)
26
26
  old_umask = File.umask(0077) # make created files readable only to the user
27
27
  dump_file = Tempfile.new('branchdb')
28
- %x{#{dump_command(config, dump_file.path)}}
28
+ cmd = dump_command(config, dump_file.path)
29
+ puts cmd if verbose?
30
+ %x{#{cmd}}
29
31
  raise Error, "Unable to dump database #{config['database']}." unless $? == 0
30
32
  dump_file.path
31
33
  ensure
@@ -34,8 +36,10 @@ module BranchDb # :nodoc:
34
36
 
35
37
  def load_branch_db(branch, dump_file)
36
38
  config = branch_config(branch)
39
+ cmd = load_command(config, dump_file)
40
+ puts cmd if verbose?
37
41
  silence_stderr do
38
- %x{{load_command(config, dump_file)}}
42
+ %x{#{cmd}}
39
43
  end
40
44
  raise Error, "Unable to load database #{config['database']}." unless $? == 0
41
45
  end
@@ -26,6 +26,7 @@ module BranchDb # :nodoc:
26
26
  def initialize(rails_env, config, branch, options = {})
27
27
  @rails_env, @config, @branch = rails_env, config, branch
28
28
  @overwrite = options[:overwrite]
29
+ @verbose = options[:verbose]
29
30
  end
30
31
 
31
32
  def current
@@ -87,6 +88,10 @@ module BranchDb # :nodoc:
87
88
  end
88
89
  end
89
90
 
91
+ def verbose?
92
+ @verbose
93
+ end
94
+
90
95
  def branch_config(branch)
91
96
  @config.merge('database' => branch_db(branch))
92
97
  end
@@ -9,9 +9,11 @@ module BranchDb # :nodoc:
9
9
  end
10
10
 
11
11
  def environment_options
12
- {
13
- :overwrite => (ENV['OVERWRITE'] =~ /^(true|1)$/i) == 0
14
- }
12
+ returning options = {} do
13
+ [:overwrite, :verbose].each do |opt|
14
+ options[opt] = (ENV[opt.to_s.upcase] =~ /\A(true|1)\Z/i) == 0
15
+ end
16
+ end
15
17
  end
16
18
 
17
19
  def target_branch
data/lib/branch_db.rb CHANGED
@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module BranchDb
5
- VERSION = '0.0.3'
5
+ VERSION = '0.0.5'
6
6
  DEFAULT_BRANCH = 'master'
7
7
 
8
8
  class Error < StandardError; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mschuerig-branch_db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Schuerig