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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 338f02687959d178804d912e5b7055f2e764a644
4
- data.tar.gz: 85e3e43519492a9614418763f0be94c890bdd524
3
+ metadata.gz: c616af20efc6517103ceebed2bd1120d4c0fc5ad
4
+ data.tar.gz: 317c28a2aa798faf3261b5c0e63cb4354dea04b7
5
5
  SHA512:
6
- metadata.gz: d241ee0923aca862c6ef840cdaced23daa31ac6c758cc0e9a2295901b4cae4c5d4947f5f019c98507f527c4119177a54a91c56cb319b891623bb56d5dba1e989
7
- data.tar.gz: 49fc4fd0579356f4b7c844f617355104270949b26e1830cbe5284e5fd92d091a7b94dc4b108f5b8bc9586645747f8b98ce665344fe41ac29a06a7330a0c83c9a
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 )
@@ -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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Brancher
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.2
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-01-19 00:00:00.000000000 Z
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: