mschuerig-branch_db 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +1 -1
- data/branch_db.gemspec +1 -1
- data/lib/branch_db/mysql_switcher.rb +12 -3
- data/lib/branch_db/postgresql_switcher.rb +10 -2
- data/lib/branch_db/real_db_switchers_common.rb +6 -2
- data/lib/branch_db/switcher.rb +5 -0
- data/lib/branch_db/task_helper.rb +5 -3
- data/lib/branch_db.rb +1 -1
- metadata +1 -1
data/README.rdoc
CHANGED
data/branch_db.gemspec
CHANGED
@@ -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
|
57
|
+
%{mysqldump #{command_options(config)} #{config["database"]} > #{dump_file}}
|
57
58
|
end
|
58
59
|
|
59
60
|
def load_command(config, dump_file)
|
60
|
-
%{mysql
|
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
|
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
|
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
|
-
|
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{{
|
42
|
+
%x{#{cmd}}
|
39
43
|
end
|
40
44
|
raise Error, "Unable to load database #{config['database']}." unless $? == 0
|
41
45
|
end
|
data/lib/branch_db/switcher.rb
CHANGED
@@ -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
|
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