mschuerig-branch_db 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/Manifest.txt +2 -0
- data/README.rdoc +0 -2
- data/branch_db.gemspec +2 -2
- data/lib/branch_db.rb +2 -1
- data/lib/branch_db/mysql_switcher.rb +63 -0
- data/lib/branch_db/postgresql_switcher.rb +8 -36
- data/lib/branch_db/real_db_switchers_common.rb +43 -0
- data/test/mocks.rb +11 -3
- data/test/test_configuration_twiddler.rb +0 -3
- metadata +3 -1
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
@@ -6,7 +6,9 @@ Rakefile
|
|
6
6
|
branch_db.gemspec
|
7
7
|
lib/branch_db.rb
|
8
8
|
lib/branch_db/configuration_twiddler.rb
|
9
|
+
lib/branch_db/mysql_switcher.rb
|
9
10
|
lib/branch_db/postgresql_switcher.rb
|
11
|
+
lib/branch_db/real_db_switchers_common.rb
|
10
12
|
lib/branch_db/sqlite_switcher.rb
|
11
13
|
lib/branch_db/switcher.rb
|
12
14
|
lib/branch_db/task_helper.rb
|
data/README.rdoc
CHANGED
@@ -9,8 +9,6 @@ Give each git branch its own databases for ActiveRecord.
|
|
9
9
|
== FEATURES/PROBLEMS:
|
10
10
|
|
11
11
|
* alpha quality
|
12
|
-
* only PostgreSQL and SQLite(3) are supported yet
|
13
|
-
(adding MySQL is trivial, I'll do it when I need it)
|
14
12
|
* create, delete, copy git branch-specific databases using rake tasks
|
15
13
|
* automatic switching based on currently checked out branch
|
16
14
|
|
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.
|
5
|
+
s.version = "0.0.3"
|
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"]
|
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.description = %q{Give each git branch its own databases for ActiveRecord.}
|
11
11
|
s.email = ["michael@schuerig.de"]
|
12
12
|
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
|
13
|
-
s.files = [".gitignore", "History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "branch_db.gemspec", "lib/branch_db.rb", "lib/branch_db/configuration_twiddler.rb", "lib/branch_db/postgresql_switcher.rb", "lib/branch_db/sqlite_switcher.rb", "lib/branch_db/switcher.rb", "lib/branch_db/task_helper.rb", "lib/tasks/db_branches.rb", "test/mocks.rb", "test/test_configuration_twiddler.rb", "test/test_helper.rb", "test/test_postgresql_switcher.rb", "test/test_sqlite_switcher.rb", "test/test_switcher.rb"]
|
13
|
+
s.files = [".gitignore", "History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "branch_db.gemspec", "lib/branch_db.rb", "lib/branch_db/configuration_twiddler.rb", "lib/branch_db/mysql_switcher.rb", "lib/branch_db/postgresql_switcher.rb", "lib/branch_db/real_db_switchers_common.rb", "lib/branch_db/sqlite_switcher.rb", "lib/branch_db/switcher.rb", "lib/branch_db/task_helper.rb", "lib/tasks/db_branches.rb", "test/mocks.rb", "test/test_configuration_twiddler.rb", "test/test_helper.rb", "test/test_postgresql_switcher.rb", "test/test_sqlite_switcher.rb", "test/test_switcher.rb"]
|
14
14
|
s.has_rdoc = false
|
15
15
|
s.homepage = %q{http://github.com/mschuerig/branch_db}
|
16
16
|
s.rdoc_options = ["--main", "README.rdoc"]
|
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.
|
5
|
+
VERSION = '0.0.3'
|
6
6
|
DEFAULT_BRANCH = 'master'
|
7
7
|
|
8
8
|
class Error < StandardError; end
|
@@ -25,5 +25,6 @@ module BranchDb
|
|
25
25
|
end
|
26
26
|
|
27
27
|
require 'branch_db/switcher'
|
28
|
+
require 'branch_db/mysql_switcher'
|
28
29
|
require 'branch_db/postgresql_switcher'
|
29
30
|
require 'branch_db/sqlite_switcher'
|
@@ -0,0 +1,63 @@
|
|
1
|
+
|
2
|
+
require 'branch_db/switcher'
|
3
|
+
require 'branch_db/real_db_switchers_common'
|
4
|
+
|
5
|
+
module BranchDb # :nodoc:
|
6
|
+
|
7
|
+
class MysqlSwitcher < Switcher
|
8
|
+
include RealDbSwitchersCommon
|
9
|
+
|
10
|
+
def self.can_handle?(config)
|
11
|
+
config['adapter'] == 'mysql'
|
12
|
+
end
|
13
|
+
|
14
|
+
def current
|
15
|
+
current_branch = branch_db_exists?(@branch) ? @branch : 'master'
|
16
|
+
puts "#{@rails_env}: #{branch_db(current_branch)} (MySQL)"
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def self.show_branches(rails_env, config)
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_database(branch)
|
26
|
+
config = branch_config(branch)
|
27
|
+
charset = ENV['CHARSET'] || 'utf8'
|
28
|
+
collation = ENV['COLLATION'] || 'utf8_general_ci'
|
29
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => nil))
|
30
|
+
ActiveRecord::Base.connection.create_database(config['database'], :charset => (config['charset'] || charset), :collation => (config['collation'] || collation))
|
31
|
+
ActiveRecord::Base.establish_connection(config)
|
32
|
+
end
|
33
|
+
|
34
|
+
def drop_database(branch)
|
35
|
+
config = branch_config(branch)
|
36
|
+
ActiveRecord::Base.establish_connection(config)
|
37
|
+
ActiveRecord::Base.connection.drop_database config['database']
|
38
|
+
@existing_databases = nil
|
39
|
+
nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def existing_databases
|
43
|
+
@existing_databases ||=
|
44
|
+
begin
|
45
|
+
raw_dbs = `mysql -e 'SHOW DATABASES'`
|
46
|
+
if $? == 0
|
47
|
+
existing_dbs = raw_dbs.split("\n").drop(1)
|
48
|
+
existing_dbs -= %w( information_schema )
|
49
|
+
else
|
50
|
+
raise Error, "Cannot determine existing databases."
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def dump_command(config, dump_file)
|
56
|
+
%{mysqldump --user "#{config["username"]}" --host "#{config["host"]}" #{config["database"]} > #{dump_file}}
|
57
|
+
end
|
58
|
+
|
59
|
+
def load_command(config, dump_file)
|
60
|
+
%{mysql --user "#{config["username"]}" --host "#{config["host"]}" #{config["database"]} < #{dump_file}}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -1,9 +1,12 @@
|
|
1
1
|
|
2
2
|
require 'branch_db/switcher'
|
3
|
+
require 'branch_db/real_db_switchers_common'
|
3
4
|
|
4
5
|
module BranchDb # :nodoc:
|
5
6
|
|
6
7
|
class PostgresqlSwitcher < Switcher
|
8
|
+
include RealDbSwitchersCommon
|
9
|
+
|
7
10
|
def self.can_handle?(config)
|
8
11
|
config['adapter'] == 'postgresql'
|
9
12
|
end
|
@@ -19,21 +22,9 @@ module BranchDb # :nodoc:
|
|
19
22
|
super
|
20
23
|
end
|
21
24
|
|
22
|
-
def branch_db(branch)
|
23
|
-
if branch == 'master'
|
24
|
-
@config['database']
|
25
|
-
else
|
26
|
-
@config['database'].sub(/(_.+?)??(_?(#{@rails_env}))?$/, "_#{branch}\\2")
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def branch_db_exists?(branch)
|
31
|
-
existing_databases.include?(branch_db(branch))
|
32
|
-
end
|
33
|
-
|
34
25
|
def create_database(branch)
|
35
26
|
config = branch_config(branch).merge(
|
36
|
-
'encoding' => @config[
|
27
|
+
'encoding' => @config['encoding'] || ENV['CHARSET'] || 'utf8')
|
37
28
|
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
38
29
|
ActiveRecord::Base.connection.create_database(config['database'], config)
|
39
30
|
ActiveRecord::Base.establish_connection(config)
|
@@ -48,13 +39,6 @@ module BranchDb # :nodoc:
|
|
48
39
|
nil
|
49
40
|
end
|
50
41
|
|
51
|
-
def copy_database(from_branch, to_branch)
|
52
|
-
dump_file = dump_branch_db(from_branch)
|
53
|
-
load_branch_db(to_branch, dump_file)
|
54
|
-
end
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
42
|
def existing_databases
|
59
43
|
@existing_databases ||=
|
60
44
|
begin
|
@@ -68,24 +52,12 @@ module BranchDb # :nodoc:
|
|
68
52
|
end
|
69
53
|
end
|
70
54
|
|
71
|
-
def
|
72
|
-
|
73
|
-
config = branch_config(branch)
|
74
|
-
old_umask = File.umask(0077) # make created files readable only to the user
|
75
|
-
dump_file = Tempfile.new('branchdb')
|
76
|
-
`pg_dump --clean -U "#{config['username']}" --host="#{config['host']}" --port=#{config['port']} #{config['database']} > #{dump_file.path}`
|
77
|
-
raise Error, "Unable to dump database #{config['database']}." unless $? == 0
|
78
|
-
dump_file.path
|
79
|
-
ensure
|
80
|
-
File.umask(old_umask)
|
55
|
+
def dump_command(config, dump_file)
|
56
|
+
%{pg_dump --clean -U "#{config['username']}" --host="#{config['host']}" --port=#{config['port']} #{config['database']} > #{dump_file}}
|
81
57
|
end
|
82
58
|
|
83
|
-
def
|
84
|
-
config =
|
85
|
-
silence_stderr do
|
86
|
-
`psql -U "#{config['username']}" -f "#{dump_file}" --host="#{config['host']}" --port=#{config['port']} #{config['database']}`
|
87
|
-
end
|
88
|
-
raise Error, "Unable to load database #{config['database']}." unless $? == 0
|
59
|
+
def load_command(config, dump_file)
|
60
|
+
%{psql -U "#{config['username']}" -f "#{dump_file}" --host="#{config['host']}" --port=#{config['port']} #{config['database']}}
|
89
61
|
end
|
90
62
|
end
|
91
63
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module BranchDb # :nodoc:
|
2
|
+
|
3
|
+
module RealDbSwitchersCommon
|
4
|
+
protected
|
5
|
+
|
6
|
+
def branch_db(branch)
|
7
|
+
if branch == 'master'
|
8
|
+
@config['database']
|
9
|
+
else
|
10
|
+
@config['database'].sub(/(_.+?)??(_?(#{@rails_env}))?$/, "_#{branch}\\2")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def branch_db_exists?(branch)
|
15
|
+
existing_databases.include?(branch_db(branch))
|
16
|
+
end
|
17
|
+
|
18
|
+
def copy_database(from_branch, to_branch)
|
19
|
+
dump_file = dump_branch_db(from_branch)
|
20
|
+
load_branch_db(to_branch, dump_file)
|
21
|
+
end
|
22
|
+
|
23
|
+
def dump_branch_db(branch)
|
24
|
+
require 'tempfile'
|
25
|
+
config = branch_config(branch)
|
26
|
+
old_umask = File.umask(0077) # make created files readable only to the user
|
27
|
+
dump_file = Tempfile.new('branchdb')
|
28
|
+
%x{#{dump_command(config, dump_file.path)}}
|
29
|
+
raise Error, "Unable to dump database #{config['database']}." unless $? == 0
|
30
|
+
dump_file.path
|
31
|
+
ensure
|
32
|
+
File.umask(old_umask)
|
33
|
+
end
|
34
|
+
|
35
|
+
def load_branch_db(branch, dump_file)
|
36
|
+
config = branch_config(branch)
|
37
|
+
silence_stderr do
|
38
|
+
%x{{load_command(config, dump_file)}}
|
39
|
+
end
|
40
|
+
raise Error, "Unable to load database #{config['database']}." unless $? == 0
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/test/mocks.rb
CHANGED
@@ -30,7 +30,7 @@ module BranchDb
|
|
30
30
|
@branch
|
31
31
|
end
|
32
32
|
|
33
|
-
class
|
33
|
+
class MysqlSwitcher < Switcher
|
34
34
|
include Recorder
|
35
35
|
record :create_database, :drop_database, :load_branch_db
|
36
36
|
record(:dump_branch_db) { 'the-dump-file' }
|
@@ -38,8 +38,16 @@ module BranchDb
|
|
38
38
|
def existing_databases
|
39
39
|
%w( testit_development testit_feature_development testit_test )
|
40
40
|
end
|
41
|
-
|
42
|
-
|
41
|
+
end
|
42
|
+
|
43
|
+
### FIXME this repetition is dumb
|
44
|
+
class PostgresqlSwitcher < Switcher
|
45
|
+
include Recorder
|
46
|
+
record :create_database, :drop_database, :load_branch_db
|
47
|
+
record(:dump_branch_db) { 'the-dump-file' }
|
48
|
+
|
49
|
+
def existing_databases
|
50
|
+
%w( testit_development testit_feature_development testit_test )
|
43
51
|
end
|
44
52
|
end
|
45
53
|
|
@@ -57,8 +57,6 @@ class TestConfigurationTwiddlerPostgreSQL < Test::Unit::TestCase
|
|
57
57
|
include RealDatabaseTests
|
58
58
|
end
|
59
59
|
|
60
|
-
=begin
|
61
|
-
### TODO
|
62
60
|
class TestConfigurationTwiddlerMysql < Test::Unit::TestCase
|
63
61
|
def setup
|
64
62
|
@mock = MockConfiguration.new({
|
@@ -76,7 +74,6 @@ class TestConfigurationTwiddlerMysql < Test::Unit::TestCase
|
|
76
74
|
|
77
75
|
include RealDatabaseTests
|
78
76
|
end
|
79
|
-
=end
|
80
77
|
|
81
78
|
class TestConfigurationTwiddlerSQLite < Test::Unit::TestCase
|
82
79
|
def setup
|
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.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Schuerig
|
@@ -62,7 +62,9 @@ files:
|
|
62
62
|
- branch_db.gemspec
|
63
63
|
- lib/branch_db.rb
|
64
64
|
- lib/branch_db/configuration_twiddler.rb
|
65
|
+
- lib/branch_db/mysql_switcher.rb
|
65
66
|
- lib/branch_db/postgresql_switcher.rb
|
67
|
+
- lib/branch_db/real_db_switchers_common.rb
|
66
68
|
- lib/branch_db/sqlite_switcher.rb
|
67
69
|
- lib/branch_db/switcher.rb
|
68
70
|
- lib/branch_db/task_helper.rb
|