brancher 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -0
- data/lib/brancher.rb +10 -1
- data/lib/brancher/auto_copying.rb +65 -0
- data/lib/brancher/database_rename_service.rb +8 -5
- data/lib/brancher/railtie.rb +2 -1
- data/lib/brancher/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c616af20efc6517103ceebed2bd1120d4c0fc5ad
|
4
|
+
data.tar.gz: 317c28a2aa798faf3261b5c0e63cb4354dea04b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76e80a391937a051a3f39a46b17ff249e44669606fe102e99befb2b82c034f281b667612adb1bbb6beee8b7e9ac1fde0cea801bb96b59b92170497abc29fe07f
|
7
|
+
data.tar.gz: 3e0c8ce75d8d71fcfe3885d701f5ed6467e38e13c8ac4426186832b01e394f007623b5bee0cc1e94f2bebe0cb5124d882a8fb85f06e6c2fe133dd8c85ebf9a3b
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Brancher
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/naoty/brancher.svg)](https://travis-ci.org/naoty/brancher)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/naoty/brancher/badges/gpa.svg)](https://codeclimate.com/github/naoty/brancher)
|
4
5
|
|
5
6
|
Brancher is a rubygem to switch databases connected with ActiveRecord by Git branch.
|
6
7
|
|
@@ -22,6 +23,22 @@ And then execute:
|
|
22
23
|
$ bundle
|
23
24
|
```
|
24
25
|
|
26
|
+
## Configuration
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
# config/environments/development.rb
|
30
|
+
|
31
|
+
Brancher.configure do |c|
|
32
|
+
# if branch is "master" or "develop", database name has no suffix.
|
33
|
+
c.except_branches << "master"
|
34
|
+
c.except_branches << "develop"
|
35
|
+
|
36
|
+
# if auto_copy is true and database does not exist,
|
37
|
+
# copy database from no suffix name database to suffixed name one.
|
38
|
+
c.auto_copy = true
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
25
42
|
## Contributing
|
26
43
|
|
27
44
|
1. Fork it ( https://github.com/[my-github-username]/brancher/fork )
|
data/lib/brancher.rb
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
$LOAD_PATH.unshift(__dir__) unless $LOAD_PATH.include?(__dir__)
|
2
2
|
|
3
|
+
require 'active_support/configurable'
|
4
|
+
|
5
|
+
module Brancher
|
6
|
+
include ActiveSupport::Configurable
|
7
|
+
config.except_branches ||= []
|
8
|
+
config.auto_copy ||= false
|
9
|
+
end
|
10
|
+
|
3
11
|
require "brancher/database_configuration_renaming"
|
4
12
|
require "brancher/database_rename_service"
|
13
|
+
require "brancher/auto_copying"
|
5
14
|
require "brancher/railtie"
|
6
|
-
require "brancher/version"
|
15
|
+
require "brancher/version"
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Brancher
|
2
|
+
module AutoCopying
|
3
|
+
def new_connection
|
4
|
+
done = false
|
5
|
+
|
6
|
+
begin
|
7
|
+
super
|
8
|
+
rescue
|
9
|
+
raise if done
|
10
|
+
raise unless Brancher.config.auto_copy
|
11
|
+
|
12
|
+
executor = Executor.new(spec.config)
|
13
|
+
executor.auto_copy
|
14
|
+
done = true
|
15
|
+
retry
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Executor
|
20
|
+
attr_reader :config
|
21
|
+
|
22
|
+
def initialize(config)
|
23
|
+
@config = config
|
24
|
+
end
|
25
|
+
|
26
|
+
def auto_copy
|
27
|
+
suffix = Brancher::DatabaseRenameService.suffix
|
28
|
+
return unless suffix
|
29
|
+
|
30
|
+
database_name = config[:database]
|
31
|
+
original_database_name = database_name.gsub(Regexp.new(suffix), "")
|
32
|
+
|
33
|
+
case config[:adapter]
|
34
|
+
when /mysql/
|
35
|
+
mysql_copy(original_database_name, database_name)
|
36
|
+
when /postgresql/
|
37
|
+
pg_copy(original_database_name, database_name)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def mysql_copy(original_database_name, database_name)
|
42
|
+
system("bundle", "exec", "rake", "db:create")
|
43
|
+
|
44
|
+
cmd = ["mysqldump", "-u", config[:username]]
|
45
|
+
cmd.concat(["-h", config[:host]]) if config[:host].present?
|
46
|
+
cmd.concat(["-p", config[:password]]) if config[:password].present?
|
47
|
+
cmd << original_database_name
|
48
|
+
cmd.concat(["|", "mysql", "-u", config[:username]])
|
49
|
+
cmd.concat(["-h", config[:host]]) if config[:host].present?
|
50
|
+
cmd.concat(["-p", config[:password]]) if config[:password].present?
|
51
|
+
cmd << database_name
|
52
|
+
system(cmd.join(" "))
|
53
|
+
end
|
54
|
+
|
55
|
+
def pg_copy(original_database_name, database_name)
|
56
|
+
env = {}
|
57
|
+
env["PGUSER"] = config[:username] if config[:username].present?
|
58
|
+
env["PGPASSWORD"] = config[:password] if config[:password].present?
|
59
|
+
env["PGHOST"] = config[:host] if config[:host].present?
|
60
|
+
|
61
|
+
system(env, "createdb", "-T", original_database_name, database_name)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -13,18 +13,21 @@ module Brancher
|
|
13
13
|
configurations
|
14
14
|
end
|
15
15
|
|
16
|
+
def suffix
|
17
|
+
return nil if current_branch.blank?
|
18
|
+
return nil if Brancher.config.except_branches.include?(current_branch)
|
19
|
+
|
20
|
+
"_#{current_branch}"
|
21
|
+
end
|
22
|
+
|
16
23
|
private
|
17
24
|
|
18
25
|
def env
|
19
26
|
Rails.env
|
20
27
|
end
|
21
28
|
|
22
|
-
def suffix
|
23
|
-
"_#{current_branch}"
|
24
|
-
end
|
25
|
-
|
26
29
|
def current_branch
|
27
30
|
@current_branch ||= `git rev-parse --abbrev-ref HEAD`.chomp
|
28
31
|
end
|
29
32
|
end
|
30
|
-
end
|
33
|
+
end
|
data/lib/brancher/railtie.rb
CHANGED
@@ -4,6 +4,7 @@ module Brancher
|
|
4
4
|
class Railtie < Rails::Railtie
|
5
5
|
initializer "brancher.rename_database", before: "active_record.initialize_database" do
|
6
6
|
Rails::Application::Configuration.send(:prepend, DatabaseConfigurationRenaming)
|
7
|
+
ActiveRecord::ConnectionAdapters::ConnectionPool.send(:prepend, AutoCopying)
|
7
8
|
end
|
8
9
|
|
9
10
|
rake_tasks do
|
@@ -14,4 +15,4 @@ module Brancher
|
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
17
|
-
end
|
18
|
+
end
|
data/lib/brancher/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: brancher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Naoto Kaneko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -109,6 +109,7 @@ files:
|
|
109
109
|
- Rakefile
|
110
110
|
- brancher.gemspec
|
111
111
|
- lib/brancher.rb
|
112
|
+
- lib/brancher/auto_copying.rb
|
112
113
|
- lib/brancher/database_configuration_renaming.rb
|
113
114
|
- lib/brancher/database_rename_service.rb
|
114
115
|
- lib/brancher/railtie.rb
|
@@ -142,3 +143,4 @@ summary: Switching databases connected with ActiveRecord by Git branch
|
|
142
143
|
test_files:
|
143
144
|
- spec/brancher/database_rename_service_spec.rb
|
144
145
|
- spec/spec_helper.rb
|
146
|
+
has_rdoc:
|