brancher 0.2.0 → 0.3.0
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.
- checksums.yaml +4 -4
- data/lib/brancher.rb +2 -0
- data/lib/brancher/auto_copying.rb +5 -6
- data/lib/brancher/database_rename_service.rb +16 -11
- data/lib/brancher/multiple_database_configuration_renaming.rb +15 -0
- data/lib/brancher/railtie.rb +8 -0
- data/lib/brancher/version.rb +1 -1
- data/spec/brancher/database_rename_service_spec.rb +42 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e46188837b98d89a415fd14531e6c8596e28e833
|
4
|
+
data.tar.gz: a30cba0b35d679485f32b964a80deecc52ce522c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d630b069a8244240763bda6357c257e042263ca9289c996f8420918d5da3417ed13e6e6ae525a4576a29492d7c19f99d1701514a529e8130d130a10dabfa7ade
|
7
|
+
data.tar.gz: e65166e71657c1bb0f9d6bfe49404ace55d7c74c913b5bb20eaa1aa5b88ade3050f1fda6e87e8c860467d48ad3a4fec648577f4c6a6f272ed17d06ff5707bcea
|
data/lib/brancher.rb
CHANGED
@@ -6,9 +6,11 @@ module Brancher
|
|
6
6
|
include ActiveSupport::Configurable
|
7
7
|
config.except_branches ||= []
|
8
8
|
config.auto_copy ||= false
|
9
|
+
config.max_database_name_length ||= 63
|
9
10
|
end
|
10
11
|
|
11
12
|
require "brancher/database_configuration_renaming"
|
13
|
+
require "brancher/multiple_database_configuration_renaming"
|
12
14
|
require "brancher/database_rename_service"
|
13
15
|
require "brancher/auto_copying"
|
14
16
|
require "brancher/railtie"
|
@@ -24,11 +24,10 @@ module Brancher
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def auto_copy
|
27
|
-
|
28
|
-
return unless suffix
|
27
|
+
return if config[:database] == config[:original_database]
|
29
28
|
|
30
29
|
database_name = config[:database]
|
31
|
-
original_database_name =
|
30
|
+
original_database_name = config[:original_database]
|
32
31
|
|
33
32
|
case config[:adapter]
|
34
33
|
when /mysql/
|
@@ -39,15 +38,15 @@ module Brancher
|
|
39
38
|
end
|
40
39
|
|
41
40
|
def mysql_copy(original_database_name, database_name)
|
42
|
-
|
41
|
+
ActiveRecord::Tasks::DatabaseTasks.create(config.with_indifferent_access)
|
43
42
|
|
44
43
|
cmd = ["mysqldump", "-u", config[:username]]
|
45
44
|
cmd.concat(["-h", config[:host]]) if config[:host].present?
|
46
|
-
cmd.concat(["-p
|
45
|
+
cmd.concat(["-p#{config[:password]}"]) if config[:password].present?
|
47
46
|
cmd << original_database_name
|
48
47
|
cmd.concat(["|", "mysql", "-u", config[:username]])
|
49
48
|
cmd.concat(["-h", config[:host]]) if config[:host].present?
|
50
|
-
cmd.concat(["-p
|
49
|
+
cmd.concat(["-p#{config[:password]}"]) if config[:password].present?
|
51
50
|
cmd << database_name
|
52
51
|
system(cmd.join(" "))
|
53
52
|
end
|
@@ -4,23 +4,28 @@ module Brancher
|
|
4
4
|
module DatabaseRenameService
|
5
5
|
extend self
|
6
6
|
|
7
|
-
def rename!(configurations)
|
8
|
-
configuration = configurations[
|
9
|
-
|
10
|
-
|
11
|
-
database_name += suffix unless database_name =~ %r{#{suffix}$}
|
12
|
-
configuration["database"] = database_name + database_extname
|
7
|
+
def rename!(configurations, key = env)
|
8
|
+
configuration = configurations[key]
|
9
|
+
configuration["original_database"] = configuration["database"]
|
10
|
+
configuration["database"] = database_name_with_suffix(configuration["database"])
|
13
11
|
configurations
|
14
12
|
end
|
15
13
|
|
16
|
-
|
17
|
-
return nil if current_branch.blank?
|
18
|
-
return nil if Brancher.config.except_branches.include?(current_branch)
|
14
|
+
private
|
19
15
|
|
20
|
-
|
16
|
+
def database_name_with_suffix(database)
|
17
|
+
database_extname = File.extname(database)
|
18
|
+
database_name = database.gsub(%r{#{database_extname}$}) { "" }
|
19
|
+
database_name += suffix unless database_name =~ %r{#{suffix}$}
|
20
|
+
database_name += database_extname
|
21
|
+
database_name = database_name.slice(0,Brancher.config.max_database_name_length-22) + [Digest::MD5.digest(database_name)].pack("m0").slice(0,22).gsub(/[^\w]/, '_').downcase if database_name.length > Brancher.config.max_database_name_length
|
22
|
+
database_name
|
21
23
|
end
|
22
24
|
|
23
|
-
|
25
|
+
def suffix
|
26
|
+
return nil if current_branch.blank? || Brancher.config.except_branches.include?(current_branch)
|
27
|
+
"_#{current_branch}"
|
28
|
+
end
|
24
29
|
|
25
30
|
def env
|
26
31
|
Rails.env
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Brancher
|
2
|
+
module MultipleDatabaseConfigurationRenaming
|
3
|
+
module ClassMethods
|
4
|
+
def establish_connection(spec = nil)
|
5
|
+
DatabaseRenameService.rename!(configurations, spec.to_s) if spec && spec.is_a?(Hash).!
|
6
|
+
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.prepended(base)
|
12
|
+
base.singleton_class.send(:prepend, ClassMethods)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/brancher/railtie.rb
CHANGED
@@ -4,15 +4,23 @@ 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::Base.send(:prepend, MultipleDatabaseConfigurationRenaming)
|
7
8
|
ActiveRecord::ConnectionAdapters::ConnectionPool.send(:prepend, AutoCopying)
|
8
9
|
end
|
9
10
|
|
10
11
|
rake_tasks do
|
11
12
|
namespace :db do
|
12
13
|
task :load_config do
|
14
|
+
require_environment!
|
13
15
|
DatabaseRenameService.rename!(ActiveRecord::Base.configurations)
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
19
|
+
|
20
|
+
def require_environment!
|
21
|
+
return unless defined? Rails
|
22
|
+
environemnt = "#{Rails.root}/config/environments/#{Rails.env}.rb"
|
23
|
+
require environemnt if File.exists?(environemnt)
|
24
|
+
end
|
17
25
|
end
|
18
26
|
end
|
data/lib/brancher/version.rb
CHANGED
@@ -46,7 +46,8 @@ describe Brancher::DatabaseRenameService do
|
|
46
46
|
"adapter" => adapter,
|
47
47
|
"pool" => 5,
|
48
48
|
"timeout" => 5000,
|
49
|
-
"database" => new_database_name
|
49
|
+
"database" => new_database_name,
|
50
|
+
"original_database" => database_name
|
50
51
|
}
|
51
52
|
}
|
52
53
|
end
|
@@ -80,5 +81,45 @@ describe Brancher::DatabaseRenameService do
|
|
80
81
|
|
81
82
|
it { is_expected.to eq new_configurations }
|
82
83
|
end
|
84
|
+
|
85
|
+
context "when the database name is longer than max_database_name_length" do
|
86
|
+
let(:adapter) do
|
87
|
+
"mysql2"
|
88
|
+
end
|
89
|
+
|
90
|
+
let(:database_name) do
|
91
|
+
"this_is_a_very_long_sample_app_development_database_name_that_exceeds_the_default_set_max_database_name_length"
|
92
|
+
end
|
93
|
+
|
94
|
+
let(:new_database_name) do
|
95
|
+
"this_is_a_very_long_sample_app_developmen#{[Digest::MD5.digest("#{database_name}_#{branch}")].pack('m0').slice(0,22).gsub(/[^\w]/, '_').downcase}"
|
96
|
+
end
|
97
|
+
|
98
|
+
it { is_expected.to eq new_configurations }
|
99
|
+
end
|
100
|
+
|
101
|
+
context "when it connect another database" do
|
102
|
+
let(:adapter) do
|
103
|
+
"mysql2"
|
104
|
+
end
|
105
|
+
|
106
|
+
let(:database_name) do
|
107
|
+
"sample_app_another_db"
|
108
|
+
end
|
109
|
+
|
110
|
+
let(:new_database_name) do
|
111
|
+
"#{database_name}_#{branch}"
|
112
|
+
end
|
113
|
+
|
114
|
+
let(:env) do
|
115
|
+
"another_db"
|
116
|
+
end
|
117
|
+
|
118
|
+
subject do
|
119
|
+
Brancher::DatabaseRenameService.rename!(configurations, env)
|
120
|
+
end
|
121
|
+
|
122
|
+
it { is_expected.to eq new_configurations }
|
123
|
+
end
|
83
124
|
end
|
84
125
|
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.
|
4
|
+
version: 0.3.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-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- lib/brancher/auto_copying.rb
|
113
113
|
- lib/brancher/database_configuration_renaming.rb
|
114
114
|
- lib/brancher/database_rename_service.rb
|
115
|
+
- lib/brancher/multiple_database_configuration_renaming.rb
|
115
116
|
- lib/brancher/railtie.rb
|
116
117
|
- lib/brancher/version.rb
|
117
118
|
- spec/brancher/database_rename_service_spec.rb
|
@@ -143,4 +144,3 @@ summary: Switching databases connected with ActiveRecord by Git branch
|
|
143
144
|
test_files:
|
144
145
|
- spec/brancher/database_rename_service_spec.rb
|
145
146
|
- spec/spec_helper.rb
|
146
|
-
has_rdoc:
|