mschuerig-branch_db 0.0.1 → 0.0.2

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/Manifest.txt CHANGED
@@ -3,6 +3,7 @@ History.txt
3
3
  Manifest.txt
4
4
  README.rdoc
5
5
  Rakefile
6
+ branch_db.gemspec
6
7
  lib/branch_db.rb
7
8
  lib/branch_db/configuration_twiddler.rb
8
9
  lib/branch_db/postgresql_switcher.rb
@@ -11,7 +12,6 @@ lib/branch_db/switcher.rb
11
12
  lib/branch_db/task_helper.rb
12
13
  lib/tasks/db_branches.rb
13
14
  test/mocks.rb
14
- test/test_branch_db.rb
15
15
  test/test_configuration_twiddler.rb
16
16
  test/test_helper.rb
17
17
  test/test_postgresql_switcher.rb
data/README.rdoc CHANGED
@@ -20,10 +20,24 @@ Put the following block of code into config/environment.rb *before*
20
20
  Rails::Initializer.run. This code ensures that the database is
21
21
  automatically switched, based on the checked out git branch.
22
22
 
23
+ # config/environment.rb
24
+ ...
23
25
  require 'branch_db/configuration_twiddler'
24
26
  Rails::Configuration.class_eval do
25
27
  include ::BranchDb::ConfigurationTwiddler
26
28
  end
29
+ ...
30
+
31
+ In config/database.yml, mark the configuration that may have
32
+ databases specific to current branch
33
+
34
+ # config/database.yml
35
+ development: &development
36
+ adapter: postgresql
37
+ database: myproject_development
38
+ host: localhost
39
+ port: 5432
40
+ per_branch: true
27
41
 
28
42
  To your Rakefile or a file in lib/tasks add
29
43
 
@@ -47,6 +61,16 @@ Then you can use these rake tasks to manage your databases.
47
61
  rake db:branches:delete
48
62
  Delete databases for a branch given by BRANCH
49
63
 
64
+ Say, you have a configuration as above and you're currently on
65
+ git branch "feature" when you decide that you need a database
66
+ specific to this branch. Then
67
+
68
+ ~/projects/myproject (feature*)$ rake db:branches:copy ORIG_BRANCH=master
69
+
70
+ will give you a copy of the database from branch "master".
71
+ If your branch already has a database that you want to overwrite,
72
+ add OVERWRITE=true to the command line.
73
+
50
74
  == REQUIREMENTS:
51
75
 
52
76
  * ActiveRecord
data/branch_db.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{branch_db}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Michael Schuerig"]
9
+ s.date = %q{2009-04-09}
10
+ s.description = %q{Give each git branch its own databases for ActiveRecord.}
11
+ s.email = ["michael@schuerig.de"]
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"]
14
+ s.has_rdoc = false
15
+ s.homepage = %q{http://github.com/mschuerig/branch_db}
16
+ s.rdoc_options = ["--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{branch_db}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Give each git branch its own databases for ActiveRecord.}
21
+ s.test_files = ["test/test_sqlite_switcher.rb", "test/test_helper.rb", "test/test_configuration_twiddler.rb", "test/test_postgresql_switcher.rb", "test/test_switcher.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_runtime_dependency(%q<activerecord>, [">= 2.0.2"])
29
+ s.add_development_dependency(%q<newgem>, [">= 1.3.0"])
30
+ s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
31
+ else
32
+ s.add_dependency(%q<activerecord>, [">= 2.0.2"])
33
+ s.add_dependency(%q<newgem>, [">= 1.3.0"])
34
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<activerecord>, [">= 2.0.2"])
38
+ s.add_dependency(%q<newgem>, [">= 1.3.0"])
39
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
40
+ end
41
+ end
@@ -28,7 +28,7 @@ module BranchDb # :nodoc:
28
28
  end
29
29
 
30
30
  def branch_db_exists?(branch)
31
- File.exist?(File.join(RAILS_ROOT, branch_db(branch)))
31
+ File.exists?(branch_db_path(branch))
32
32
  end
33
33
 
34
34
  def create_database(branch)
@@ -38,11 +38,18 @@ module BranchDb # :nodoc:
38
38
  end
39
39
 
40
40
  def drop_database(branch)
41
- FileUtils.rm_f(branch_db(branch))
41
+ FileUtils.rm_f(branch_db_path(branch))
42
42
  end
43
43
 
44
44
  def copy_database(from_branch, to_branch)
45
- FileUtils.cp(branch_db(from_branch), branch_db(to_branch))
45
+ FileUtils.cp(branch_db_path(from_branch), branch_db_path(to_branch))
46
46
  end
47
+
48
+ private
49
+
50
+ def branch_db_path(branch)
51
+ File.join(RAILS_ROOT, branch_db(branch))
52
+ end
53
+
47
54
  end
48
55
  end
@@ -5,7 +5,7 @@ module BranchDb # :nodoc:
5
5
  def self.which(config)
6
6
  switcher = switchers.detect { |sw| sw.can_handle?(config) }
7
7
  unless switcher
8
- $stderr.puts 'Your database adapter is not supported yet.'
8
+ $stderr.puts "Your database adapter (#{config['adapter']}) is not supported yet."
9
9
  switcher = self # double as null switcher
10
10
  end
11
11
  switcher
@@ -10,7 +10,7 @@ module BranchDb # :nodoc:
10
10
 
11
11
  def environment_options
12
12
  {
13
- :overwrite => ENV['OVERWRITE']
13
+ :overwrite => (ENV['OVERWRITE'] =~ /^(true|1)$/i) == 0
14
14
  }
15
15
  end
16
16
 
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.1'
5
+ VERSION = '0.0.2'
6
6
  DEFAULT_BRANCH = 'master'
7
7
 
8
8
  class Error < StandardError; end
@@ -27,11 +27,7 @@ namespace :db do
27
27
 
28
28
  desc "Copy databases from one branch to another. Default is from ORIG_BRANCH=master to BRANCH=<current branch>"
29
29
  task :copy => :setup do
30
- if target_branch == current_branch
31
- $stderr.puts "Cannot copy database to itself."
32
- else
33
- each_local_database { |switcher| switcher.copy_from(originating_branch) }
34
- end
30
+ each_local_database { |switcher| switcher.copy_from(originating_branch) }
35
31
  end
36
32
 
37
33
  desc "Delete databases for a branch given by BRANCH"
data/test/mocks.rb CHANGED
@@ -1,17 +1,26 @@
1
1
 
2
- require 'branch_db'
3
- require 'branch_db/configuration_twiddler'
4
-
5
- class MockConfiguration
6
- def initialize(config)
7
- @config = config
2
+ module Recorder
3
+ include Test::Unit::Assertions
4
+ def self.included(base)
5
+ base.extend(SingletonMethods)
8
6
  end
9
-
10
- def database_configuration
11
- @config
7
+ def recorded_calls
8
+ @recorded_calls ||= []
12
9
  end
13
- include ::BranchDb::ConfigurationTwiddler
14
- end
10
+ module SingletonMethods
11
+ def record(*methods, &block)
12
+ methods.each do |m|
13
+ define_method(m) do |*args|
14
+ recorded_calls << [m.to_sym] + args
15
+ block.call if block
16
+ end
17
+ end
18
+ end
19
+ end
20
+ def verify(*expected)
21
+ assert_equal(expected, recorded_calls)
22
+ end
23
+ end
15
24
 
16
25
  module BranchDb
17
26
  def self.set_branch(branch)
@@ -22,14 +31,10 @@ module BranchDb
22
31
  end
23
32
 
24
33
  class PostgresqlSwitcher < Switcher
25
- def dump_branch_db(*args)
26
- @ops ||= []
27
- @ops << [:dump_branch_db] + args
28
- end
29
- def load_branch_db(*args)
30
- @ops ||= []
31
- @ops << [:load_branch_db] + args
32
- end
34
+ include Recorder
35
+ record :create_database, :drop_database, :load_branch_db
36
+ record(:dump_branch_db) { 'the-dump-file' }
37
+
33
38
  def existing_databases
34
39
  %w( testit_development testit_feature_development testit_test )
35
40
  end
@@ -37,8 +42,17 @@ module BranchDb
37
42
  @ops
38
43
  end
39
44
  end
45
+
46
+ class SqliteSwitcher < Switcher
47
+ include Recorder
48
+ record :create_database
49
+ end
40
50
  end
41
51
 
42
- def create_mock_sqlite_db(name)
43
- File.open(File.join(RAILS_ROOT, name), 'w') { |f| f.puts 'Mock' }
52
+ def create_mock_sqlite_db(*names)
53
+ db_dir = File.join(RAILS_ROOT, 'db')
54
+ FileUtils.mkdir_p(db_dir) unless File.directory?(db_dir)
55
+ names.each do |name|
56
+ File.open(File.join(RAILS_ROOT, name), 'w') { |f| f.puts 'Mock' }
57
+ end
44
58
  end
@@ -1,8 +1,20 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
1
+
2
+ require 'test_helper'
2
3
 
3
4
  require 'branch_db/configuration_twiddler'
4
5
  require 'mocks'
5
6
 
7
+ class MockConfiguration
8
+ def initialize(config)
9
+ @config = config
10
+ end
11
+
12
+ def database_configuration
13
+ @config
14
+ end
15
+ include ::BranchDb::ConfigurationTwiddler
16
+ end
17
+
6
18
  module RealDatabaseTests
7
19
  def test_master_branch
8
20
  BranchDb.set_branch('master')
@@ -68,6 +80,11 @@ end
68
80
 
69
81
  class TestConfigurationTwiddlerSQLite < Test::Unit::TestCase
70
82
  def setup
83
+ create_mock_sqlite_db(
84
+ 'db/development.sqlite3',
85
+ 'db/development_feature.sqlite3',
86
+ 'db/test.sqlite3'
87
+ )
71
88
  @mock = MockConfiguration.new({
72
89
  'development' => {
73
90
  'database' => 'db/development.sqlite3',
@@ -79,11 +96,6 @@ class TestConfigurationTwiddlerSQLite < Test::Unit::TestCase
79
96
  'adapter' => 'sqlite3'
80
97
  }
81
98
  })
82
-
83
- @tmpdir = FileUtils.mkdir_p(File.join(RAILS_ROOT, 'db'))
84
- create_mock_sqlite_db('db/development.sqlite3')
85
- create_mock_sqlite_db('db/development_feature.sqlite3')
86
- create_mock_sqlite_db('db/test.sqlite3')
87
99
  end
88
100
 
89
101
  def teardown
data/test/test_helper.rb CHANGED
@@ -1,8 +1,42 @@
1
+
1
2
  require 'stringio'
3
+ require 'fileutils'
2
4
  require 'test/unit'
3
- require File.dirname(__FILE__) + '/../lib/branch_db'
4
5
 
5
- require 'rubygems'
6
- require 'ruby-debug'
6
+ require 'branch_db'
7
+
8
+ #require 'rubygems'
9
+ #require 'ruby-debug'
7
10
 
8
11
  RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), 'tmp'))
12
+
13
+ def assert_stdout(expected)
14
+ oldout = $stdout
15
+ $stdout = StringIO.new
16
+ yield
17
+ $stdout.rewind
18
+ case expected
19
+ when String
20
+ assert_equal(expected, $stdout.read)
21
+ when Regexp
22
+ assert_match(expected, $stdout.read)
23
+ else
24
+ raise ArgumentError, "Don't know how to check stdout against #{expected.inspect}."
25
+ end
26
+ ensure
27
+ $stdout = oldout
28
+ end
29
+
30
+ def assert_sqlite_db_exist(name)
31
+ assert File.exists?(File.join(RAILS_ROOT, name)), "SQLite database #{name} does not exist."
32
+ end
33
+
34
+ # from ActiveSupport
35
+ def silence_stream(stream)
36
+ old_stream = stream.dup
37
+ stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
38
+ stream.sync = true
39
+ yield
40
+ ensure
41
+ stream.reopen(old_stream)
42
+ end
@@ -1,5 +1,5 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
2
1
 
2
+ require 'test_helper'
3
3
  require 'mocks'
4
4
 
5
5
  class TestPostgresqlSwitcher < Test::Unit::TestCase
@@ -7,13 +7,69 @@ class TestPostgresqlSwitcher < Test::Unit::TestCase
7
7
  @config = {
8
8
  'development' => {
9
9
  'database' => 'testit_development',
10
+ 'adapter' => 'postgresql',
11
+ 'per_branch' => true
12
+ },
13
+ 'test' => {
14
+ 'database' => 'testit_test',
10
15
  'adapter' => 'postgresql'
11
16
  }
12
17
  }
13
- @switcher = BranchDb::PostgresqlSwitcher.new('development', @config, 'feature')
18
+ end
19
+
20
+ def make_switcher(options = {})
21
+ env = options.delete(:env) || 'development'
22
+ branch = options.delete(:branch) || 'feature'
23
+ BranchDb::PostgresqlSwitcher.new(env, @config[env], branch, options)
14
24
  end
15
25
 
16
26
  def test_branches
17
- puts BranchDb::PostgresqlSwitcher.branches('development', @config['development'])
27
+ assert_stdout "development: Has branch databases. Cannot determine which ones.\n" do
28
+ BranchDb::Switcher.branches('development', @config['development'])
29
+ end
30
+ assert_stdout "" do
31
+ BranchDb::Switcher.branches('development', @config['test'])
32
+ end
33
+ end
34
+
35
+ def test_create_empty_database_non_existing
36
+ switcher = make_switcher(:branch => 'misfeature')
37
+ silence_stream($stdout) do
38
+ switcher.create_empty_database
39
+ end
40
+ switcher.verify(
41
+ [:create_database, 'misfeature']
42
+ )
43
+ end
44
+
45
+ def test_create_empty_database_existing
46
+ switcher = make_switcher
47
+ silence_stream($stderr) do
48
+ switcher.create_empty_database
49
+ end
50
+ switcher.verify
51
+ end
52
+
53
+ def test_create_empty_database_existing_overwrite
54
+ switcher = make_switcher(:overwrite => true)
55
+ silence_stream($stdout) do
56
+ switcher.create_empty_database
57
+ end
58
+ switcher.verify(
59
+ [:drop_database, 'feature'],
60
+ [:create_database, 'feature']
61
+ )
62
+ end
63
+
64
+ def test_copy_database
65
+ switcher = make_switcher(:branch => 'misfeature')
66
+ silence_stream($stdout) do
67
+ switcher.copy_from('feature')
68
+ end
69
+ switcher.verify(
70
+ [:create_database, "misfeature"],
71
+ [:dump_branch_db, "feature"],
72
+ [:load_branch_db, "misfeature", "the-dump-file"]
73
+ )
18
74
  end
19
75
  end
@@ -1,9 +1,82 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
2
1
 
3
- require 'branch_db/switcher'
2
+ require 'test_helper'
3
+ require 'mocks'
4
4
 
5
5
  class TestSqliteSwitcher < Test::Unit::TestCase
6
+ def setup
7
+ create_mock_sqlite_db(
8
+ 'db/development.sqlite3',
9
+ 'db/development_feature.sqlite3',
10
+ 'db/test.sqlite3'
11
+ )
12
+ @config = {
13
+ 'development' => {
14
+ 'database' => 'db/development.sqlite3',
15
+ 'adapter' => 'sqlite3',
16
+ 'per_branch' => true
17
+ },
18
+ 'test' => {
19
+ 'database' => 'db/test.sqlite3',
20
+ 'adapter' => 'sqlite3'
21
+ }
22
+ }
23
+ end
24
+
25
+ def teardown
26
+ FileUtils.rm_rf(RAILS_ROOT)
27
+ end
28
+
29
+ def make_switcher(options = {})
30
+ env = options.delete(:env) || 'development'
31
+ branch = options.delete(:branch) || 'feature'
32
+ BranchDb::SqliteSwitcher.new(env, @config[env], branch, options)
33
+ end
6
34
 
7
- def test_dummy
35
+ def test_branches
36
+ assert_stdout "development: Has branch databases. Cannot determine which ones.\n" do
37
+ BranchDb::Switcher.branches('development', @config['development'])
38
+ end
39
+ assert_stdout "" do
40
+ BranchDb::Switcher.branches('development', @config['test'])
41
+ end
42
+ end
43
+
44
+ def test_create_empty_database_non_existing
45
+ switcher = make_switcher(:branch => 'misfeature')
46
+ silence_stream($stdout) do
47
+ switcher.create_empty_database
48
+ end
49
+ switcher.verify(
50
+ [:create_database, 'misfeature']
51
+ )
52
+ end
53
+
54
+ def test_create_empty_database_existing
55
+ switcher = make_switcher
56
+ silence_stream($stderr) do
57
+ switcher.create_empty_database
58
+ end
59
+ switcher.verify
60
+ end
61
+
62
+ def test_create_empty_database_existing_overwrite
63
+ switcher = make_switcher(:overwrite => true)
64
+ assert_stdout(/^Dropping/) do
65
+ switcher.create_empty_database
66
+ end
67
+ switcher.verify(
68
+ [:create_database, 'feature']
69
+ )
70
+ end
71
+
72
+ def test_copy_database
73
+ switcher = make_switcher(:branch => 'misfeature')
74
+ silence_stream($stdout) do
75
+ switcher.copy_from('feature')
76
+ end
77
+ switcher.verify(
78
+ [:create_database, "misfeature"]
79
+ )
80
+ assert_sqlite_db_exist('db/development_misfeature.sqlite3')
8
81
  end
9
82
  end
@@ -1,6 +1,5 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
2
1
 
3
- require 'branch_db/switcher'
2
+ require 'test_helper'
4
3
 
5
4
  class TestSwitcher < Test::Unit::TestCase
6
5
 
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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Schuerig
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-08 00:00:00 -07:00
12
+ date: 2009-04-09 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,7 @@ files:
59
59
  - Manifest.txt
60
60
  - README.rdoc
61
61
  - Rakefile
62
+ - branch_db.gemspec
62
63
  - lib/branch_db.rb
63
64
  - lib/branch_db/configuration_twiddler.rb
64
65
  - lib/branch_db/postgresql_switcher.rb
@@ -67,7 +68,6 @@ files:
67
68
  - lib/branch_db/task_helper.rb
68
69
  - lib/tasks/db_branches.rb
69
70
  - test/mocks.rb
70
- - test/test_branch_db.rb
71
71
  - test/test_configuration_twiddler.rb
72
72
  - test/test_helper.rb
73
73
  - test/test_postgresql_switcher.rb
@@ -105,5 +105,4 @@ test_files:
105
105
  - test/test_helper.rb
106
106
  - test/test_configuration_twiddler.rb
107
107
  - test/test_postgresql_switcher.rb
108
- - test/test_branch_db.rb
109
108
  - test/test_switcher.rb
@@ -1,13 +0,0 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
2
-
3
- require 'branch_db'
4
-
5
- class TestBranchDb < Test::Unit::TestCase
6
-
7
- def setup
8
- end
9
-
10
- def test_truth
11
- assert true
12
- end
13
- end